【问题标题】:Paypal integration to Flask applicationPaypal 与 Flask 应用程序的集成
【发布时间】:2016-03-23 07:15:18
【问题描述】:

阅读https://developer.paypal.com/docs/api/后,我对Paypal流事件有点误解。我想将快速结帐和信用卡付款集成到我的网站。我正在使用 Flaskpaypalrestsdk 没有任何 Flask 扩展。

这是我的应用程序的摘录:

@app.route('/', methods=['GET'])
def index():
    # Page with but form, price/quantity/name values
    # are stored in hidden fields, "Buy now" acts as submit
    return render_template('index.html')

@app.route('/payment/paypal', methods=['POST'])
def payment_paypal():
    # Here I am creating dict with required params
    payment_template = {
        'intent': 'sale',
        'payer': {'payment_method': 'paypal'},
        'redirect_urls': {
          'return_url': url_for('payment_paypal_execute'),
          'cancel_url': url_for('payment_paypal_error')
        },
        ......
    }

    payment = paypalrestsdk.Payment(payment)

    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_urls)

@app.route('/payment/paypal/execute', methods=['GET'])
def payment_paypal_execute():
    payer_id = request.args.get('payerId')
    payment_id = request.args.get('paymentId')
    token = request.args.get('token')

    pending_payment = PayPalPayment.query.filter_by(token=token).filter_by(state='created').first_or_404()

    try:
        payment = paypalrestsdk.Payment.find(pending_payment.payment_id)
    except paypalrestsdk.exceptions.ResourceNotFound as ex:
        print('Paypal resource not found: {}'.format(ex))
        abort(404)

    if payment.execute({"payer_id": payer_id}):
        pending_payment.state = payment.state
        pending_payment.updated_at = datetime.strptime(payment.update_time, "%Y-%m-%dT%H:%M:%SZ")
        db.session.commit()
        return render_template('payment/success.html', payment_id=payment.id, state=payment.state)

    return render_template('payment/error.html', payment_error=payment.error, step='Finallizing payment')

工作正常,点击按钮payment 成功创建(状态为created)后,用户重定向到审批页面。在那里他点击“确认”......当我指定return_url时,我再也没有回到我的应用程序,事件! IE。应用程序永远不会被告知买方已批准付款,它应该在我自己的数据库中更新,并且应该将新许可证发送给那个人。

问题:

  1. 我找不到使用pyhtonrestsdk 定义某些回调的方法。怎么办?

  2. 即使我使用data-callback 添加回调(我尝试使用纯 Javascript 按钮代码嵌入 Express Checkout),我的应用程序也没有被调用。我怀疑是因为远程服务器无法调用http://127.0.0.1/payment/paypal/success

  3. 用户可以在单击“确认”后立即关闭带有 PayPal 确认的窗口,因此我无法相信它稍后会以某种方式执行的浏览器重定向。

最后,我怀疑我不清楚 PayPal 工作流程,但我无法在开发者门户上找到有关它的更多信息。

【问题讨论】:

  • 我在尝试重定向时遇到了一些奇怪的 CORS 错误。 “无法加载 paypal.sandbox.com,请求的资源上没有 'Access-Control-Allow-Origin' 标头。”

标签: javascript paypal flask


【解决方案1】:

像往常一样,魔鬼隐藏在细节中。我的主要问题如下:paypal 不会将我重定向到我的应用程序,但我发现它(在确认后)将我重定向到看起来像 https://sandbox.paypal.com/ 的 URL,其中查询字符串包含所需的参数。 IE。 redirect_urls 按预期工作,只是将我重定向到错误的主机。

在那之后我记得url_for 生成相对链接。所以刚刚添加了关键字_external=True 我已经被重定向到我的应用程序,所有必需的参数和付款成功确认并执行。

即正确的 redirect_urls 块看起来像:

'redirect_urls': {
    'return_url': url_for('payment_paypal_execute', _external=True),
    'cancel_url': url_for('payment_paypal_error', _external=True)
}

最后我有以下工作流程:

  1. 打开/ (index) 有按钮Pay with PayPal 这是表格里面的图片按钮。在这个按钮表单旁边包含了数量、产品名称和数量的隐藏字段(实际上如果不是好主意,因为我们不能信任用户,所以我只存储了product_license_type_id,它存储在数据库中并包含有关产品的所有必需信息)。

  2. 一旦点击它POST 表单到 '/payment/paypal' (paypal_create) 创建对象Payment 并填充所有字段。如果调用payment.create 成功完成,它还会在我自己的数据库中使用payment_idstate 创建记录(这些字段与paypal 工作流相关,当然实际上我正在存储与我的应用相关的几个其他字段)。

  3. 在 PayPal 端创建付款后,应用程序会查看列表 payment.links 的响应。我们想要一个 rel == 'approval_url'method == 'REDIRECT' 并返回 flask.redirect(found_link)

  4. 在 PayPal 网站上,买家应点击“批准”,查看送货地址,然后他将立即重定向到 redirect_urls.return_url,查询字符串中包含以下参数:PayerIDpaymentIdtoken

  5. 1234563 /p>
  6. 当完成付款时,状态变为approved

  7. ....

  8. 利润!

UPD:您无需在销售帐户偏好中打开“自动重定向”,这种方法适合将一个帐户集成到多个站点。

【讨论】:

  • 抱歉第二条评论,但在第 3 步,当我尝试重定向时,我得到“无法加载 paypal.sandbox.com 没有 'Access-Control-Allow-Origin'。我在本地运行烧瓶。你遇到过这种情况吗?
猜你喜欢
  • 2013-09-09
  • 2022-10-08
  • 2013-08-18
  • 2021-03-14
  • 2011-07-25
  • 2015-05-26
  • 2013-07-29
  • 2021-03-26
  • 1970-01-01
相关资源
最近更新 更多