如何通过REST SDK获取已执行贝宝付款的销售ID [英] How to get Sale Id of executed PayPal payment via REST SDK
问题描述
我正在使用Java的PayPal REST SDK: https://github.com/paypal/PayPal-Java-SDK
I am using PayPal's REST SDK for java: https://github.com/paypal/PayPal-Java-SDK
通过SDK执行付款后,如果有人选择退款,我希望通过SDK执行退款.
After executing a payment via the SDK, if someone should choose to refund a payment, I wish to execute the refund via the SDK.
首先,我使用Payment.get(APIContext, PaymentId)
方法获得付款的详细信息.
First, I get the details of the payment using the Payment.get(APIContext, PaymentId)
method.
然后,要执行退款,我需要销售编号.因此,我通过以下调用从上一步中检索到的Payment
对象中检索了此信息:
Then, to execute the refund I need the Sale Id. So I retrieve this from the Payment
Object retrieved in the previous step with the following call:
String saleId = ppPayment.getTransactions().get(0).getRelatedResources().get(0).getSale().getId();
上面的调用假定列表中有一个Transaction
对象,在Payment
和Transaction
对象中分别有一个RelatedResources
对象.
The call above assumes that there is one Transaction
Object in the list and one RelatedResources
Object in the Payment
and Transaction
Objects, respectively.
我的问题是:可以假设相关事务和相关资源对象始终是列表中的第一元素吗?
My question is: Is it safe to assume that the relevant Transaction and Related Resource Object will always be the first elements in the list?
对于Transaction
对象,我知道只有一个,因为我是创建付款的那个.对于RelatedResource
对象,我不确定如何知道列表中的哪个元素与我相关.
For the case of the Transaction
Object I know that there is only one because I am the one creating the payment. For the case of the RelatedResource
Object I'm not sure how I'm supposed to know which element in the list is relevant to me.
对于两个列表,有没有一种方法可以确保列表中的元素是与您相关的元素?在哪种情况下,返回列表中有多个RelatedResource
元素?
For both lists, is there a way to ensure that the element in the list is the one that is relevant to you? In what case is there more than one RelatedResource
element in the returned list?
谢谢
推荐答案
您可以执行以下操作.
String saleId = "";
try{
Payment payment = Payment.get(apiContext, paymentId);
List<Transaction> transactions = payment.getTransactions();
for(Transaction transaction : transactions){
List<RelatedResources> resources = transaction.getRelatedResources();
for(RelatedResources resource : resources){
Sale sale = resource.getSale();
if(sale != null){
saleId = sale.getId();
break;
}
}
}
System.out.println(saleId);
}catch(PayPalRESTException e){
System.err.println(e.getDetails());
}
我不确定交易对象,但是如果您已完成的付款(即销售)已退还,则RelatedResources对象在返回列表中将有多个元素.在这种情况下,您的付款对象的交易中的RelatedResources将具有多个元素,其中之一将是销售,其他将被退款.
I am not sure about Transaction Object, but RelatedResources Object will have more than one element in the returned list if your completed Payment(i.e. sale) is already refunded. In this case, RelatedResources in Transaction of your Payment Object will have multiple elements, one of which would be sale and other would be refund.
这篇关于如何通过REST SDK获取已执行贝宝付款的销售ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!