【问题标题】:PayPal Checkout (javascript) with Smart Payment Buttons create order problem带有智能支付按钮的 PayPal Checkout (javascript) 创建订单问题
【发布时间】:2019-10-18 06:11:12
【问题描述】:

在我的网页上,我正在尝试按照手册使用 JavaScript 实现 PayPal 结账:https://developer.paypal.com/docs/checkout/
标准选项一切正常。例如,这工作得很好:

paypal.Buttons({
    createOrder: function(data, actions) {
        return actions.order.create({
            purchase_units: [{
                amount: {
                    currency_code: 'EUR',
                    value: '120.16'
                },
                description: 'Purchase Unit test description',
                custom_id: '64735',
            }]
        })
    },
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
            alert('Transaction completed by ' +
                  details.payer.name.given_name)
            // Call your server to save the transaction
            return fetch('/api/paypal-transaction-complete', {
                method: 'post',
                headers: {
                    'content-type': 'application/json'
                },
                body: JSON.stringify({
                    orderID: data.orderID
                })
            })
        })
    }
}).render('#paypal-button-container')

但是当我尝试更具体地了解订单详细信息时,它给了我一个错误:

Error: "Order Api response error: 
{ 
    "name": "INVALID_REQUEST", 
    "message": "Request is not well-formed, syntactically incorrect, or violates schema.", 
    "debug_id": "1ed03d18530c1", 
    "details": [ 
        { 
            "location": "body", 
            "issue": "INVALID_SYNTAX", 
            "description": "Cannot deserialize instance of `com.paypal.api.platform.checkout.orders.v2.model.AmountBreakdown` out of START_ARRAY token line: 1, column: 82" 
            } 
            ], 
            "links": [ 
                { 
                    "href": "https://developer.paypal.com/docs/api/orders/v2/#error-INVALID_SYNTAX", "rel": "information_link", "encType": "application/json" 
                } 
            ] 
        }"
}

这是我的代码:

paypal.Buttons({
    createOrder: function(data, actions) {
        return actions.order.create({
            purchase_units: [{
                amount: {
                    currency_code: 'EUR',
                    value: '120.16',
                    breakdown: [{
                                    item_total: {
                                        unit_amount: 7,
                                        currency_code: 'EUR',
                                        value: '120.16'
                                    }
                                    }]

                },
                description: 'Purchase Unit test description',
                custom_id: '64735',
                items: [{
                    name: 'Test item 1',
                    unit_amount: {
                        currency_code: 'EUR',
                        value: '60.12'
                    },
                    quantity: 2,
                    description: 'Uaua item 1 description'
                }, {
                    name: 'Test item 2',
                    unit_amount: {
                        currency_code: 'EUR',
                        value: '60.00'
                    },
                    quantity: 5,
                    description: 'Test item 2 description'
                }]
            }]
        })
    },
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
            alert('Transaction completed by ' +
                  details.payer.name.given_name)
            // Call your server to save the transaction
            return fetch('/api/paypal-transaction-complete', {
                method: 'post',
                headers: {
                    'content-type': 'application/json'
                },
                body: JSON.stringify({
                    orderID: data.orderID
                })
            })
        })
    }
}).render('#paypal-button-container')

有谁知道问题出在哪里? PayPal 文档信息量不是很大...

【问题讨论】:

    标签: javascript paypal


    【解决方案1】:

    经过一番挖掘,我找到了正确的格式:

    paypal.Buttons({
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [
                {
                    reference_id: "PUHF",
                    description: "Some description",
    
                    custom_id: "Something7364",
                    soft_descriptor: "Great description 1",
                    amount: {
                        currency_code: "EUR",
                        value: "200.00",
                        breakdown: {
                            item_total: {
                                currency_code: "EUR",
                                value: "200.00"
                            }
                        }
                    },
                    items: [
                        {
                            name: "Item 1",
                            description: "The best item ever",
                            sku: "xyz-2654",
                            unit_amount: {
                                currency_code: "EUR",
                                value: "100.00"
                            },
                            quantity: "1"
                        },
                        {
                            name: "Item 2",
                            description: "Not bad too",
                            sku: "zdc-3942",
                            unit_amount: {
                                currency_code: "EUR",
                                value: "50.00"
                            },
                            quantity: "2"
                        }
                    ],
    
                }
            ]
        });
    },
    onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
            alert('Transaction completed by ' + details.payer.name.given_name);
            // Call your server to save the transaction
            return fetch('/api/paypal-transaction-complete', {
                method: 'post',
                headers: {
                    'content-type': 'application/json'
                },
                body: JSON.stringify({
                    orderID: data.orderID
                })
            });
        });
    }
    }).render('#paypal-button-container');
    

    这是一组完整选项的示例:

    paypal.Buttons({
        createOrder: function(data, actions) {
            return actions.order.create({
                purchase_units: [{
                    reference_id: "PUHF",
                    description: "Sporting Goods",
    
                    custom_id: "CUST-HighFashions",
                    soft_descriptor: "HighFashions",
                    amount: {
                        currency_code: "USD",
                        value: "230.00",
                        breakdown: {
                            item_total: {
                                currency_code: "USD",
                                value: "180.00"
                            },
                            shipping: {
                                currency_code: "USD",
                                value: "30.00"
                            },
                            handling: {
                                currency_code: "USD",
                                value: "10.00"
                            },
                            tax_total: {
                                currency_code: "USD",
                                value: "20.00"
                            },
                            shipping_discount: {
                                currency_code: "USD",
                                value: "10"
                            }
                        }
                    },
                    items: [{
                        name: "T-Shirt",
                        description: "Green XL",
                        sku: "sku01",
                        unit_amount: {
                             currency_code: "USD",
                             value: "90.00"
                        },
                        tax: {
                            currency_code: "USD",
                            value: "10.00"
                        },
                        quantity: "1",
                        category: "PHYSICAL_GOODS"
                    },
                        {
                        name: "Shoes",
                        description: "Running, Size 10.5",
                        sku: "sku02",
                        unit_amount: {
                             currency_code: "USD",
                             value: "45.00"
                        },
                        tax: {
                            currency_code: "USD",
                            value: "5.00"
                        },
                        quantity: "2",
                        category: "PHYSICAL_GOODS"
                    }
                    ],
                    shipping: {
                        method: "United States Postal Service",
                        address: {
                            name: {
                                full_name: "John",
                                surname: "Doe"
                            },
                            address_line_1: "123 Townsend St",
                            address_line_2: "Floor 6",
                            admin_area_2: "San Francisco",
                            admin_area_1: "CA",
                            postal_code: "94107",
                            country_code: "US"
                        }
                    }
                }]
            })
        },
        onApprove: function(data, actions) {
            return actions.order.capture().then(function(details) {
                alert('Transaction completed by ' + details.payer.name.given_name)
                // Call your server to save the transaction
                return fetch('/api/paypal-transaction-complete', {
                    method: 'post',
                    headers: {
                        'content-type': 'application/json'
                    },
                    body: JSON.stringify({
                        orderID: data.orderID
                    })
                })
            })
        }
    }).render('#paypal-button-container')
    

    【讨论】:

    • 谢谢!它只是工作。尽管对 PayPal 感到失望,但您的代码是在线可用的最佳文档。 PayPal 应该聘请您更新他们的文档。
    • 很高兴它有帮助 :) 如果你喜欢它,请投票。
    • 有效!很难找到这样一个实际的例子。
    • 我同意 Lukas - 我找到的最佳文档。谢谢!
    • 我确实有一个问题。当您“调用您的服务器以保存交易”时,您正在传递它 data.orderID 但我在 createOrder 中看不到它。那是reference_id还是custom_id?
    猜你喜欢
    • 2020-10-21
    • 2021-01-28
    • 2020-05-09
    • 2020-08-07
    • 2020-03-23
    • 2019-12-11
    • 2020-06-23
    • 2019-11-16
    • 2019-10-31
    相关资源
    最近更新 更多