使用 python-paypalrestsdk 启动快速结账交易

Initiating an Express Checkout transaction with python-paypalrestsdk

问题

我正在尝试通过服务器端 python 启动快速结账交易(我正在使用 Flask)。我使用的是标准 paypal python SDK.

不幸的是,我不知道如何使用 python SDK 实际执行此操作。

paypal SDK源码中的examples好像没有相关的东西可以提供。


问题

如何在 python 中发起 SetExpressCheckout 调用以启动 Paypal Express Checkout 工作流程?

非常感谢,

Express Checkout API 是经典 API 的一部分。您在此处展示的 SDK 使用的是 REST API,这有点不同,因此它们对您帮助不大。

REST API documentation is here,在顶部右侧,您可以选择 Python 作为您将看到的示例代码。该文档应该更符合该 SDK 为您所做的事情。

REST API 支持 PayPal Express Checkout。 SetExpressCheckoutGetExpressCheckoutDetailsDoExpressCheckoutPayment (NVP API) 对应的操作是 createfindexecute (REST API).

from uuid import uuid4
from paypalrestsdk import Payment, WebProfile
from paypalrestsdk import Api as PaypalAPI

def SetExpressCheckout(client_id, client_secret, data, profile=None, sandbox=False):
    api = PaypalAPI({
            'mode': sandbox and 'sandbox' or 'live',
            'client_id': client_id,
            'client_secret': client_secret})
    if profile:
        profile['name'] = uuid4().hex
        profile['temporary'] = True
        webprofile = WebProfile(profile, api=api)
        if not webprofile.create():
            raise Exception(webprofile.error)
        data['experience_profile_id'] = webprofile.id

    payment = Payment(data, api=api)
    if not payment.create():
        raise Exception(payment.error)
    return payment

payment = SetExpressCheckout(
            client_id='...',
            client_secret='...',
            sandbox=True,
            profile={
                'presentation': {
                    'brand_name': 'My Shop',
                    'logo_image': 'https://www.shop.com/logo.png',
                    'locale_code': 'DE',
                    },
                'input_fields': {
                    'allow_note': False,
                    'no_shipping': 0,
                    'address_override': 0,
                    },
                'flow_config': {
                    'landing_page_type': 'Login',
                    },
                 },
             data={
                 'intent': 'sale',
                 'payer': {
                     'payment_method': 'paypal',
                     'payer_info': {
                         'email': 'buyer@email.com',
                         },
                      },
                  'note_to_payer': 'A note',
                  'redirect_urls': {
                      'return_url': 'https://www.shop.com/success.py',
                      'cancel_url': 'https://www.shop.com/canceled.py',
                      },
                  'transactions': [{
                      'notify_url': 'https://www.shop.com/paypal_notify.py',
                      'item_list': {
                          'items': [{
                              'name': 'Item name',
                              'description': 'Description',
                              'sku': 'SKU',
                              'price': '10.00',
                              'currency': 'EUR',
                              'quantity': 1,
                              }],
                          },
                      'amount': {
                          'total': '10.00',
                          'currency': 'EUR',
                          },
                      'description': 'Description',
                      'payment_options': {
                          'allowed_payment_method': 'INSTANT_FUNDING_SOURCE',
                          },
                      }],
                  },
             )           

for link in payment.links:
    if link.method == 'REDIRECT':
        redirect_url = link.href
        redirect_url += '&useraction=continue' #'&useraction=commit'
        break