为什么代码在收到响应后没有执行

时间:2014-08-14 18:13:59

标签: python paypal tornado paypal-sandbox

我正在运行以下代码,用户需要PayPal进行付款,然后按预期将其返回return_url。但是,代码不再执行,也不执行付款。

我的代码基于https://github.com/paypal/rest-api-sdk-python

class PayPalHandler(tornado.web.RequestHandler):

    def get(self):

        logging.basicConfig(level=logging.INFO)

        paypal.configure({
                             "mode": PAYPAL_MODE, 
                             "client_id": PAYPAL_CLIENT_ID,
                             "client_secret": PAYPAL_CLIENT_SECRET})



        payment = paypal.Payment({
            "intent":  "sale",


            "payer":  {
                 "payment_method":  "paypal" },


            "redirect_urls": {
                 "return_url": "http://127.0.0.1:8000/ty",
                  "cancel_url": "http://127.0.0.1:8000/" },


            "transactions":  [ {

                 "item_list": {
                      "items": [{
                                 "name": "membership",
                                 "price": "2.00",
                                 "currency": "GBP",
                                 "quantity": 1 }]},


                      "amount":  {
                            "total":  "2.00",
                             "currency":  "GBP" },
                             "description":  "One of membership fee." } ] } )

        redirect_url = ""

        if payment.create():
            print("Payment[%s] created successfully"%(payment.id))

            for link in payment.links:
                if link.method == "REDIRECT":
                    redirect_url = link.href
            print("Redirect for approval: %s"%(redirect_url))
            return self.redirect(redirect_url)

        else:
            print("Error while creating payment.")
            print(payment.error)


        response = payment.to_dict()
        print response
        payment = paypal.Payment.find(payment.id)

        if payment.execute({"payer_id": response['payer_id']}):              
            print ("Payment executed successfully")
        else:
            print(payment.error) # Error Hash

        print payment.to_dict()

        print userData

所以在https://devtools-paypal.com/guide/pay_paypal/python?success=true&token=EC-8JL96732FP068791F&PayerID=QQGSRNHDACTLJ的示例中。第5步没有发生,PayPal没有回复?

3 个答案:

答案 0 :(得分:2)

这是来自PayPal的Avi。我对Tornado并不是很熟悉,但是在您的代码中出现return self.redirect(redirect_url)行并将用户返回到return_url之后,在payment.execute({"payer_id": response['payer_id']})中您是否正确获得了payer_id? Payer_id作为格式为http://<return_url>?token=EC-60U79048BN7719609&PayerID=7E7MGXCWTTKK2的参数之一附加到return_url。此外,执行payment = paypal.Payment.find(payment.id)后付款的状态如何。我想要的另一个建议是查看print payment.error是否打印了一个有用的调试消息和一个debug_id,paypal商业技术服务可以用来查看这个问题。

答案 1 :(得分:1)

当付款成功时,您需要其他网址,其中papypal重定向,您将收到令牌和PayerID。在那个GET方法中,你可以把这部分代码(伪代码):

 payerid_param = request.get('PayerID')
 payment = paypal.Payment.find(db_payment.id)

 if payment.execute({"payer_id": payerid_param}):              
    print ("Payment executed successfully")
 else:
    print(payment.error) # Error Hash

您需要在两次通话之间保存payment_id。

答案 2 :(得分:0)

为什么使用

return self.redirect(redirect_url)

我认为你可以只使用

self.redirect(redirect_url)

我从未在Tornado处理程序中看到return语句。