解决Apex调用Stripe实现灵活付款条款时amounts_due参数错误问题
问题:Salesforce Apex调用Stripe API创建灵活付款条款发票时提示未知参数
amounts_due 我正尝试在Salesforce中实现灵活付款条款(Flexible Payment Terms),通过Apex发起REST调用请求Stripe服务。参考官方文档实现,按照要求传递amounts_due参数创建带付款条款的发票,但收到如下错误:
{ "error": { "code": "parameter_unknown", "doc_url": "https://stripe.com/docs/error-codes/parameter-unknown", "message": "Received unknown parameter: amounts_due", "param": "amounts_due", "request_log_url": "https://dashboard.stripe.com/test/logs/req_dsdMpC36mua7TP?t=1745322267", "type": "invalid_request_error" } }
我的Apex代码如下:
public static void createInvoiceWithInstallments(String apiKey,String customerId) { HttpRequest req = new HttpRequest(); req.setEndpoint('https://api.stripe.com/v1/invoices'); req.setMethod('POST'); req.setHeader('Authorization', 'Bearer ' + apiKey); req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); String body = 'customer=' + EncodingUtil.urlEncode(customerId, 'UTF-8') + '&collection_method=send_invoice' + '&auto_advance=true' + '&amounts_due[][amount]=2000' + '&amounts_due[][days_until_due]=30' + '&amounts_due[][description]=' + EncodingUtil.urlEncode('Payment 1', 'UTF-8') + '&amounts_due[][amount]=2000' + '&amounts_due[][days_until_due]=60' + '&amounts_due[][description]=' + EncodingUtil.urlEncode('Payment 2', 'UTF-8') + '&amounts_due[][amount]=2000' + '&amounts_due[][days_until_due]=90' + '&amounts_due[][description]=' + EncodingUtil.urlEncode('Payment 3', 'UTF-8'); req.setBody(body); Http http = new Http(); HttpResponse res = http.send(req); system.debug('req --> '+ req); if (res.getStatusCode() == 200 || res.getStatusCode() == 201) { System.debug('Stripe Invoice Created: ' + res.getBody()); } else { System.debug('Stripe Invoice Creation Failed: ' + res.getBody()); throw new StripeValidationException('Failed to create invoice. Code: ' + res.getStatusCode() + ' Body: ' + res.getBody()); } }
我尝试过多种方式传递amounts_due参数,但均未解决问题,预期是通过Apex调用在Stripe中创建带付款计划的发票,实际收到未知参数错误,求解决方案。
解决方案
- 问题根源:Stripe的灵活付款条款并非直接通过
amounts_due参数传入发票创建接口,正确的参数是payment_schedule,付款阶段明细需要包裹在payment_schedule[phases][]结构下。 - 修改后的Apex代码:
public static void createInvoiceWithInstallments(String apiKey,String customerId) { HttpRequest req = new HttpRequest(); req.setEndpoint('https://api.stripe.com/v1/invoices'); req.setMethod('POST'); req.setHeader('Authorization', 'Bearer ' + apiKey); req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); // 使用正确的payment_schedule参数结构传递付款阶段 String body = 'customer=' + EncodingUtil.urlEncode(customerId, 'UTF-8') + '&collection_method=send_invoice' + '&auto_advance=true' + // 第一阶段付款 '&payment_schedule[phases][][amount]=2000' + '&payment_schedule[phases][][days_until_due]=30' + '&payment_schedule[phases][][description]=' + EncodingUtil.urlEncode('Payment 1', 'UTF-8') + // 第二阶段付款 '&payment_schedule[phases][][amount]=2000' + '&payment_schedule[phases][][days_until_due]=60' + '&payment_schedule[phases][][description]=' + EncodingUtil.urlEncode('Payment 2', 'UTF-8') + // 第三阶段付款 '&payment_schedule[phases][][amount]=2000' + '&payment_schedule[phases][][days_until_due]=90' + '&payment_schedule[phases][][description]=' + EncodingUtil.urlEncode('Payment 3', 'UTF-8'); req.setBody(body); Http http = new Http(); HttpResponse res = http.send(req); system.debug('req --> '+ req); if (res.getStatusCode() == 200 || res.getStatusCode() == 201) { System.debug('Stripe Invoice Created: ' + res.getBody()); } else { System.debug('Stripe Invoice Creation Failed: ' + res.getBody()); throw new StripeValidationException('Failed to create invoice. Code: ' + res.getStatusCode() + ' Body: ' + res.getBody()); } }
- 额外注意事项:
- 确保你的Stripe账号已开通Invoice Payment Plans功能,否则参数正确也会报错。
- 验证所有付款阶段的金额总和与发票总金额匹配,避免金额不一致导致的验证错误。
- 确保所有含特殊字符的字段都完成了URL编码。
内容的提问来源于stack exchange,提问作者Laxmi Kanta Kar
相关产品推荐
相关产品推荐

