首页 > 解决方案 > PayPal 智能支付按钮:设置发票 ID 以避免重复交易

问题描述

有时,由于意外点击了paypal 智能支付按钮中的支付按钮,用户多次收费。在贝宝仪表板 > 付款首选项中,我已经启用了“是的,每个发票 ID 阻止多次付款”选项。

现在我想在我的智能按钮脚本代码中设置发票 ID,如下所示:

function initPayPalButton() {
      paypal.Buttons({
        style: {
          shape: 'rect',
          color: 'gold',
          layout: 'vertical',
          label: 'pay',
        },

        createOrder: function(data, actions) {
          return actions.order.create({
            purchase_units: [{"amount":{"currency_code":"USD","value":total_amount}}],
                            application_context: {
                                shipping_preference: 'NO_SHIPPING'
                            }
          });
        },

        onApprove: function(data, actions) {
          return actions.order.capture().then(function(details) {
            window.location = 'success_url';
          });
        },

        onError: function(err) {
          console.log(err);
                                        alert('Something went wrong. Please refresh the page and try again.');
        }
      }).render('#button_container_id');
    }
    initPayPalButton();

标签: paypal

解决方案


添加invoice_id系统中的唯一标识符以避免重复付款。

请参阅:https ://developer.paypal.com/docs/multiparty/checkout/standard/integrate/#link-addpaymentbuttons

其他有用的购买单位参数包括:

invoice_id未用于先前完成的交易的唯一标识,用于识别用于会计目的的订单并防止重复付款。

您选择的custom_id值供您的系统或自己参考。此值对买方不编入索引或可见。

这就是我的代码的样子:

paypal.Buttons({
    style: {
        shape: 'rect',
        color: 'silver',
        label: 'buynow'
    },
    onError: function (err) {
        // FIXME: show the user an error message
    },
    onClick: function(data)  {
        // FIXME: do something cool when the button is clicked
    },
    // Set up the transaction
    createOrder: function(data, actions) {
        return actions.order.create({
            purchase_units: [{
                description: 'My awesome product',
                amount: {
                    currency_code: "USD",
                    value: 9.99,
                    breakdown: {
                        item_total: {
                            currency_code: 'USD',
                            value: 9.99
                        },
                        discount: {
                            currency_code: "USD",
                            value: 1.25
                        }
                    }
                },
                invoice_id: 'UNIQUE-ID',
                custom_id: 'ANOTHER-SYSTEM-ID'
            }],
            application_context:  { 
                shipping_preference: "NO_SHIPPING"
            }
        });
    },
    // Finalize the transaction
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
            // FIXME: finalise the order and show the success message
            console.log("order id: "+details.id);
        });
    }
}).render('#paypal-button-container');

}


推荐阅读