spring-boot - Paypal REST API 返回 INVALID_CURRENCY_AMOUNT_FORMAT

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

response-code: 400 details: name: VALIDATION_ERROR message: Invalid request - see details details: [{ "field": "transactions.amount", "issue": "Cannot construct instance of com.paypal.platform.payments.model.rest.common.Amount, >problem: INVALID_CURRENCY_AMOUNT_FORMAT" }] debug-id: 86ad5783892c3 information-link: 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);
}



}

最佳答案

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

PayPal API 只接受以句点 (.) 作为小数分隔符的数字

关于spring-boot - Paypal REST API 返回 INVALID_CURRENCY_AMOUNT_FORMAT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61268706/

相关文章:

php - 如何测试定期计费 (Paypal Payments Pro)

php - 接收订单详情和付款确认(电子商务网站)

ios - 如何在iOS中实现Authorize.Net SDK

payment-gateway - 支付完成后注销Paypal Express账户

java - 如果存在我的自定义注释并且 RestTemplate 正在使用对象映射器,则不会调用 AnnotationIntrospector

PayPal preapproval,执行前验证付款

java - 限制调用 Spring Boot Rest 端点,直到完成从数据库的映射

iphone - 付款被拒绝。请在您的计算机上为此金额添加有效的付款方式。 (iOS 中的 PayPal 实时模式)

java - 使用 Spring Boot 编写 Spring Batch

java - Apache Camel RabbitMQ 从队列到队列