【问题标题】:How to set up Paypal Smart Buttons with Flask?如何使用 Flask 设置 Paypal 智能按钮?
【发布时间】:2020-07-14 18:12:53
【问题描述】:

我已经为 Paypal Payments 进行了服务器端设置。我从 Html 表单中获取产品价格,然后使用 paypalrestsdk 模块 创建付款。整个系统的重要代码块在这里:-

payment = paypalrestsdk.Payment({
        "intent": "sale",
        # Set payment method
        "payer": {
            "payment_method": "paypal"
        },
        # Set redirect URLs
        "redirect_urls": {
            "return_url": url_for('after_payment_done_2_sol', hash_id=hash_id, user_id=current_user.user_id, _external=True),
            "cancel_url": url_for('topup', _external=True)
        },
        # Set transaction object
        "transactions": [{
            "amount": {
                "total": query.total_price,
                "currency": "USD"
            },
            "description": f"Payment Method for paying {query.total_price} for Top Up"
        }]
    })
    if payment.create():
        print('Payment "{}" created successfully'.format(payment.id))
        for link in payment.links:
            if link.method == "REDIRECT":
                redirect_url = str(link.href)
                print('Redirect for approval: {}'.format(redirect_url))
                return redirect(redirect_url)

因此,这将创建一个指向“通过 Paypal 支付 X 金额”的链接。但我想像这里一样集成智能按钮 https://developer.paypal.com/demo/checkout/#/pattern/server。现在这里有一个代码块,如:

 <script>
    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({
        // How Does this work?????????????????????
        // Set up the transaction
        createOrder: function(data, actions) {
            return fetch('/demo/checkout/api/paypal/order/create/', {
                method: 'post'
            }).then(function(res) {
                return res.json();
            }).then(function(data) {
                return data.orderID;
            });
        },
        // ???????????????????????????????????????? How does this Work?????
        // Finalize the transaction
        onApprove: function(data, actions) {
            return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', {
                method: 'post'
            }).then(function(res) {
                return res.json();
            }).then(function(details) {
                // Show a success message to the buyer
                alert('Transaction completed by ' + details.payer.name.given_name + '!');
            });
        }


    }).render('#paypal-button-container');
</script>

所以这里 onAprrove 和 createOrder 是如何工作的?我应该如何在烧瓶中设置我的路线?请帮忙!

【问题讨论】:

    标签: flask paypal


    【解决方案1】:

    这是一个服务器端集成,因此需要将/demo/.... 替换为将在您的网络服务器上触发服务器端代码的 URL。

    如果您需要 javascript 进行客户端集成,请使用 https://developer.paypal.com/demo/checkout/#/pattern/client

    【讨论】:

    • 谢谢老兄!但是我应该在获取 createOrder 时从服务器返回什么?我应该返回或传递 onApprove 什么?您能否详细说明这两件事将如何与服务器端一起使用?只是程序,伙计。
    • developer.paypal.com/docs/checkout/reference/server-integration 设置事务时,返回一个 orderID。 onApprove 应该调用捕获 orderID 并返回成功/失败的服务器代码
    猜你喜欢
    • 1970-01-01
    • 2021-09-30
    • 2020-10-06
    • 2020-10-19
    • 2020-06-28
    • 2020-11-19
    • 2020-10-06
    • 2019-12-28
    • 2021-10-31
    相关资源
    最近更新 更多