英文:
How to get Order ID of a subscription payment from a Transaction from paypal API
问题
以下是代码部分的翻译:
I have a problem connecting subscription payments in paypal with their respective orders.
我在将PayPal的订阅付款与相应的订单连接方面遇到问题。
In a nutshell, the initial payment for the subscription is made on an order.
简而言之,订阅的初始付款是在订单上完成的。
Once the user pays, the order is approved and I get this information from the paypal API:
一旦用户付款,订单就会被批准,然后我可以从PayPal API中获取以下信息:
Right off the bat the data is incomplete, missing crucial information such as fees and transaction details. So the next best thing is to get the data on the webhook.
一开始数据是不完整的,缺少重要信息,比如费用和交易详情。因此,下一个最好的方法是从Webhook获取数据。
When the payment goes through, an event is triggered "PAYMENT.SALE.COMPLETED," which sends this info to my server:
当付款成功时,会触发一个事件“PAYMENT.SALE.COMPLETED”,该事件将此信息发送到我的服务器:
From the billing agreement ID (which is the subscription ID), I can get the transactions, based on a time period:
通过结算协议ID(即订阅ID),我可以根据时间段获取交易信息:
Now, why it requires start and end date even though the filter is the agreement ID, I couldn't tell you, however here I can see the fees, and since the event "PAYMENT.SALE.COMPLETED" has been triggered, I can be sure that the payment has gone through.
现在,为什么它要求开始和结束日期,即使过滤器是协议ID,我无法告诉你,但在这里我可以看到费用,而且由于触发了事件“PAYMENT.SALE.COMPLETED”,我可以确定付款已经完成。
All that's left is for me to connect the transaction with the order. But how? There is no direct connection between the order and the transaction, no ID specified, and nothing shown in the approved order. How can I connect the transaction ID with the Order ID so I can have a proper confirmation and extract the fees?
现在剩下的就是将交易与订单连接起来。但是如何连接呢?订单和交易之间没有直接的连接,没有指定的ID,而在已批准的订单中也没有显示。如何将交易ID与订单ID连接起来,以便进行正确的确认和提取费用?
I've tried direct capture, pulling the data from the API, and setting up a webhook for all payment and subscription events, yet nothing I've seen provides the required information.
我尝试了直接捕获,从API中提取数据,并为所有付款和订阅事件设置Webhook,但是我没有看到任何提供所需信息的内容。
SOLUTION:
解决方案:
So for any future developer that stumbles on this problem, here is my advice and solution.
因此,对于未来遇到这个问题的开发人员,这是我的建议和解决方案。
Make sure that you conform to the Paypal API and create reference records with their Subscription ID and transaction ID.
确保您符合PayPal API并创建具有其订阅ID和交易ID的参考记录。
Now the tricky part is getting the transaction right away, so the user doesn't have to wait too long for a confirmation. The way I've done this is after the subscription is created and the order approved, I send the relevant data to the server and using the Subscription ID, I keep sending requests to get all transactions for it using this endpoint: [PayPal API链接]
现在棘手的部分是立即获取交易,以便用户不必等待太长时间以获得确认。我采取的方法是在创建订阅并批准订单后,将相关数据发送到服务器,并使用订阅ID,我不断发送请求以获取所有与之相关的交易,使用以下终端:[PayPal API链接]
I do this request with a while(){}
cycle, for a maximum of 5 attempts with a 2-second sleep period between requests, since the transaction is not shown for the subscription right away.
我使用while(){}
循环执行此请求,最多尝试5次,每次请求之间间隔2秒,因为交易不会立即显示在订阅中。
After I get it, I create the reference record, and if the subscription transaction has the proper status, I save it as finished.
在获取它之后,我创建参考记录,如果订阅交易具有正确的状态,我将其保存为已完成。
On the webhook, when receiving the "PAYMENT.SALE.COMPLETED" event, you need to check if the transaction reference is already created and if not, create it. This will serve you for any future payments (since it's a subscription).
在Webhook上,当接收到“PAYMENT.SALE.COMPLETED”事件时,您需要检查交易参考是否已经创建,如果没有,创建它。这将为您提供未来的任何付款(因为它是一个订阅)。
希望这对您有所帮助。
英文:
I have a problem connecting subscription payments in paypal with their respective orders.
In a nutshell, the initial payment for the subscription is made on an order.
Once the user pays, the order is approved and I get this information from the paypal API:
{"id":"2M9235603X788581X","intent":"CAPTURE","status":"APPROVED","payment_source":{"paypal":{"email_address":"email_address","account_id":"8XHXZUT5Y3CVS","name":{"given_name":"John","surname":"Doe"},"address":{"country_code":"US"}}},"purchase_units":[],"payer":{"name":{"given_name":"John","surname":"Doe"},"email_address":"email_address","payer_id":"8XHXZUT5Y3CVS","address":{"country_code":"US"}},"create_time":"2023-02-13T17:24:46Z","links":[{"href":"https:\/\/api.sandbox.paypal.com\/v2\/checkout\/orders\/2M9235603X788581X","rel":"self","method":"GET"},{"href":"https:\/\/api.sandbox.paypal.com\/v2\/checkout\/orders\/2M9235603X788581X","rel":"update","method":"PATCH"},{"href":"https:\/\/api.sandbox.paypal.com\/v2\/checkout\/orders\/2M9235603X788581X\/capture","rel":"capture","method":"POST"}]}
Right of the bat the data is incomplete, missing crucial information such as fees and transaction details. So next best thing is to get the data on the webhook.
When the payment goes through an event is triggered "PAYMENT.SALE.COMPLETED", which sends this info to my server:
{"id":"WH-6SE66006R98946535-7F814879YL577135N","event_version":"1.0","create_time":"2023-02-13T17:25:09.906Z","resource_type":"sale","event_type":"PAYMENT.SALE.COMPLETED","summary":"Payment completed for EUR 39.37 EUR","resource":{"billing_agreement_id":"I-0HN4N0KTWLMP","amount":{"total":"39.37","currency":"EUR","details":{"subtotal":"39.37"}},"payment_mode":"INSTANT_TRANSFER","update_time":"2023-02-13T17:25:03Z","create_time":"2023-02-13T17:25:03Z","protection_eligibility_type":"ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE","transaction_fee":{"currency":"EUR","value":"1.76"},"protection_eligibility":"ELIGIBLE","links":[{"method":"GET","rel":"self","href":"https://api.sandbox.paypal.com/v1/payments/sale/036421861N8145017"},{"method":"POST","rel":"refund","href":"https://api.sandbox.paypal.com/v1/payments/sale/036421861N8145017/refund"}],"id":"036421861N8145017","state":"completed","invoice_number":""},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-6SE66006R98946535-7F814879YL577135N","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-6SE66006R98946535-7F814879YL577135N/resend","rel":"resend","method":"POST"}]}
From the billing agreement ID (which is the subscription ID) I can get the transactions, based on a time period:
{"transactions":[{"status":"COMPLETED","id":"036421861N8145017","amount_with_breakdown":{"gross_amount":{"currency_code":"EUR","value":"39.37"},"fee_amount":{"currency_code":"EUR","value":"1.76"},"net_amount":{"currency_code":"EUR","value":"37.61"}},"payer_name":{"given_name":"John","surname":"Doe"},"payer_email":"sb-wi93p1551674@personal.example.com","time":"2023-02-13T17:25:03.000Z"}],"links":[{"href":"https://api.sandbox.paypal.com/v1/billing/subscriptions/I-0HN4N0KTWLMP/transactions?start_time=2023-02-01T07%3A50%3A20.940Z&end_time=2023-02-28T07%3A50%3A20.940Z","rel":"SELF","method":"GET"}]}
Now why it requires start and end date even though the filter is the agreement ID, I couldnt tell you, however here I can see the fees and since the event "PAYMENT.SALE.COMPLETED" has been triggered I can be sure that the payment has went through.
All thats left is for me to connect the transaction with the order.
But how?
There is no direct connection between the order and the transaction, no ID specified and nothing shown in the approved order.
How can I connect the transaction ID with the Order ID so I can have a proper confirmation and extract the fees?
I`ve tried direct capture, pulling the data from the API and setting up webhook for all payment and subscription events, yet nothing I've seen provides the required information.
SOLUTION:
So for any future developer that stumbels on this problem here is my advice and solution.
Make sure that you conform to the Paypal API and create reference records with their Subscription ID and transaction ID.
Now the tricky part is getting the transaction right away, so the user doesnt have to wait to long for a confirmation.
The way I`ve done this is after the subscription is created and the order approved, I send the relevant data to the server and using the Subscription ID I keep sending requests to get all transactions for it using this endpoint: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_transactions
I do this request with a while(){}
cycle, for 5 max attempts with 2 seconds of sleep period between requests, since the transaction is not shown for the subscription right away.
After I get it I create the reference record and if the subscription transaction has the proper status, I save it as finished.
On the webhook, when receiving the "PAYMENT.SALE.COMPLETED" event, you need to check if the transaction reference is already created and if not, create it.
This will server you for any future payments (since its a subscription)
Hope this helps
答案1
得分: 0
在你的示例中,订阅 ID 是 I-0HN4N0KTWLMP
,第一笔付款(销售/扣款)的交易 ID 是 036421861N8145017
(我不知道订单 ID 2M9235603X788581X
是从哪里来的;PayPal 订阅不使用订单 ID,那些是一次性付款的)。
使用订阅 ID,你可以通过简单的 GET 调用获取其详细信息:https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get,无需日期范围。
有关实际付款的详细信息,如费用,可以使用以下任何一种支付 API 版本与该 ID 一起使用:
- V2 捕获获取:https://developer.paypal.com/docs/api/payments/v2/#captures_get
- 旧版 V1 销售获取:https://developer.paypal.com/docs/api/payments/v1/#sale_get
监听 Webhook 事件 PAYMENT.SALE.COMPLETED
是接收初始和未来付款的服务器端通知的正确通用解决方案。
如果你需要自己的“用户”或类似的 ID 进行对账,可以在创建订阅时设置 custom_id
。
英文:
In your example, the subscription ID is I-0HN4N0KTWLMP
and the transaction ID of the first payment (sale/capture) is 036421861N8145017
. (I don't know where the order ID 2M9235603X788581X
is coming from; PayPal subscriptions do not use Order IDs, those are for one-time payments)
With the subscription ID you can get its details with a simple GET call: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_get , no date ranges are needed.
For details on the actual payment, such as fees, either of the payments API versions may work with that ID:
- V2 capture get: https://developer.paypal.com/docs/api/payments/v2/#captures_get
- Older V1 sale get: https://developer.paypal.com/docs/api/payments/v1/#sale_get
Listening for the webhook event PAYMENT.SALE.COMPLETED
is the correct general solution for receiving a server-side notification of both the initial and future payments.
If you need your own "user" or similar ID for reconciliation, set custom_id
when creating the subscription.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论