Why does the code fail after receiving a response

I am running the code below and it takes the user to PayPal for payment and then returns it in return_url

as expected. However, the code is no longer executed and it does not complete the payment.

I based my code on 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

      

So, in the example https://devtools-paypal.com/guide/pay_paypal/python?success=true&token=EC-8JL96732FP068791F&PayerID=QQGSRNHDACTLJ . Step 5 fails and no response is sent from PayPal?

+3


source to share


3 answers


Here is Avi from PayPal. I'm not very familiar with Tornado, but after the line return self.redirect(redirect_url)

happens in your code and returns the user to return_url, payment.execute({"payer_id": response['payer_id']})

are you getting the payer correctly? Payer_id is returned in return_url as one of the parameters in the format http://<return_url>?token=EC-60U79048BN7719609&PayerID=7E7MGXCWTTKK2

. Also what is the status of the payment after execution payment = paypal.Payment.find(payment.id)

. Another suggestion I would like to make is to check what print payment.error

prints out a helpful debug message and debug_id that PayPal point of sale tech services can use to look at the issue.



+2


source


You need a different url where papypal redirects when the payment was successful, where you will get the token and PayerID. In this GET method, you can put this piece of code (pseudocode):

 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

      



You will need to keep the payment_id between calls.

+1


source


Why are you using

return self.redirect(redirect_url)

      

I think you can only use

self.redirect(redirect_url)

      

I've never seen a return in Tornado handlers.

0


source







All Articles