如何将变量发送到paypal api并使用python SDK返回变量?

时间:2019-01-20 14:17:48

标签: python django paypal

我正在与卖家和买家制作一个市场类型的应用程序,并且正在尝试将PayPal API集成为在用户之间进行付款的一种方式。

我需要能够将卖家的用户名(在我的网站上)作为参数发送到PayPal API,并在成功付款后将其取回,以便我可以通知卖家已付款。如何做到这一点?

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')

我的payment.html付款模板

<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>

1 个答案:

答案 0 :(得分:1)

我假设您正在使用PayPal REST API。在transaction object下有一个名为note_to_payee的字段,该字段在付款查询的响应中返回。

您可以使用它,也可以在description上使用一些字符串格式来查找。