Paypal express fails when credits > item price

Posts: 12
Joined: 07/26/2008

I am using ubercart with userpoints. Sometimes, credits from userpoints are larger than item price (for example, item price = $10, shipping = $5, customer pays with 12 points + $3). Paypal (and other gateways) do not accept subtotal of zero or less. Payment would fail, but worse is that customer do not get error message, but a message saying that her order is complete.

I added the below code to the end of function uc_paypal_ec_submit_form_submit in uc_paypal.module (around line 1150) - removes subtotal, shipping and tax lines when subtotal < 0, also checks for success or failure. It seems to be working fine, but should any of the $_SESSION variables be unset or otherwise changed in case of failuer?

if ($subtotal < 0) {
$nvp_request = array(
'METHOD' => 'DoExpressCheckoutPayment',
'TOKEN' => $_SESSION['TOKEN'],
'PAYMENTACTION' => variable_get('uc_paypal_wpp_payment_action', 'Sale'),
'PAYERID' => $_SESSION['PAYERID'],
'AMT' => uc_currency_format($order->order_total, FALSE, FALSE, '.'),
'DESC' => substr($desc, 0, 127),
'INVNUM' => $order->order_id .'-'. time(),
'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US',
'NOTIFYURL' => url('uc_paypal/ipn', NULL, NULL, TRUE),
'CURRENCYCODE' => variable_get('uc_paypal_wpp_currency', 'USD'),
);
}
else {
$nvp_request = array(
'METHOD' => 'DoExpressCheckoutPayment',
'TOKEN' => $_SESSION['TOKEN'],
'PAYMENTACTION' => variable_get('uc_paypal_wpp_payment_action', 'Sale'),
'PAYERID' => $_SESSION['PAYERID'],
'AMT' => uc_currency_format($order->order_total, FALSE, FALSE, '.'),
'DESC' => substr($desc, 0, 127),
'INVNUM' => $order->order_id .'-'. time(),
'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US',
'NOTIFYURL' => url('uc_paypal/ipn', NULL, NULL, TRUE),
'ITEMAMT' => uc_currency_format($subtotal, FALSE, FALSE, '.'),
'SHIPPINGAMT' => uc_currency_format($shipping, FALSE, FALSE, '.'),
'TAXAMT' => uc_currency_format($tax, FALSE, FALSE, '.'),
'CURRENCYCODE' => variable_get('uc_paypal_wpp_currency', 'USD'),
);
}

$nvp_response = uc_paypal_api_request($nvp_request, variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'));

//check for success here
if ($nvp_response['ACK'] == 'Success') {
unset($_SESSION['TOKEN'], $_SESSION['PAYERID']);
$_SESSION['do_complete'] = TRUE;
drupal_goto('cart/checkout/complete');
} else
{
//Do we need to change any of the session varibales here?
drupal_set_message(t('There was an error while processing your PayPal payment. Please try another payment method.'), 'error');
watchdog('uc_paypal', 'Paypal payment failed: ' . $nvp_response['L_LONGMESSAGE0'], WATCHDOG_ERROR);
drupal_goto('cart/checkout/review');
}
}

Posts: 5353
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

You might check out the free order payment method contribution... if an order totals $0.00 or less it will reduce payment options to a single payment method for free orders.

http://www.ubercart.org/contrib/4089

Posts: 12
Joined: 07/26/2008

Thanks, but this doesn't really work for me cause the total doesn't drop to zero. Subtotal is less than zero, but total is more than zero (because of shipping and/or tax).