如何在 Python 中获取来自 PayPal 的订单
How to capture an order from PayPal in Python
我一直在努力了解 PayPal SDK 的捕获过程究竟是如何工作的。我目前正在开发带有 PayPal Checkout 选项的 Python Kivy 移动应用程序。我一直在努力使这个示例在这里工作:https://github.com/paypal/Checkout-Python-SDK#capturing-an-order but 执行时出现此错误:
422
{'Cache-Control': 'max-age=0, no-cache, no-store, must-revalidate', 'Content-Length': '584', 'Content-Type': 'application/json', 'Date': 'Thu, 04 Mar 2021 18:32:56 GMT', 'Paypal-Debug-Id': 'd092a377ca029'}
{"name":"UNPROCESSABLE_ENTITY","details":[{"issue":"ORDER_NOT_APPROVED","description":"Payer has not yet approved the Order for payment. Please redirect the payer to the 'rel':'approve' url returned as part of the HATEOAS links within the Create Order call or provide a valid payment_source in the request."}],"message":"The requested action could not be performed, semantically incorrect, or failed business validation.","debug_id":"d092a377ca029","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-ORDER_NOT_APPROVED","rel":"information_link","method":"GET"}]}
据我了解,发生这种情况是因为我都尝试同时创建和捕获订单。我怎样才能使捕获仅在客户批准后才开始,这样我很可能不会看到此错误消息?感谢您的帮助!
.py
def PayPal(self):
client_id = "ID"
client_secret = "SECRET"
environment = SandboxEnvironment(client_id=client_id, client_secret=client_secret)
client = PayPalHttpClient(environment)
request = OrdersCreateRequest()
request.prefer("return=representation")
request.request_body({
"application_context": {
"return_url": ""},
"intent": "CAPTURE",
"purchase_units": [{
"amount": {
"currency_code": "CAD",
"value": str(App.get_running_app().cart)
}}]})
try:
response = client.execute(request)
print("Order With Complete Payload:")
print("Status Code:", response.status_code)
print("Status:", response.result.status)
print("Order ID:", response.result.id)
print("Intent:", response.result.intent)
print("Links:")
for link in response.result.links:
print('\t{}: {}\tCall Type: {}'.format(link.rel, link.href, link.method))
print("Total Amount: {} {}".format(response.result.purchase_units[0].amount.currency_code,
response.result.purchase_units[0].amount.value))
order = response.result
print(order)
except IOError as ioe:
print(ioe)
if isinstance(ioe, HttpError):
print(ioe.status_code)
webbrowser.open("https://www.sandbox.paypal.com/checkoutnow?token=" + response.result.id)
# Capture order
request = OrdersCaptureRequest(order_id= response.result.id)
try:
response = client.execute(request)
order = response.result.id
except IOError as ioe:
if isinstance(ioe, HttpError):
print(ioe.status_code)
print(ioe.headers)
print(ioe)
else:
print(ioe)
只有在客户通过审批流程(在 PayPal 上)并 returns 进入您的应用后,才能进行捕获。如果您在创建订单时指定 return_url
,这可以设置为 deeplink 回到您的应用程序,这应该是一个意图,然后调用函数,然后才进行捕获。
我一直在努力了解 PayPal SDK 的捕获过程究竟是如何工作的。我目前正在开发带有 PayPal Checkout 选项的 Python Kivy 移动应用程序。我一直在努力使这个示例在这里工作:https://github.com/paypal/Checkout-Python-SDK#capturing-an-order but 执行时出现此错误:
422
{'Cache-Control': 'max-age=0, no-cache, no-store, must-revalidate', 'Content-Length': '584', 'Content-Type': 'application/json', 'Date': 'Thu, 04 Mar 2021 18:32:56 GMT', 'Paypal-Debug-Id': 'd092a377ca029'}
{"name":"UNPROCESSABLE_ENTITY","details":[{"issue":"ORDER_NOT_APPROVED","description":"Payer has not yet approved the Order for payment. Please redirect the payer to the 'rel':'approve' url returned as part of the HATEOAS links within the Create Order call or provide a valid payment_source in the request."}],"message":"The requested action could not be performed, semantically incorrect, or failed business validation.","debug_id":"d092a377ca029","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-ORDER_NOT_APPROVED","rel":"information_link","method":"GET"}]}
据我了解,发生这种情况是因为我都尝试同时创建和捕获订单。我怎样才能使捕获仅在客户批准后才开始,这样我很可能不会看到此错误消息?感谢您的帮助!
.py
def PayPal(self):
client_id = "ID"
client_secret = "SECRET"
environment = SandboxEnvironment(client_id=client_id, client_secret=client_secret)
client = PayPalHttpClient(environment)
request = OrdersCreateRequest()
request.prefer("return=representation")
request.request_body({
"application_context": {
"return_url": ""},
"intent": "CAPTURE",
"purchase_units": [{
"amount": {
"currency_code": "CAD",
"value": str(App.get_running_app().cart)
}}]})
try:
response = client.execute(request)
print("Order With Complete Payload:")
print("Status Code:", response.status_code)
print("Status:", response.result.status)
print("Order ID:", response.result.id)
print("Intent:", response.result.intent)
print("Links:")
for link in response.result.links:
print('\t{}: {}\tCall Type: {}'.format(link.rel, link.href, link.method))
print("Total Amount: {} {}".format(response.result.purchase_units[0].amount.currency_code,
response.result.purchase_units[0].amount.value))
order = response.result
print(order)
except IOError as ioe:
print(ioe)
if isinstance(ioe, HttpError):
print(ioe.status_code)
webbrowser.open("https://www.sandbox.paypal.com/checkoutnow?token=" + response.result.id)
# Capture order
request = OrdersCaptureRequest(order_id= response.result.id)
try:
response = client.execute(request)
order = response.result.id
except IOError as ioe:
if isinstance(ioe, HttpError):
print(ioe.status_code)
print(ioe.headers)
print(ioe)
else:
print(ioe)
只有在客户通过审批流程(在 PayPal 上)并 returns 进入您的应用后,才能进行捕获。如果您在创建订单时指定 return_url
,这可以设置为 deeplink 回到您的应用程序,这应该是一个意图,然后调用函数,然后才进行捕获。