【问题标题】:How can I send variables to paypal api and get them back with python SDK?如何将变量发送到 paypal api 并使用 python SDK 取回它们?
【发布时间】:2019-06-14 02:22:14
【问题描述】:

我正在与卖家和买家一起制作一个市场类型的应用程序,并尝试将 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>

【问题讨论】:

    标签: python django paypal


    【解决方案1】:

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

    您可以使用它,或者在description 上提出一些字符串格式并寻找它。

    【讨论】:

    • 谢谢 :) 我会给description 一个字符串格式,这看起来是完成此任务的好方法
    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    相关资源
    最近更新 更多