提交 fd6bbddb 编写于 作者: J Javen205

添加 PayPal 支付

上级 33aa2d29
......@@ -37,6 +37,11 @@
<artifactId>IJPay-QQ</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.github.javen205</groupId>
<artifactId>IJPay-PayPal</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
<build>
......
/**
* <p>IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
*
* <p>不依赖任何第三方 mvc 框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里。 </p>
*
* <p>IJPay 交流群: 723992875</p>
*
* <p>Node.js 版: https://gitee.com/javen205/TNWX</p>
*
* <p>PayPal 支付示例</p>
*
* @author Javen
*/
package com.ijpay.demo.controller.paypal;
import com.ijpay.core.kit.HttpKit;
import com.ijpay.demo.entity.PayPalBean;
import com.ijpay.paypal.PayPalApiConfig;
import com.ijpay.paypal.PayPalApiConfigKit;
import com.paypal.api.payments.*;
import com.paypal.base.rest.PayPalRESTException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/payPal")
public class PayPalController {
private Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
PayPalBean payPalBean;
private final static String RETURN_URL = "/payPal/return";
private final static String CANCEL_URL = "/payPal/cancel";
@RequestMapping("")
@ResponseBody
public String index() {
log.info("欢迎使用 IJPay 中的 PayPal 支付 -By Javen <br/><br> 交流群:723992875");
log.info(payPalBean.toString());
return ("欢迎使用 IJPay 中的 PayPal 支付 -By Javen <br/><br> 交流群:723992875");
}
@RequestMapping(value = "/toPay")
@ResponseBody
public void toPay(HttpServletResponse response) {
PayPalApiConfig config = PayPalApiConfig.builder()
.setClientId(payPalBean.getClientId())
.setClientSecret(payPalBean.getClientSecret())
.setMode(payPalBean.getMode())
.build();
// Set payer details
Payer payer = new Payer();
payer.setPaymentMethod("paypal");
// Set redirect URLs
RedirectUrls redirectUrls = new RedirectUrls();
redirectUrls.setCancelUrl(payPalBean.getDomain().concat(CANCEL_URL));
redirectUrls.setReturnUrl(payPalBean.getDomain().concat(RETURN_URL));
// Set payment details
Details details = new Details();
details.setShipping("1");
details.setSubtotal("5");
details.setTax("1");
// Payment amount
Amount amount = new Amount();
amount.setCurrency("USD");
// Total must be equal to sum of shipping, tax and subtotal.
amount.setTotal("7");
amount.setDetails(details);
// Transaction information
Transaction transaction = new Transaction();
transaction.setAmount(amount);
transaction.setDescription("IJPay 让支付触手可及 PayPal 支付");
// Add transaction to a list
List<Transaction> transactions = new ArrayList<Transaction>();
transactions.add(transaction);
// Add payment details
Payment payment = new Payment();
payment.setIntent("sale");
payment.setPayer(payer);
payment.setRedirectUrls(redirectUrls);
payment.setTransactions(transactions);
try {
Payment createdPayment = payment.create(config.getApiContext());
for (Links link : createdPayment.getLinks()) {
System.out.println(link);
if (link.getRel().equalsIgnoreCase("approval_url")) {
// Redirect the customer to link.getHref()
response.sendRedirect(link.getHref());
}
}
} catch (PayPalRESTException e) {
System.err.println(e.getDetails());
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping(value = "/return")
@ResponseBody
public void returnUrl(HttpServletRequest request, HttpServletResponse response) {
String paymentId = request.getParameter("paymentId");
String payerId = request.getParameter("PayerID");
log.info("paymentId:" + paymentId);
log.info("payerId:" + payerId);
Payment payment = new Payment();
payment.setId(paymentId);
PaymentExecution paymentExecution = new PaymentExecution();
paymentExecution.setPayerId(payerId);
try {
Payment createdPayment = payment.execute(PayPalApiConfigKit.getPayPalApiConfig().getApiContext(),
paymentExecution);
System.out.println(createdPayment);
} catch (PayPalRESTException e) {
System.err.println(e.getDetails());
}
}
@RequestMapping(value = "/cancel")
@ResponseBody
public String cancelUrl(HttpServletRequest request, HttpServletResponse response) {
String readData = HttpKit.readData(request);
System.out.println(readData);
return readData;
}
}
package com.ijpay.demo.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* <p>IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
*
* <p>不依赖任何第三方 mvc 框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里。 </p>
*
* <p>IJPay 交流群: 723992875</p>
*
* <p>Node.js 版: https://gitee.com/javen205/TNWX</p>
*
* <p>PayPal配置 Bean</p>
*
* @author Javen
*/
@Component
@PropertySource("classpath:/paypal.properties")
@ConfigurationProperties(prefix = "paypal")
public class PayPalBean {
private String clientId;
private String clientSecret;
private String mode;
private String domain;
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public String getMode() {
return mode;
}
public void setMode(String mode) {
this.mode = mode;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
@Override
public String toString() {
return "PayPalBean{" +
"clientId='" + clientId + '\'' +
", clientSecret='" + clientSecret + '\'' +
", mode='" + mode + '\'' +
", domain='" + domain + '\'' +
'}';
}
}
\ No newline at end of file
paypal.clientId = clientId
paypal.clientSecret = clientSecret
paypal.mode = sandbox
paypal.domain = domain
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>IJPay</artifactId>
<groupId>com.github.javen205</groupId>
<version>2.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>IJPay-PayPal</artifactId>
<name>${project.artifactId}</name>
<description>IJPay 让支付触手可及(Easy Pay Library)-PayPal</description>
<url>https://github.com/javen205/IJPay</url>
<dependencies>
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>rest-api-sdk</artifactId>
<version>1.14.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/**
* <p>IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
*
* <p>不依赖任何第三方 mvc 框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里。 </p>
*
* <p>IJPay 交流群: 723992875</p>
*
* <p>Node.js 版: https://gitee.com/javen205/TNWX</p>
*
* <p>PayPal Api</p>
*
* @author Javen
*/
package com.ijpay.paypal;
public class PayPalApi {
}
/**
* <p>IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
*
* <p>不依赖任何第三方 mvc 框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里。 </p>
*
* <p>IJPay 交流群: 723992875</p>
*
* <p>Node.js 版: https://gitee.com/javen205/TNWX</p>
*
* <p>PayPal支付配置</p>
*
* @author Javen
*/
package com.ijpay.paypal;
import com.paypal.base.rest.APIContext;
import java.io.Serializable;
public class PayPalApiConfig implements Serializable {
private static final long serialVersionUID = -6012811778236113584L;
private String clientId;
private String clientSecret;
private String mode;
private APIContext apiContext;
private PayPalApiConfig() {
}
public static PayPalApiConfig builder() {
return new PayPalApiConfig();
}
public PayPalApiConfig build() {
this.apiContext = new APIContext(getClientId(), getClientSecret(),getMode());
return this;
}
public String getClientId() {
return clientId;
}
public PayPalApiConfig setClientId(String clientId) {
this.clientId = clientId;
return this;
}
public String getClientSecret() {
return clientSecret;
}
public PayPalApiConfig setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
return this;
}
public String getMode() {
return mode;
}
public PayPalApiConfig setMode(String mode) {
this.mode = mode;
return this;
}
public APIContext getApiContext() {
return apiContext;
}
public PayPalApiConfig setApiContext(APIContext apiContext) {
this.apiContext = apiContext;
return this;
}
}
package com.ijpay.paypal;
import cn.hutool.core.util.StrUtil;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* <p>IJPay 让支付触手可及,封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
*
* <p>不依赖任何第三方 mvc 框架,仅仅作为工具使用简单快速完成支付模块的开发,可轻松嵌入到任何系统里。 </p>
*
* <p>IJPay 交流群: 723992875</p>
*
* <p>Node.js 版: https://gitee.com/javen205/TNWX</p>
*
* @author Javen
*/
public class PayPalApiConfigKit {
private static final ThreadLocal<String> TL = new ThreadLocal<String>();
private static final Map<String, PayPalApiConfig> CFG_MAP = new ConcurrentHashMap<String, PayPalApiConfig>();
private static final String DEFAULT_CFG_KEY = "_default_key_";
/**
* <p>向缓存中设置 PayPalApiConfig </p>
* <p>每个 clientId 只需添加一次,相同 clientId 将被覆盖</p>
*
* @param payPalApiConfig PayPal 支付配置
* @return {@link PayPalApiConfig}
*/
public static PayPalApiConfig putApiConfig(PayPalApiConfig payPalApiConfig) {
if (CFG_MAP.size() == 0) {
CFG_MAP.put(DEFAULT_CFG_KEY, payPalApiConfig);
}
return CFG_MAP.put(payPalApiConfig.getClientId(), payPalApiConfig);
}
/**
* 向当前线程中设置 {@link PayPalApiConfig}
*
* @param payPalApiConfig {@link PayPalApiConfig} PayPal 支付配置
* @return {@link PayPalApiConfig}
*/
public static PayPalApiConfig setThreadLocalPayPalApiConfig(PayPalApiConfig payPalApiConfig) {
if (StrUtil.isNotEmpty(payPalApiConfig.getClientId())) {
setThreadLocalClientId(payPalApiConfig.getClientId());
}
return putApiConfig(payPalApiConfig);
}
/**
* 通过 PayPalApiConfig 移除支付配置
*
* @param payPalApiConfig {@link PayPalApiConfig} PayPal 支付配置
* @return {@link PayPalApiConfig}
*/
public static PayPalApiConfig removeApiConfig(PayPalApiConfig payPalApiConfig) {
return removeApiConfig(payPalApiConfig.getClientId());
}
/**
* 通过 clientId 移除支付配置
*
* @param clientId 应用编号
* @return {@link PayPalApiConfig}
*/
public static PayPalApiConfig removeApiConfig(String clientId) {
return CFG_MAP.remove(clientId);
}
/**
* 向当前线程中设置 clientId
*
* @param clientId 应用编号
*/
public static void setThreadLocalClientId(String clientId) {
if (StrUtil.isEmpty(clientId)) {
clientId = CFG_MAP.get(DEFAULT_CFG_KEY).getClientId();
}
TL.set(clientId);
}
/**
* 移除当前线程中的 clientId
*/
public static void removeThreadLocalClientId() {
TL.remove();
}
/**
* 获取当前线程中的 clientId
*
* @return 应用编号 clientId
*/
public static String getClientId() {
String clientId = TL.get();
if (StrUtil.isEmpty(clientId)) {
clientId = CFG_MAP.get(DEFAULT_CFG_KEY).getClientId();
}
return clientId;
}
/**
* 获取当前线程中的 PayPalApiConfig
*
* @return {@link PayPalApiConfig}
*/
public static PayPalApiConfig getPayPalApiConfig() {
String clientId = getClientId();
return getApiConfig(clientId);
}
/**
* 通过 clientId 获取 PayPalApiConfig
*
* @param clientId 应用编号
* @return {@link PayPalApiConfig}
*/
public static PayPalApiConfig getApiConfig(String clientId) {
PayPalApiConfig cfg = CFG_MAP.get(clientId);
if (cfg == null) {
throw new IllegalStateException("需事先调用 PayPalApiConfigKit.putApiConfig(payPalApiConfig) 将 clientId 对应的 payPalApiConfig 对象存入,才可以使用 PayPalApiConfigKit.getPayPalApiConfig() 的系列方法");
}
return cfg;
}
}
\ No newline at end of file
......@@ -20,6 +20,7 @@
<module>IJPay-QQ</module>
<module>IJPay-UnionPay</module>
<module>IJPay-JDPay</module>
<module>IJPay-PayPal</module>
</modules>
<licenses>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册