During the Step 2 of checkout, it seems like depending on the percentage of the coupon, the payment would not go through and it won't redirect to PayPal because the amounts sent in the request don't add up.
While debugging, I found that all the amounts are exact and not rounded until the end when sent to PayPal. For example:
Item 2.95, coupon 5% (.1475), tax (.23895), total 3.04145. Due to this, I have to calculate amount after minus discount as total - tax (this is more accurate since it represents subtotal). If I calculate by item_total minus discount, there's a chance that I can be off by .01.
Another reason is that I am not charging for shipping (delivery) and the discount coupon is only applied to the shipping when sent to PayPal for collection and it errors out since it cannot apply a discount to the shipping when shipping is 0 (PAYMENTREQUEST_0_SHIPDISCAMT)
And the third reason is probably that the discount amount is not rounded to two decimals when sent to PayPal.
The file that I only modified was: tastyigniter\extensions\paypal_express\models\Paypal_model.php
Removed lines containing: PAYMENTREQUEST_0_SHIPDISCAMT
Within setExpressCheckout:
[php]
// Commented these lines
// foreach (array_keys($cart_items) as $key => $rowid) { // loop through cart items to create items name-value pairs data to be sent to paypal
// foreach ($cart_items as $cart_item) {
// if (isset($cart_item['rowid']) AND $rowid === $cart_item['rowid']) {
// if (!empty($cart_item['options']['option_id'])) {
// $cart_options = $cart_item['name'] .': '. $this->currency->format($cart_item['price']);
// } else {
// $cart_options = '';
// }
//
// $nvp_data .= '&L_PAYMENTREQUEST_0_NUMBER'. $key .'='. urlencode($cart_item['id']);
// $nvp_data .= '&L_PAYMENTREQUEST_0_NAME'. $key .'='. urlencode($cart_item['name']);
// $nvp_data .= '&L_PAYMENTREQUEST_0_DESC'. $key .'='. urlencode($cart_options);
// $nvp_data .= '&L_PAYMENTREQUEST_0_QTY'. $key .'='. urlencode($cart_item['qty']);
// $nvp_data .= '&L_PAYMENTREQUEST_0_AMT'. $key .'='. urlencode($cart_item['price']);
// }
// }
// }
if ($this->cart->coupon_discount()) {
$nvp_data .= '&PAYMENTREQUEST_0_ITEMAMT='. urlencode(round($this->cart->order_total(), 2, PHP_ROUND_HALF_UP) - round($this->cart->tax_amount(), 2, PHP_ROUND_HALF_UP));
}
else
$nvp_data .= '&PAYMENTREQUEST_0_ITEMAMT='. urlencode($this->cart->total());[/php]
Within doExpressCheckout:
[php] if ($this->cart->coupon_discount()) {
$nvp_data .= '&PAYMENTREQUEST_0_ITEMAMT='. urlencode(round($this->cart->order_total(), 2, PHP_ROUND_HALF_UP) - round($this->cart->tax_amount(), 2, PHP_ROUND_HALF_UP));
}
else
$nvp_data .= '&PAYMENTREQUEST_0_ITEMAMT='. urlencode($this->cart->total());
if ($this->cart->delivery() > 0) {
$nvp_data .= '&PAYMENTREQUEST_0_SHIPPINGAMT='. urlencode($this->cart->delivery());
}[/php]
My changes are in addition to the previous changes posted here: https://forum.tastyigniter.com/thread-682.html