扫码效果图
做这个非常非常的困难,经历了很多很多坑后才能做出来,在这里贴一下相关的代码希望能帮助做支付开发的同事们,有不懂的问题可以咨询我QQ76949433,提供技术解答
项目结构截图:
package com.pay.common;
public class AliPayConfig {
public static String returnUrl="http://test.pay.sucaiku.xin";
public static String notifyUrl="http://test.pay.sucaiku.xin/alipayNotifyServlet";
public static String appId="2018235502132232140";
public static String alipayPublicKey="MIIBIjANB12121iG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAomvTj08OCZy3OJr5jGEquQMiLASsDPOCc9C5v240prBNNkwIHPPE3LeO0alcgMXCBNxoC62L3sSe69ZfMMPMEnwO05yofrT1VkLnKsGDLirKv3vbmOEn+D5rree532DfMfI3s3IFX6OCQIDAQAB";
public static String privateKey="MIIEvgIBq2ddewqhksss9w0BAQEFAASCBKgwggSkAgEAAoIBAQDKXggIp3vtM37eMdplPvK+R+DOkA82nuWe4ytQPiO2nJ2ZpxLsTmfTrmvu3zDhENAJ2vKa4ykmGurJtLsP3ZI9hKZbCqKzTVLqeeZKBxmECzZ0OQihGSPhpyMY2+qsH3iMrBzGLozON/4j6G5b+9m9kgwH94ETvspUnp3E1LquAUb7DaHCriI2gVezTBmAwj2xBk+HraJKxAYw1ni+xLLxjcHDE81XHHItGyCihY87GXxr9FuGRj7/j8M30w2bWJWdvT399wol+6iCdKZuI6l1CWlk+omgHG59Hse45+z0yx3pPmGrlilseNW+iQAxaASmd7DytfUWMNIni+HaPkbNAgMBAAECggEBAIzuiXfFdCfQShVh8K+78/+SPTx935343Vb9pM8PF32zJhW4N+k19Tw3z+cQNlMEQejxRakZ7MONAvG6aW6ndgw3kqqRYTK1QFoWci0HPhbjeCzS0w7F6AVHw849EvB2zDWjpF/I+G3uLRTLSIhAwdXGlSdD4zJUEhlTnzYIhWqCLMPEa2u6pL4awfyytVc87305dK6a56DMz+ysrW9mhwnY/bZEdutJIeIZIPXyzc4GEkhFLcH74J33WkRrvYoUaG2mETPgr9detY241214Rknx7kwGvviq/GjjjCnWYWLfMLf/F2VwGE/WsK5qhsEOUyawv4ECgYEA7Rg/OSCWVyA1XgvLgws3q1GMemvtFmPq+k/mvBjrMo8vgm0XI/Czfc4E2F0QV0tCqt9koJ0IAHBjVmAbbB5HZTQjW4O4o92NnbV6nxO1QqBXD+V1GVLK47WDF/I/1wMlhenGxsn5axSCk6Yy0boOG739odqh2FoqJ2XkJcptgqECgYEA2oDnJDBG2xhRfMDr1KJM+g58IEK/X74RpIVXiZVPnrVHHhJjvj+LpMtoYMOroVqJ6DglY4/U3p/BIQkDHGFx5V7kypzOao7YbkJO8x82Tsd3s9HtQ5gqSDnDdA+6s1rcLjLkA+HgStXhecHdfVD+DJQNl/tcSpwdhc/4CwIdAK0CgYEAhFCJx/nVrN2+qrNuab1y3druyMs+TsaINhUtMT++0kJyQxlaVqZXpRe6QHz/R5gqpm1xHOlFs+kEpwmPhDEJqwMD0GicPGbCR4cl3W0ENSCCNc7aIL3BmH6NoGmDEx0gbbFfS0UgOpABpyCTL9PjV7wZtZ/rvm/jIoWMY0KrBsECgYBrEq2x0yJ1rNYJrzQhj6LvCngTCwJitW5J07nQxuUsXaLK/rH4AYW5rJ+qg1gwioB31kOkJVBoA5sNSDBJEQtSHg05qFuSZgtzBHVRuCLRufdTm/SLoh5eJEg+MJMJ3uV0kiWgLWjO4z5JfxG9nhnQKiD7MAbK3POupfnj0xZJRQKBgAR5PzYtuKlH6HC3T7pcwkM9Gy8dgxqRXnk8KvrFBYLw/fAG5xW/5eytgZHq042iVNZLHD234242lugHT4C9oCk/Zw7p4NIQcdUYOR0O9l19sb+SkPGNxFJG8waZrzbxDR92414216wI";
public static String apiUrl="https://openapi.alipay.com/gateway.do";
public static String charset = "UTF-8";
public static String signType="RSA2";
}
package com.pay.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.jpay.alipay.AliPayApi;
import com.jpay.alipay.AliPayApiConfig;
import com.jpay.alipay.AliPayApiConfigKit;
import com.jpay.util.StringUtils;
import com.pay.common.AliPayConfig;
/**
*/
public class AliPayCodeServlet extends HttpServlet {
private static final long serialVersiOnUID= 1L;
public AliPayCodeServlet() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
AliPayApiConfig aliPayApiConfig;
try {
aliPayApiCOnfig= AliPayApiConfigKit.getApiConfig(AliPayConfig.appId);
} catch (Exception e) {
aliPayApiCOnfig= AliPayApiConfig.New();
//支付宝appid
aliPayApiConfig.setAppId(AliPayConfig.appId);
//支付宝公钥
aliPayApiConfig.setAlipayPublicKey(AliPayConfig.alipayPublicKey);
//编码方式
aliPayApiConfig.setCharset(AliPayConfig.charset);
//应用私钥
aliPayApiConfig.setPrivateKey(AliPayConfig.privateKey);
//支付网关
aliPayApiConfig.setServiceUrl(AliPayConfig.apiUrl);
//签名方式
aliPayApiConfig.setSignType(AliPayConfig.signType);
aliPayApiConfig.build();
AliPayApiConfigKit.putApiConfig(aliPayApiConfig);
}
//进度
String totalAmount = "0.01";
//订单号
String outTradeNo = StringUtils.getOutTradeNo();
//同步回调
String returnUrl = AliPayConfig.returnUrl;
//异步回调
String notifyUrl = AliPayConfig.notifyUrl;
AlipayTradePagePayModel model = new AlipayTradePagePayModel();
model.setOutTradeNo(outTradeNo);
//产品 固定值
model.setProductCode("FAST_INSTANT_TRADE_PAY");
model.setTotalAmount(totalAmount);
model.setSubject("订单标题");
model.setBody("订单描述");
model.setPassbackParams("passback_params");
//0:订单码-简约前置模式,对应 iframe 宽度不能小于600px,高度不能小于300px;
//1:订单码-前置模式,对应iframe 宽度不能小于 300px,高度不能小于600px;
//3:订单码-迷你前置模式,对应 iframe 宽度不能小于 75px,高度不能小于75px;
//4:订单码-可定义宽度的嵌入式二维码,商户可根据需要设定二维码的大小。
//跳转模式下,用户的扫码界面是由支付宝生成的,不在商户的域名下。
//2:订单码-跳转模式
model.setQrPayMode("4");
//二维码尺寸
model.setQrcodeWidth(300L);
AliPayApi.tradePage(response, model, notifyUrl, returnUrl);
} catch (AlipayApiException e) {
e.printStackTrace();
}
}
}
package com.pay.servlet;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.jpay.alipay.AliPayApi;
import com.pay.common.AliPayConfig;
/**
*/
public class AlipayNotifyServlet extends HttpServlet {
private static final long serialVersiOnUID= 1L;
public AlipayNotifyServlet() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.err.println("收到支付宝回调");
// 获取支付宝POST过来反馈信息
try {
Map
for (Map.Entry
System.err.println(entry.getKey() + " = " + entry.getValue());
}
boolean verify_result = AlipaySignature.rsaCheckV1(params, AliPayConfig.alipayPublicKey, AliPayConfig.charset, AliPayConfig.signType);
if (verify_result) {// 验证成功
// TODO 请在这里加上商户的业务逻辑程序代码 异步通知可能出现订单重复通知 需要做去重处理
/*
* 字段对应的意思 在支付宝文档中对
* https://docs.open.alipay.com/270/105902/
* body = 订单描述
subject = 订单标题
sign_type = RSA2
auth_app_id = 2018070260533140
notify_type = trade_status_sync
out_trade_no = 092623564015379
point_amount = 0.00
version = 1.0
fund_bill_list = [{"amount":"0.01","fundChannel":"ALIPAYACCOUNT"}]
passback_params = passback_params
buyer_id = 2088302257843420
total_amount = 0.01
trade_no = 2018092622001443420535911693
notify_time = 2018-09-26 23:56:50
charset = UTF-8
invoice_amount = 0.01
trade_status = TRADE_SUCCESS
gmt_payment = 2018-09-26 23:56:50
sign = NmYuT6lnoL7AIkK1c6oDqWhcIvaaAYGq1mEnWL3qqvyoUznccm5LABesSM1ciS0RyXmoL3HX2HDB2+b9FPqgwoFrZ6QQk0h6cJWhGD3nRJoMKK3mBwW9croDcdMhpupIrqpNcOJNDqdgA/89JpRjPy0Z6RYafIAoo0zxG56CYguR0yQw9Mfr9O3rlRqyN0IjqFDC5jh8Rox72j1PP6h2AyvDqgccTwE8sOS1caukhYMkmKwjSsHJRRo2Z3lQyeTDggH4drGowz/MU8Dfnf4jTqTnfcRh77Ya/LuNYuV8mPRG5Ay1H3hKX9EC9uXLx/CvM0ttY++Q1YVojMjXQWS9Xg==
gmt_create = 2018-09-26 23:56:45
buyer_pay_amount = 0.01
receipt_amount = 0.01
app_id = 2018070260533140
seller_id = 2088131608520239
notify_id = 2018092600222235650043420521185261*/
System.err.println("notify_url 验证成功succcess");
return;
} else {
System.err.println("notify_url 验证失败");
return;
}
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.pay.util;
public class MoneyUtil {
public static String getMoney(String amount) {
if (amount == null) {
return "";
}
// 金额转化为分为单位
String currency = amount.replaceAll("\\$|\\¥|\\,", ""); // 处理包含, ¥
// 或者$的金额
int index = currency.indexOf(".");
int length = currency.length();
Long amLOng= 0l;
if (index == -1) {
amLOng= Long.valueOf(currency + "00");
} else if (length - index >= 3) {
amLOng= Long.valueOf((currency.substring(0, index + 3)).replace(".", ""));
} else if (length - index == 2) {
amLOng= Long.valueOf((currency.substring(0, index + 2)).replace(".", "") + 0);
} else {
amLOng= Long.valueOf((currency.substring(0, index + 1)).replace(".", "") + "00");
}
return amLong.toString();
}
}