集成PayPal Smart Buttons数字商品收款遇422错误,求正确方案
Hey there, that 422 Unprocessable Entity error is almost always tied to a mismatch between your items total and the amount value you're sending—let's get this sorted for your digital goods setup.
Why the Error Happens
When you include an items array in your order creation request, PayPal requires that the top-level amount value exactly matches the sum of all items' unit amounts multiplied by their quantities. Even more importantly, you need to explicitly define this breakdown in the amount object so PayPal can validate the totals correctly. Without that breakdown, PayPal can't reconcile the items with the overall amount, hence the 422 error.
Correct Implementation for Digital Goods
You absolutely should keep the items parameter (it's best practice for digital goods/services, as it provides clear transaction details to both you and PayPal). Here's how to adjust your code to fix the error:
order = actions.order.create({ application_context: { locale : "ITA", shipping_preference:"NO_SHIPPING" // Perfect for digital goods—no delivery needed }, purchase_units: [{ description:"BLA BLA", items:[ { name: "BLA BLA", category:"DIGITAL_GOODS", quantity:"1", unit_amount :{ currency_code:"EUR", value : "5.00" } } ], amount: { currency_code: "EUR", value: "5.00", // Must match the sum of items (1 * 5.00) breakdown: { // Add this breakdown to link the amount to your items item_total: { currency_code: "EUR", value: "5.00" } } } }] });
Key Notes
- Always use string values for currency amounts (like
"5.00"instead of5.00) to avoid floating-point precision issues. - The
breakdown.item_totalmust equal the sum of allunit_amount.value * quantityfrom your items array. If you add more items later, update this value accordingly. - Keeping
shipping_preference:"NO_SHIPPING"is the right call for digital goods/services—this skips any shipping-related steps in the checkout flow, which improves user experience.
Removing the items parameter works as a quick fix, but it's not ideal because you lose transaction transparency and may run into issues with PayPal's fraud detection or reporting down the line. The adjusted code above is the compliant, recommended approach for your use case.
内容的提问来源于stack exchange,提问作者Filippo

