import com.paypal.api.payments.*; import com.paypal.api.payments.Patch; import com.paypal.base.rest.APIContext; import com.paypal.base.rest.PayPalRESTException; import com.paypal.core.PayPalEnvironment; import com.paypal.core.PayPalHttpClient; import com.paypal.http.HttpResponse; import com.paypal.http.exceptions.SerializeException; import com.paypal.http.serializer.Json; import com.paypal.orders.*; import com.paypal.orders.Capture; import com.paypal.orders.Order; import com.paypal.orders.Payer; import com.paypal.orders.Refund; import com.paypal.payments.CapturesRefundRequest; import com.paypal.payments.RefundRequest; import com.paypal.payments.RefundsGetRequest; import lombok.extern.slf4j.Slf4j; import org.json.JSONException; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.DelayQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * PayPal 海外支付 * * @author YSM * @user Admin * @data 2021/8/25 */ @Slf4j public class PalPayTemplate { private PalPayProperties payPalProperties; public PalPayTemplate(PalPayProperties payPalProperties) { this.payPalProperties = payPalProperties; } /** * 获取支付服务 * * @return */ public PayPalHttpClient client() { PayPalEnvironment environment = !payPalProperties.isSandbox() ? new PayPalEnvironment.Live(payPalProperties.getClientID(), payPalProperties.getClientSecret()) : new PayPalEnvironment.Sandbox(payPalProperties.getClientID(), payPalProperties.getClientSecret()); return new PayPalHttpClient(environment); } /** * 生成订单主体信息 * * @param customId 交易号 * @param invoiceId 订单号 * @param totalAmount 0.05 * @return */ private OrderRequest createRequestBody(String customId, String invoiceId, String totalAmount) { OrderRequest orderRequest = new OrderRequest(); orderRequest.checkoutPaymentIntent(PalPayProperties.CAPTURE); ApplicationContext applicationContext = new ApplicationContext() .brandName(PalPayProperties.BRANDNAME) .landingPage(PalPayProperties.LANDINGPAGE) .cancelUrl("https://www.yogayuren.com/").returnUrl(payPalProperties.getReturnUrl()) .userAction(PalPayProperties.USERACTION) .shippingPreference(PalPayProperties.SHIPPINGPREFERENCE); applicationContext.returnUrl(payPalProperties.getReturnUrl()); applicationContext.cancelUrl("https://www.baidu.com/"); //取消回调 orderRequest.applicationContext(applicationContext); List<PurchaseUnitRequest> purchaseUnitRequests = new ArrayList<PurchaseUnitRequest>(); @SuppressWarnings("serial") PurchaseUnitRequest purchaseUnitRequest = new PurchaseUnitRequest() .description("商品名字") .customId(customId) .invoiceId(invoiceId) .amountWithBreakdown(new AmountWithBreakdown() .currencyCode(currency()) //货币 .value(totalAmount)) .shippingDetail(new ShippingDetail() .name(new Name().fullName("RATTA")) .addressPortable(new AddressPortable() .addressLine1("梅陇镇") .addressLine2("集心路168号") .adminArea2("闵行区") .adminArea1("上海市") .postalCode("20000") .countryCode("CN"))); purchaseUnitRequests.add(purchaseUnitRequest); orderRequest.purchaseUnits(purchaseUnitRequests); return orderRequest; } @Autowired private PalConsumer consumer; @Value("${spring.profiles.active}") private String model; /** * 获取交易货币类型 * @return */ public String currency(){ if ("en".equals(model)){ return "USD"; } else if ("jp".equals(model)){ return "JPY"; } return "HKD"; } /** * 创建订单的方法 * * @param customId * @param invoiceId * @param totalAmount 金额 0.04 * @return 收银台地址 * @throws IOException */ public String createOrder(String customId, String invoiceId, String totalAmount) { OrdersCreateRequest request = new OrdersCreateRequest(); request.header("prefer", "return=representation"); request.requestBody(createRequestBody(customId, invoiceId, totalAmount)); HttpResponse<Order> response = null; try { response = client().execute(request); } catch (IOException e1) { try { log.error("第1次调用paypal订单创建失败"); response = client().execute(request); } catch (Exception e) { try { log.error("第2次调用paypal订单创建失败"); response = client().execute(request); } catch (Exception e2) { log.error("第3次调用paypal订单创建失败,失败原因:{}", e2.getMessage()); } } } String approve = ""; if (response.statusCode() == 201) { log.info("Status Code = {}, Status = {}, OrderID = {}, Intent = {}", response.statusCode(), response.result().status(), response.result().id(), response.result().checkoutPaymentIntent()); for (com.paypal.orders.LinkDescription link : response.result().links()) { log.info("Links-{}: {} \tCall Type: {}", link.rel(), link.href(), link.method()); if (link.rel().equals("approve")) { approve = link.href(); } } String totalAmountRe = response.result().purchaseUnits().get(0).amountWithBreak
JAVA集成PalPay支付
最新推荐文章于 2023-10-24 15:09:36 发布
本文详细介绍了如何在Java后端环境中集成PayPal支付系统,包括配置关键步骤,并探讨了如何利用阻塞队列来处理订单消息,确保在并发环境下实现订单操作的幂等性。
摘要由CSDN通过智能技术生成