danny bee
danny bee

Reputation: 870

How can I send variables to paypal api and get them back with python SDK?

I am making a marketplace-type app with sellers and buyers and am trying to integrate PayPal API as a means to pay between users.

I need to be able to send the seller's username(on my website) as a parameter to PayPal API and get it back after a successful payment so I can notify the seller he has been paid. How can this be accomplished?

from paypalrestsdk import Payment
from django.http import HttpResponseRedirect
def payment_page(request):

    if request.method == 'POST':
        approval_url = 'http://127.0.0.1:8000/'
        paypalrestsdk.configure({
          "mode": "sandbox", # sandbox or live
          "client_id": "client_id",
          "client_secret": "client_secret"})

        payment = paypalrestsdk.Payment({
            "intent": "sale",
            "payer": {
                "payment_method": "paypal"},
            "redirect_urls": {
                "return_url": "http://localhost:8000/success",
                "cancel_url": "http://localhost:8000/fail"},
            "transactions": [{
                "item_list": {
                    "items": [{
                        "name": "item",
                        "sku": "item",
                        "price": "5.00",
                        "currency": "USD",
                        "quantity": 1}]},
                "amount": {
                    "total": "5.00",
                    "currency": "USD"},
                "description": "This is the payment transaction description."}]})

        if payment.create():
          print("Payment created successfully")

          for link in payment.links:
             if link.rel == "approval_url":
                 # Convert to str to avoid Google App Engine Unicode issue
                 # https://github.com/paypal/rest-api-sdk-python/pull/58
                 approval_url = str(link.href)
                 print("Redirect for approval: %s" % (approval_url))
                 return HttpResponseRedirect(approval_url)               
        else:
          print(payment.error)
    else:
        print('loading page')
    return render(request, 'app/payment.html')


def success(request):
    //Here I also want to capture seller username and buyer username

    payment_id = request.GET.get('paymentId')
    payer_id = request.GET.get('PayerID')
    # Payment ID obtained when creating the payment (following redirect)
    payment = Payment.find(payment_id)

    # Execute payment with the payer ID from the create payment call (following redirect)
    if payment.execute({"payer_id": payer_id}):
        print("Payment[%s] execute successfully" % (payment.id))
    else:
        print(payment.error)

    return render(request, 'app/success.html')

my payment.html payment template

<html>
  <head>

  </head>


  <body>
    <h3>Seller username:foo1 Buyer username:foo2</h3>

    <form action='{% url "app:payment_page" %}' method='post'>
    {% csrf_token %}
    <input type='submit' value='pay'>

    </form>
  </body>




</html>

Upvotes: 1

Views: 422

Answers (1)

M&#225;t&#233;
M&#225;t&#233;

Reputation: 2345

I'm assuming you're using the PayPal REST API. Under the transaction object there is a field called note_to_payee which is returned on the response of the payment lookup.

You can use that, or come up with some string format on the description and look for that.

Upvotes: 1

Related Questions