aldo_vw
aldo_vw

Reputation: 638

Implementation of Paypal REST API in Python

I've been following PayPal directions to implement its REST API in Python, but I'm missing some key pieces of the puzzle and I don't know where to find them. So far I have the following code to start processing a payment:

api = paypalrestsdk.set_config(
    mode="sandbox", 
    client_id="XXX", 
    client_secret="XXX")

api.get_token()

payment = paypalrestsdk.Payment({
    "intent": "sale",
    "payer": { "payment_method": "paypal" },
    "redirect_urls": {
        "return_url": "https://XXX",
        "cancel_url": "https://XXX" },

    "transactions": [ {
        "amount": {
            "total": "50",
            "currency": "USD" },
        "description": "creating a payment" 
        } ] 
    } )

payment.create()

So far, so good (though I don't know what to do with the token I get...). Here begin my problems (or lack or knowledge). First, I need the approval url. And then, in the next steps, I don't know how to get the information needed to process the execution of the payment: payment id and payer id

payment = paypalrestsdk.Payment.find("XXX")
payment.execute({"payer_id": "XXX"})

These last two lines are key to make the transaction happen. Where do I get their arguments?

Thanks in advance for your help!

Upvotes: 1

Views: 5366

Answers (3)

Zeeshan Husain
Zeeshan Husain

Reputation: 1

First, get the payment id after creating payment and save it in the user session like this:

if payment.create():
    print('Payment success!')
    request.session["payment_id"] = payment.id
else:
    print(payment.error)

After that write function for execute payment and get "Payer id" from returned url:

def execute(request):
    success = False
    payment_id = request.session["payment_id"]
    payment = paypalrestsdk.Payment.find(payment_id)
    if payment.execute({'payer_id':request.POST.get("payerID")}):
        print('Execute success!')
        success = True
    else:
        print(payment.error)

Thank you

Upvotes: 0

Loris
Loris

Reputation: 11

Maybe this helps:

When the user approves the payment, PayPal redirects the user to the return_url that was specified when the payment was created. A payer Id and payment Id are appended to the return URL, as PayerID and paymentId:

http://return_url?paymentId=PAY-6RV70583SB702805EKEYSZ6Y&token=EC-60U79048BN7719609&PayerID=7E7MGXCWTTKK2

From https://developer.paypal.com/webapps/developer/docs/integration/web/accept-paypal-payment/

Upvotes: 1

siddick
siddick

Reputation: 1478

After creating the payment.create(), You have to get payment.id and save it in user session.

And redirect the user to approve url. Use below code to get the approval url:

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

Use the payment.id from user session and PayerID from return_url to execute the payment.

Samples:

Upvotes: 3

Related Questions