问题
I am using PayPal's REST SDK for java: https://github.com/paypal/PayPal-Java-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.
First, I get the details of the payment using the Payment.get(APIContext, PaymentId)
method.
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();
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?
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.
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?
Thanks
回答1:
You can do something like this.
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());
}
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.
来源:https://stackoverflow.com/questions/38606395/how-to-get-sale-id-of-executed-paypal-payment-via-rest-sdk