1 Star 1 Fork 14

tomas/电商平台-支付

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
AliPayUtil 6.86 KB
一键复制 编辑 原始数据 按行查看 历史
小半斤拔凉 提交于 2017-06-16 17:41 . 更新 AliPayUtil
package com.uzengroup.jkd.portal.util;
import java.util.HashMap;
import java.util.Map;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.config.AlipayConfig;
import com.alipay.util.AlipayNotify;
import com.alipay.util.AlipaySubmit;
import com.uzengroup.jkd.core.Constants;
import com.uzengroup.jkd.core.util.StringUtil;
import com.uzengroup.jkd.core.vo.MsgModel;
/**
* 支付宝支付
* @author liujun
*
*/
public class AliPayUtil {
public static final String ALI_PAY_NEW_WAP_TYPE = "QUICK_WAP_PAY"; //新手机网站支付
public static final String ALI_PAY_CLT_APP_TYPE = "QUICK_MSECURITY_PAY"; //客户端app支付
/**
* 支付宝支付在微信浏览器使用WAP网页支付
* @param outTradeNo 订单号
* @param orderTotal 支付金额
* @param body 订单支付
* @param notifyURL 回调地址
* @param returnURL 回调地址
* @return
*/
public static String getAliPayWapInfo(String outTradeNo, String orderTotal, String body, String notifyURL, String returnURL){
//把请求参数打包成数组
Map<String, String> sParaTemp = new HashMap<String, String>();
sParaTemp.put("service", AlipayConfig.service);
sParaTemp.put("partner", AlipayConfig.partner);
sParaTemp.put("seller_id", AlipayConfig.seller_id);
sParaTemp.put("_input_charset", AlipayConfig.input_charset);
sParaTemp.put("payment_type", AlipayConfig.payment_type);
sParaTemp.put("notify_url", StringUtil.null2Str(notifyURL));
sParaTemp.put("return_url", StringUtil.null2Str(returnURL));
sParaTemp.put("anti_phishing_key", AlipayConfig.anti_phishing_key);
sParaTemp.put("exter_invoke_ip", AlipayConfig.exter_invoke_ip);
sParaTemp.put("out_trade_no", StringUtil.null2Str(outTradeNo));
sParaTemp.put("subject", StringUtil.null2Str(body));
sParaTemp.put("total_fee", StringUtil.null2Str(orderTotal));
sParaTemp.put("body", StringUtil.null2Str(body));
//其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.O9yorI&treeId=62&articleId=103740&docType=1
//sParaTemp.put("参数名","参数值");
//建立请求
return AlipaySubmit.buildRequest(sParaTemp,"get","确认");
}
/**
* 支付宝APP客户端支付
* @param outTradeNo
* @param orderTotal
* @param body
* @param notifyURL
* @param returnURL
* @return
*/
public static String getAliPayAppInfo(String aliPayType, String outTradeNo, String orderTotal, String body, String notifyURL, String returnURL){
//JAVA服务端SDK生成APP支付订单信息示例
String appId = Constants.conf.getProperty("alipay.app.appId");
String appPrivateKey = Constants.conf.getProperty("alipay.app.private.key");
String aliPayPublicKey = Constants.conf.getProperty("alipay.app.public.key");
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, appPrivateKey, "json", AlipayConfig.input_charset, aliPayPublicKey, "RSA2");
//实例化具体API对应的request,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdkmodel入参方式(modelbiz_content同时存在的情况下取biz_content)
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(StringUtil.null2Str(body));
model.setSubject(StringUtil.null2Str(body));
model.setOutTradeNo(outTradeNo);
model.setTimeoutExpress("30m");
model.setTotalAmount(StringUtil.null2Str(orderTotal));
model.setProductCode(StringUtil.null2Str(aliPayType));
alipayRequest.setBizModel(model);
alipayRequest.setNotifyUrl(StringUtil.null2Str(notifyURL));
alipayRequest.setReturnUrl(StringUtil.null2Str(returnURL));
try {
//这里和普通的接口调用不同,使用的是sdkExecute
if(StringUtil.compareObject(AliPayUtil.ALI_PAY_NEW_WAP_TYPE, aliPayType)){
String content = alipayClient.pageExecute(alipayRequest).getBody();
return content;
}else{
AlipayTradeAppPayResponse alipayResponse = alipayClient.sdkExecute(alipayRequest);
//就是orderString 可以直接给客户端请求,无需再做处理。
return alipayResponse.getBody();
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return null;
}
public static boolean verify(Map<String, String> params){
String aliPayPublicKey = Constants.conf.getProperty("alipay.app.public.key");
String signType = StringUtil.null2Str(params.get("sign_type"));
boolean signVerified = false;
try {
if(signType.equals("MD5")){
signVerified = AlipayNotify.verify(params);
}else{
signVerified = AlipaySignature.rsaCheckV1(params, aliPayPublicKey, AlipayConfig.input_charset, signType);
}
} catch (Exception e) {
e.printStackTrace();
}
return signVerified;
}
/**
* 支付宝APP客户端支付
* @param outTradeNo
* @param orderTotal
* @param body
* @param notifyURL
* @param returnURL
* @return
*/
public static MsgModel<String> getQueryAliPayInfo(String outTradeNo, String tradeNo){
MsgModel<String> msgModel = new MsgModel<String> ();
try{
//JAVA服务端SDK生成APP支付订单信息示例
String appId = Constants.conf.getProperty("alipay.app.appId");
String appPrivateKey = Constants.conf.getProperty("alipay.app.private.key");
String aliPayPublicKey = Constants.conf.getProperty("alipay.app.public.key");
Map<String, String> paramMap = new HashMap<String, String> ();
paramMap.put("out_trade_no", outTradeNo);
paramMap.put("trade_no", tradeNo);
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, appPrivateKey, "json","GBK", aliPayPublicKey,"RSA2");
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizContent(StringUtil.mapStrToJson(paramMap));
AlipayTradeQueryResponse response = alipayClient.execute(request);
if(response != null
&& StringUtil.nullToBoolean(response.isSuccess())
&& !StringUtil.isNull(response.getTotalAmount())){
msgModel.setIsSucc(true);
msgModel.setData(StringUtil.null2Str(response.getTotalAmount()));
return msgModel;
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
msgModel.setIsSucc(false);
return msgModel;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/redArmy/dianshangpingtai-zhifu.git
[email protected]:redArmy/dianshangpingtai-zhifu.git
redArmy
dianshangpingtai-zhifu
电商平台-支付
master

搜索帮助