【问题标题】:PayPal REST API returns INVALID_CURRENCY_AMOUNT_FORMATPayPal REST API 返回 INVALID_CURRENCY_AMOUNT_FORMAT
【发布时间】:2020-07-30 18:46:48
【问题描述】:

响应代码:400 详细信息:名称:VALIDATION_ERROR 消息:无效请求 - 查看详细信息详细信息:[{ "field": "transactions.amount", “问题”:“无法构造 com.paypal.platform.payments.model.rest.common.Amount 的实例,>问题:INVALID_CURRENCY_AMOUNT_FORMAT” }] 调试ID:86ad5783892c3 信息链接:https://developer.paypal.com/docs/api/payments/#errors

package com.spring.soap.api;


@Configuration
public class PaypalConfig {

@Value("${paypal.client.id}")
   private String clientId;
@Value("${paypal.client.secret}")
   private String clientSecret;
@Value("${paypal.mode}")
   private String mode;
@Bean
public Map<String,String> paypalSdkConfig(){
    Map<String,String> configMap= new HashMap<>();
    configMap.put("mode",mode);
    return configMap;
    }
@Bean
public OAuthTokenCredential oAuthTokenCredential() {
    return new OAuthTokenCredential(clientId,clientSecret,paypalSdkConfig());

}

@Bean
public APIContext apiContext() throws PayPalRESTException {
    APIContext context = new APIContext(oAuthTokenCredential().getAccessToken());
    context.setConfigurationMap(paypalSdkConfig());
    return context;
}


}

{
@Autowired
PaypalService service;



public static final String SUCCESS_URL = "pay/success";
public static final String CANCEL_URL = "pay/cancel";

@GetMapping("/")
public  String home() {
    return "home";
}

@PostMapping("/pay")
public String payment(@ModelAttribute("order") Order order) {
    try {
        Payment payment = service.createPayment(order.getPrice(), order.getCurrency(), order.getMethod(),
                order.getIntent(), order.getDescription(), "http://localhost:9090/" + CANCEL_URL,
                "http://localhost:9090/" + SUCCESS_URL);
        for(Links link:payment.getLinks()) {
            if(link.getRel().equals("approval_url")) {
                return "redirect:"+link.getHref();
            }
        }

    } catch (PayPalRESTException e) {

        e.printStackTrace();
    }
    return "redirect:/";
}
@GetMapping(value = CANCEL_URL)
public String cancelPay() {
    return "cancel";
}

@GetMapping(value = SUCCESS_URL)
public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId) {
    try {
        Payment payment = service.executePayment(paymentId, payerId);
        System.out.println(payment.toJSON());
        if (payment.getState().equals("approved")) {
            return "success";
        }
    } catch (PayPalRESTException e) {
     System.out.println(e.getMessage());
    }
    return "redirect:/";
}


}

{
@Autowired
private APIContext apiContext;

public Payment createPayment(
        Double total, 
        String currency, 
        String method,
        String intent,
        String description, 
        String cancelUrl, 
        String successUrl) throws PayPalRESTException{
    Amount amount = new Amount();
    amount.setCurrency(currency);
    total = new BigDecimal(total).setScale(2, RoundingMode.HALF_UP).doubleValue();
    amount.setTotal(String.format("%.2f", total));

    Transaction transaction = new Transaction();
    transaction.setDescription(description);
    transaction.setAmount(amount);

    List<Transaction> transactions = new ArrayList<>();
    transactions.add(transaction);

    Payer payer = new Payer();
    payer.setPaymentMethod(method);

    Payment payment = new Payment();
    payment.setIntent(intent);
    payment.setPayer(payer);  
    payment.setTransactions(transactions);
    RedirectUrls redirectUrls = new RedirectUrls();
    redirectUrls.setCancelUrl(cancelUrl);
    redirectUrls.setReturnUrl(successUrl);
    payment.setRedirectUrls(redirectUrls);

    return payment.create(apiContext);
}

public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException{
    Payment payment = new Payment();
    payment.setId(paymentId);
    PaymentExecution paymentExecute = new PaymentExecution();
    paymentExecute.setPayerId(payerId);
    return payment.execute(apiContext, paymentExecute);
}



}

【问题讨论】:

  • 您的问题不包括您尝试在金额字段中设置的字符串的示例值。

标签: spring-boot paypal payment-gateway paypal-sandbox


【解决方案1】:

您的语言环境似乎正在使用逗号 (,) 作为小数分隔符来格式化小数。

PayPal API 仅接受带有句点 (.) 作为小数分隔符的数字

【讨论】:

  • 谢谢你是真的
【解决方案2】:

走这条线:

amount.setTotal(String.format("%.2f", total));

%.2f 更改为%.3f。最终代码应如下所示:

amount.setTotal(String.format("%.3f", total));

【讨论】:

    【解决方案3】:

    在我的例子中,我发送的是带有非四舍五入值的细节小计:

    141.750
    

    所以我只是像这样对值进行四舍五入:

    details.setSubtotal(subTotal.setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());
    

    (换句话说)

    141.75
    

    【讨论】:

      猜你喜欢
      • 2015-04-25
      • 2013-06-26
      • 2016-03-18
      • 2015-07-18
      • 1970-01-01
      • 2013-03-20
      • 2013-12-28
      • 2013-03-17
      • 2017-07-06
      相关资源
      最近更新 更多