PHP版PayPal REST API创建支付时多Transaction报错问题咨询
Alright, let's break this down clearly for you:
Does a Single Payment Support Multiple Transactions?
First, the core answer: No, the PayPal REST API does not allow multiple Transaction objects in a single Payment—even though the PHP SDK's setTransactions() method accepts an array (which is why your code doesn't throw a local syntax error). This is a hard constraint of PayPal's REST API design; each payment resource is intended to map to one transaction only.
Why You're Getting the 400 Error
Your 400 HTTP response is almost certainly caused by trying to include two transactions in one payment. That said, let's cover other potential triggers for this error in case you run into similar issues later:
- Invalid Amount Structure: Double-check that
$amount1and$amount2are properly constructedAmountobjects, not just raw numbers. They need a currency code and formatted total value, like:$amount1 = new Amount(); $amount1->setCurrency('USD') ->setTotal('15.99'); - Item List vs. Amount Mismatch: PayPal validates that the sum of items in an
itemListexactly matches the transaction's total amount. If either of your transactions has a mismatch here, it could also trigger a 400 error. - Missing Required Fields: Ensure every required field (like description, currency code, etc.) is set for each transaction. A missing mandatory field can also result in a bad request.
How to Handle Multiple Charges
If you need to process separate charges, you have two valid approaches:
- Create Separate Payments: Build a distinct
Paymentobject for each transaction, each with its own singleTransactioninstance. - Combine into One Transaction: If the charges are part of the same customer session, merge all items into a single
itemListand use oneTransactionwith a total amount that sums all individual line items.
内容的提问来源于stack exchange,提问作者LarAng

