REST API は PayPal エクスプレス チェックアウトをサポートしています。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