【问题标题】:Initiating an Express Checkout transaction with python-paypalrestsdk使用 python-paypalrestsdk 启动 Express Checkout 交易
【发布时间】:2015-05-15 10:27:20
【问题描述】:

问题

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

  • 我正在关注文档中的 Express Checkout 工作流程。 PayPal documentation for Express Checkout 不是很有帮助,因为它不包含任何 python 示例。

  • 根据文档,Express Checkout 交易的第一个服务器端步骤是向 PayPal API 发出 SetExpressCheckout 调用。

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

paypal SDK 源代码中的examples 似乎没有提供任何相关内容。


问题

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

非常感谢,

【问题讨论】:

    标签: python paypal paypal-rest-sdk


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。我知道 REST 与 CLassic API,但无法找到 Express Checkout 的 REST 文档。我认为现在 REST API 实际上可能不支持 Express Checkout。如果你能确认这一点,那就回答了我的问题。
    • 支持相同的功能,但据我所知,他们只是将其称为“PayPal Payments”而不是 Express Checkout。
    • 我认为 PayPal Payments 是常规(非 Express)流程。这肯定令人困惑。我只是改用经典的非 REST API,它似乎工作正常,尽管我必须编写自己的 API 客户端。谢谢!
    • 这也是我一直坚持的。 REST 太新了,他们试图将其投入生产以与 Stripe 竞争。在我看来,经典要好得多。
    • 完全同意。如果您想修改答案以说明 REST 目前不支持 Express Checkout,我会接受。如果你觉得懒惰,我会把它写成一个单独的答案并接受它....lmk 不管怎样。
    【解决方案2】:

    REST API 确实支持 PayPal Express Checkout。 SetExpressCheckout、GetExpressCheckoutDetails和DoExpressCheckoutPayment(NVP API)对应的动作是create、find和execute(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
    

    【讨论】:

      猜你喜欢
      • 2013-03-15
      • 2012-11-14
      • 2012-08-22
      • 2020-08-06
      • 2014-10-27
      • 2018-01-05
      • 2021-01-21
      • 2022-01-05
      • 2015-03-28
      相关资源
      最近更新 更多