package com.stylefeng.guns.modular.system.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alipay.global.api.AlipayClient;
|
import com.alipay.global.api.DefaultAlipayClient;
|
import com.alipay.global.api.exception.AlipayApiException;
|
import com.alipay.global.api.model.ams.*;
|
import com.alipay.global.api.model.constants.EndPointConstants;
|
import com.alipay.global.api.request.ams.notify.AlipayPayResultNotify;
|
import com.alipay.global.api.request.ams.pay.AlipayPayQueryRequest;
|
import com.alipay.global.api.request.ams.pay.AlipayPayRequest;
|
import com.alipay.global.api.response.ams.pay.AlipayPayQueryResponse;
|
import com.alipay.global.api.response.ams.pay.AlipayPayResponse;
|
import com.alipay.global.api.tools.WebhookTool;
|
import lombok.Data;
|
|
import javax.servlet.ServletInputStream;
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.util.Date;
|
|
/**
|
* 支付宝国际版支付工具类
|
*/
|
public class AntomPaymentUtil {
|
/**
|
* replace with your client id <br>
|
* find your client id here: <a href="https://dashboard.antom.com/global-payments/developers/quickStart">quickStart</a>
|
*/
|
public static final String CLIENT_ID = "your_client_id";
|
|
/**
|
* replace with your antom public key (used to verify signature) <br>
|
* find your antom public key here: <a href="https://dashboard.antom.com/global-payments/developers/quickStart">quickStart</a>
|
*/
|
public static final String ANTOM_PUBLIC_KEY = "antom_public_key";
|
|
/**
|
* replace with your private key (used to sign) <br>
|
* please ensure the secure storage of your private key to prevent leakage
|
*/
|
public static final String MERCHANT_PRIVATE_KEY = "your_private_key";
|
|
private final static AlipayClient CLIENT = new DefaultAlipayClient(EndPointConstants.SG, MERCHANT_PRIVATE_KEY, ANTOM_PUBLIC_KEY, CLIENT_ID);
|
|
|
/**
|
* 发起付款请求
|
* @param payment
|
* @return
|
*/
|
public static ResultUtil<AlipayPayResponse> pay(PaymentVO payment){
|
AlipayPayRequest alipayPayRequest = new AlipayPayRequest();
|
alipayPayRequest.setClientId(CLIENT_ID);
|
alipayPayRequest.setProductCode(ProductCodeType.CASHIER_PAYMENT);
|
alipayPayRequest.setPaymentRequestId(payment.getPaymentRequestId());
|
|
Amount paymentAmount = new Amount();
|
paymentAmount.setCurrency(payment.getCurrency());
|
paymentAmount.setValue(payment.getAmountValue());
|
alipayPayRequest.setPaymentAmount(paymentAmount);
|
|
PaymentMethod paymentMethod = new PaymentMethod();
|
paymentMethod.setPaymentMethodType(WalletPaymentMethodType.ALIPAY_CN.name());
|
alipayPayRequest.setPaymentMethod(paymentMethod);
|
|
alipayPayRequest.setPaymentNotifyUrl(payment.getPaymentNotifyUrl());
|
alipayPayRequest.setPaymentRedirectUrl(payment.getPaymentRedirectUrl());
|
|
SettlementStrategy settlementStrategy = new SettlementStrategy();
|
settlementStrategy.setSettlementCurrency(payment.getCurrency());
|
alipayPayRequest.setSettlementStrategy(settlementStrategy);
|
|
Order order = new Order();
|
order.setReferenceOrderId(payment.getReferenceOrderId());
|
order.setOrderDescription(payment.getOrderDescription());
|
Amount orderAmount = new Amount();
|
orderAmount.setCurrency(payment.getCurrency());
|
orderAmount.setValue(payment.getAmountValue());
|
order.setOrderAmount(orderAmount);
|
alipayPayRequest.setOrder(order);
|
|
Env env = new Env();
|
env.setTerminalType(TerminalType.APP);
|
alipayPayRequest.setEnv(env);
|
AlipayPayResponse alipayPayResponse = null;
|
try {
|
alipayPayResponse = CLIENT.execute(alipayPayRequest);
|
} catch (AlipayApiException e) {
|
return ResultUtil.error(e.getErrMsg());
|
}
|
return ResultUtil.success(alipayPayResponse);
|
}
|
|
|
/**
|
* 查询付款结果
|
* @param paymentRequestId
|
* @return
|
*/
|
public static ResultUtil<AlipayPayQueryResponse> inquiryPayment(String paymentRequestId){
|
AlipayPayQueryRequest alipayPayQueryRequest = new AlipayPayQueryRequest();
|
alipayPayQueryRequest.setPaymentRequestId(paymentRequestId);
|
|
AlipayPayQueryResponse alipayPayQueryResponse;
|
try {
|
long startTime = System.currentTimeMillis();
|
System.out.println("inquiry payment request: " + JSON.toJSONString(alipayPayQueryRequest));
|
alipayPayQueryResponse = CLIENT.execute(alipayPayQueryRequest);
|
System.out.println("inquiry payment response: " + JSON.toJSONString(alipayPayQueryResponse));
|
System.out.println("inquiry payment request cost time: " + (System.currentTimeMillis() - startTime) + "ms\n");
|
} catch (AlipayApiException e) {
|
return ResultUtil.error(e.getErrMsg());
|
}
|
return ResultUtil.success(alipayPayQueryResponse);
|
}
|
|
/**
|
* 支付回调处理
|
* @param request
|
* @return
|
*/
|
public static ResultUtil<AlipayPayResultNotify> receivePaymentNotify(HttpServletRequest request){
|
// retrieve the required parameters from http request
|
String requestUri = request.getRequestURI();
|
String requestMethod = request.getMethod();
|
// retrieve the required parameters from request header
|
String requestTime = request.getHeader("request-time");
|
String clientId = request.getHeader("client-id");
|
String signature = request.getHeader("signature");
|
|
try {
|
ServletInputStream inputStream = request.getInputStream();
|
StringBuilder sb = new StringBuilder();
|
String s;
|
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
while ((s = in.readLine()) != null) {
|
sb.append(s);
|
}
|
in.close();
|
inputStream.close();
|
String notifyBody = sb.toString();
|
// verify the signature of notification
|
boolean verifyResult = WebhookTool.checkSignature(requestUri, requestMethod, clientId,
|
requestTime, signature, notifyBody, ANTOM_PUBLIC_KEY);
|
if (!verifyResult) {
|
return ResultUtil.error("Invalid notify signature");
|
}
|
// deserialize the notification body
|
AlipayPayResultNotify paymentNotify = JSON.parseObject(notifyBody, AlipayPayResultNotify.class);
|
if (paymentNotify != null && "SUCCESS".equals(paymentNotify.getResult().getResultCode())) {
|
// handle your own business logic.
|
// e.g. The relationship between payment information and users is kept in the database.
|
System.out.println("receive payment notify: " + JSON.toJSONString(paymentNotify));
|
return ResultUtil.success(paymentNotify);
|
}
|
return ResultUtil.error("system_error");
|
} catch (Exception e) {
|
e.printStackTrace();
|
return ResultUtil.error(e.getMessage());
|
}
|
}
|
|
|
|
@Data
|
public static class PaymentVO {
|
/**
|
* 支付请求流水号
|
*/
|
private String paymentRequestId;
|
/**
|
* 付款的结算货币
|
*/
|
private String currency;
|
/**
|
* 支付金额
|
*/
|
private String amountValue;
|
/**
|
* 商户端支付结果页面
|
*/
|
private String paymentRedirectUrl;
|
/**
|
* 支付结果通知地址
|
*/
|
private String paymentNotifyUrl;
|
/**
|
* 订单描述
|
*/
|
private String orderDescription;
|
/**
|
* 商户订单唯一标识
|
*/
|
private String referenceOrderId;
|
}
|
|
|
@Data
|
public static class PaymentNotify {
|
/**
|
* 支付状态通知的类型
|
* PAYMENT_RESULT: 表示通知是关于支付结果的。
|
* PAYMENT_PENDING: 表示用户已完成支付。商家需要等待最终的支付结果。
|
*/
|
private String notifyType;
|
/**
|
* 支付结果的详细信息,如支付状态、结果代码和结果消息
|
*/
|
private String result;
|
/**
|
* 商户为识别支付请求而分配的专属 ID
|
*/
|
private String paymentRequestId;
|
/**
|
* Antom 为识别支付而分配的支付 ID。paymentId 与 paymentRequestId 之间存在一对一对应关系。
|
*/
|
private String paymentId;
|
/**
|
* 商家请求在订单币种中接收的支付金额。
|
*/
|
private String paymentAmount;
|
/**
|
* 支付创建的日期和时间。
|
*/
|
private Date paymentCreateTime;
|
/**
|
* 支付成功达到最终状态的日期和时间。
|
*/
|
private Date paymentTime;
|
/**
|
* 支付结果信息
|
*/
|
private String paymentResultInfo;
|
}
|
}
|