【发布时间】:2016-03-23 07:15:18
【问题描述】:
阅读https://developer.paypal.com/docs/api/后,我对Paypal流事件有点误解。我想将快速结帐和信用卡付款集成到我的网站。我正在使用 Flask 和 paypalrestsdk 没有任何 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。应用程序永远不会被告知买方已批准付款,它应该在我自己的数据库中更新,并且应该将新许可证发送给那个人。
问题:
我找不到使用
pyhtonrestsdk定义某些回调的方法。怎么办?即使我使用
data-callback添加回调(我尝试使用纯 Javascript 按钮代码嵌入 Express Checkout),我的应用程序也没有被调用。我怀疑是因为远程服务器无法调用http://127.0.0.1/payment/paypal/success用户可以在单击“确认”后立即关闭带有 PayPal 确认的窗口,因此我无法相信它稍后会以某种方式执行的浏览器重定向。
最后,我怀疑我不清楚 PayPal 工作流程,但我无法在开发者门户上找到有关它的更多信息。
【问题讨论】:
-
我在尝试重定向时遇到了一些奇怪的 CORS 错误。 “无法加载 paypal.sandbox.com,请求的资源上没有 'Access-Control-Allow-Origin' 标头。”
标签: javascript paypal flask