PayPal Order PATCH请求返回Bad Request问题求助
PayPal订单更新(PATCH)接口Bad Request问题解决
核心错误原因
你的请求体格式不符合PayPal Orders PATCH接口要求:
- PayPal的PATCH接口要求请求体是JSON数组,而非单个JSON对象
- 需确保请求头的
Content-Type设置为application/json-patch+json
正确的请求示例
请求URL
PATCH https://api-m.sandbox.paypal.com/v2/checkout/orders/{{order_Id}}
请求头
Content-Type: application/json-patch+jsonAuthorization: Bearer {你的访问令牌}
请求体(数组格式)
[ { "op": "add", "path": "/purchase_units/@reference_id=='default'/invoice_id", "value": "123456" } ]
SpringBoot代码示例(RestTemplate)
import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class PayPalOrderUpdate { public static void main(String[] args) { String orderId = "你的订单ID"; String accessToken = "你的PayPal访问令牌"; String url = "https://api-m.sandbox.paypal.com/v2/checkout/orders/" + orderId; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Content-Type", "application/json-patch+json"); headers.set("Authorization", "Bearer " + accessToken); // 构造PATCH操作的数组 List<Map<String, Object>> patchOperations = new ArrayList<>(); Map<String, Object> operation = new HashMap<>(); operation.put("op", "add"); operation.put("path", "/purchase_units/@reference_id=='default'/invoice_id"); operation.put("value", "123456"); patchOperations.add(operation); HttpEntity<List<Map<String, Object>>> request = new HttpEntity<>(patchOperations, headers); ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PATCH, request, String.class); System.out.println("响应状态码:" + response.getStatusCode()); System.out.println("响应体:" + response.getBody()); } }
额外检查点
- 确认订单状态允许更新:只有
CREATED或APPROVED状态的订单才能被修改 - 检查
reference_id是否正确:如果创建订单时自定义了reference_id,需替换路径中的'default'为实际值
内容的提问来源于stack exchange,提问作者Lakshmi Pravallika
相关产品推荐
相关产品推荐

