mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-09 04:25:54 +08:00
增加PayPal支付demo
This commit is contained in:
@@ -47,6 +47,11 @@
|
||||
<artifactId>pay-java-fuiou</artifactId>
|
||||
<version>${pay.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.egzosn</groupId>
|
||||
<artifactId>pay-java-paypal</artifactId>
|
||||
<version>${pay.version}</version>
|
||||
</dependency>
|
||||
<!--/支付-->
|
||||
|
||||
<dependency>
|
||||
@@ -89,6 +94,11 @@
|
||||
<artifactId>pay-java-union</artifactId>
|
||||
<version>${pay.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.egzosn</groupId>
|
||||
<artifactId>pay-java-paypal</artifactId>
|
||||
<version>2.0.8-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<profiles>
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package com.egzosn.pay.demo.controller;
|
||||
|
||||
|
||||
import com.egzosn.pay.ali.bean.AliTransactionType;
|
||||
import com.egzosn.pay.common.api.PayService;
|
||||
import com.egzosn.pay.common.bean.CurType;
|
||||
import com.egzosn.pay.common.bean.MethodType;
|
||||
import com.egzosn.pay.common.bean.PayOrder;
|
||||
import com.egzosn.pay.common.bean.RefundOrder;
|
||||
import com.egzosn.pay.paypal.api.PayPalConfigStorage;
|
||||
import com.egzosn.pay.paypal.api.PayPalPayService;
|
||||
import com.egzosn.pay.paypal.bean.PayPalTransactionType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 发起支付入口
|
||||
*
|
||||
* @author: egan
|
||||
* @email egzosn@gmail.com
|
||||
* @date 2018/05/06 10:30
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("payPal")
|
||||
public class PayPalPayController {
|
||||
|
||||
|
||||
private PayService service = null;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
PayPalConfigStorage storage = new PayPalConfigStorage();
|
||||
storage.setClientID("AUWunqrIzeSLQTQqRp_gQwkt1vLRcVa5kJIO4mp0ZvQnTreLmxaji9bqOFpqz-0h8mdeQpYxix6g_PRD");
|
||||
storage.setClientSecret("EJFAZoD_ZG7PoRutRhX93TmeWs-DQ-PjdBkgr0j4GZA6lqAgmS2Z7yKQWqnTP5O74KqBaYOZnUEcq3Ug");
|
||||
storage.setTest(true);
|
||||
//发起付款后的页面转跳地址
|
||||
storage.setReturnUrl("http://127.0.0.1:8088/pay/success");
|
||||
//取消按钮转跳地址,这里用异步通知地址的兼容的做法
|
||||
storage.setNotifyUrl("http://127.0.0.1:8088/pay/cancel");
|
||||
service = new PayPalPayService(storage);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 跳到支付页面
|
||||
* 针对实时支付,即时付款
|
||||
*
|
||||
* @param price 金额
|
||||
* @return 跳到支付页面
|
||||
*/
|
||||
@RequestMapping(value = "toPay.html", produces = "text/html;charset=UTF-8")
|
||||
public String toPay(BigDecimal price) {
|
||||
//及时收款
|
||||
PayOrder order = new PayOrder("订单title", "摘要", null == price ? new BigDecimal(0.01) : price, UUID.randomUUID().toString().replace("-", ""), PayPalTransactionType.sale);
|
||||
|
||||
Map orderInfo = service.orderInfo(order);
|
||||
return service.buildRequest(orderInfo, MethodType.POST);
|
||||
}
|
||||
/**
|
||||
* 申请退款接口
|
||||
*
|
||||
* @return 返回支付方申请退款后的结果
|
||||
*/
|
||||
@RequestMapping("refund")
|
||||
public Map<String, Object> refund() {
|
||||
// TODO 这里需要 refundAmount, curType, description, tradeNo
|
||||
RefundOrder order = new RefundOrder();
|
||||
order.setCurType(CurType.USD);
|
||||
order.setDescription(" description ");
|
||||
order.setTradeNo("paypal 平台的单号");
|
||||
order.setRefundAmount(new BigDecimal(0.01));
|
||||
return service.refund(order);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* return url
|
||||
* PayPal确认付款调用的接口
|
||||
* 用户确认付款后,paypal调用的这个方法执行付款
|
||||
*
|
||||
* @return 付款成功信息
|
||||
*/
|
||||
@GetMapping(value = "payBack.json")
|
||||
public String executePayment(HttpServletRequest request) throws IOException {
|
||||
try (InputStream is = request.getInputStream()) {
|
||||
if (service.verify(service.getParameter2Map(request.getParameterMap(), is))) {
|
||||
// TODO 这里进行成功后的订单业务处理
|
||||
// TODO 返回成功付款页面,这个到时候再做一个漂亮的页面显示,并使用前后端分离的模式
|
||||
return "success";
|
||||
}
|
||||
}
|
||||
return "failure";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -14,6 +14,9 @@ import com.egzosn.pay.fuiou.bean.FuiouTransactionType;
|
||||
import com.egzosn.pay.payoneer.api.PayoneerConfigStorage;
|
||||
import com.egzosn.pay.payoneer.api.PayoneerPayService;
|
||||
import com.egzosn.pay.payoneer.bean.PayoneerTransactionType;
|
||||
import com.egzosn.pay.paypal.api.PayPalConfigStorage;
|
||||
import com.egzosn.pay.paypal.api.PayPalPayService;
|
||||
import com.egzosn.pay.paypal.bean.PayPalTransactionType;
|
||||
import com.egzosn.pay.union.api.UnionPayConfigStorage;
|
||||
import com.egzosn.pay.union.api.UnionPayService;
|
||||
import com.egzosn.pay.union.bean.UnionTransactionType;
|
||||
@@ -188,16 +191,24 @@ public enum PayType implements BasePayType {
|
||||
@Override
|
||||
public PayService getPayService(ApyAccount apyAccount) {
|
||||
PayoneerConfigStorage configStorage = new PayoneerConfigStorage();
|
||||
configStorage.setProgramId("商户id");
|
||||
//设置商户Id
|
||||
configStorage.setProgramId(apyAccount.getPartner());
|
||||
configStorage.setMsgType(MsgType.json);
|
||||
configStorage.setInputCharset("utf-8");
|
||||
//"PayoneerPay 用户名"
|
||||
configStorage.setUserName(apyAccount.getSeller());
|
||||
//PayoneerPay API password
|
||||
configStorage.setApiPassword(apyAccount.getPrivateKey());
|
||||
//是否为沙箱
|
||||
configStorage.setTest(true);
|
||||
return new PayoneerPayService(configStorage);
|
||||
|
||||
//以下不建议进行使用,会引起两次请求的问题
|
||||
//Basic Auth
|
||||
HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
|
||||
/* HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
|
||||
httpConfigStorage.setAuthUsername("PayoneerPay 用户名");
|
||||
httpConfigStorage.setAuthPassword("PayoneerPay API password");
|
||||
return new PayoneerPayService(configStorage, httpConfigStorage);
|
||||
return new PayoneerPayService(configStorage, httpConfigStorage);*/
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -206,6 +217,26 @@ public enum PayType implements BasePayType {
|
||||
}
|
||||
|
||||
|
||||
},payPal{
|
||||
@Override
|
||||
public PayService getPayService(ApyAccount apyAccount) {
|
||||
PayPalConfigStorage storage = new PayPalConfigStorage();
|
||||
storage.setClientID(apyAccount.getAppid());
|
||||
storage.setClientSecret(apyAccount.getPrivateKey());
|
||||
storage.setTest(true);
|
||||
//发起付款后的页面转跳地址
|
||||
storage.setReturnUrl(apyAccount.getReturnUrl());
|
||||
//取消按钮转跳地址,这里兼容的做法
|
||||
storage.setNotifyUrl(apyAccount.getNotifyUrl());
|
||||
return new PayPalPayService(storage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransactionType getTransactionType(String transactionType) {
|
||||
return PayPalTransactionType.valueOf(transactionType);
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
public abstract PayService getPayService(ApyAccount apyAccount);
|
||||
|
||||
Reference in New Issue
Block a user