2:需要我们为项目添加应用公钥(由平台提供的秘钥工具生成,一般用RSA2,2048位),支付宝公钥由支付宝提供。主要用来加密和解密。
4:将与应用公钥对应的应用私钥,放到验签工具的左方框内,生成右边的签名。
6:为应用添加应用网关和授权回调地址
package verticleTest; import reference.AliPayConfigStorage; public class AliPayStorageSetting { AliPayConfigStorage aliPayConfigStorage; public static AliPayConfigStorage getAliPayConfigStorage() { String app_id = "2016091200494690"; //appid
//应用私钥, 加密 String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCrjJrvXTZubckZqlOjD/V+VyMmAvDuD++rT6sAjM6X1J3gAXkcOotO8WWTXo/zcFHfbtSZjte0xoTz1RFkobWu0RRXJcM7+khCYbjxo/x0nkoqMQuwviikWKDoyxwbkUYEWmdKVcC7rg5tiyBbFGwIywjfo9ztDE7fklDCcDbhSCBlxrOG9Y8xjq/3V0xtvHSQiw+y/966SH+l1ZvnT/RTVb3tKlkSWEvgOJv/c4xz5yuXimFJp1gVKS118O6NRO7PM86Lvqz4GLb6RJ6wrGdwqwig6QTbCPxC9mmONaUE6WQOjhyuvEejoSh8T3GZSUKaMqO/gmAKJWnBAlpnOKt5AgMBAAECggEBAIpZAZZq0Pvr1BO7tmm59BzovzLnilAjUAETZfAynmX6FTxTU829NR38NhPMXkFQYubTZedVOIbU1uIiAj3q4C/U9Bd4H+Rd6yqFzvYpbu7JvTwbg7cNFI+93j7/lnJxDUtzcPuU/rMdIwua+QQbCyloPIbHiuMtWuqZAEJe5xN0mlEH2kKaOjvHvwaBUAC5w3gy/RswTBKOe4n1XaFXUmzuwWD6ECmHjGbKtooPwjuGPKsLYTINCu047uGjzyA0nd8+1dWDdWiWBDflWQRpYWR1zHINftiedHVSuhrKsprPI3/bJE1DV/0+8MbNY2lfIrh54d9e1OKpO+FqoXmrd/UCgYEA2cDo2Lxw7QvT/zEmv1q6q0LUwPhOo9K7lPNW7kB82uaeAdKrei9BC7hJwMfP4c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JNKMmu95O0cpx1Vy0HRZEF0uABvhOnZxflqg4c1U9lffEQ/QWKEzFZKyBui/pNE9CR069eYjS4IIii0ZXQUewpCMDJB1xlTYCVl0a73eGnszl58KzFDWDGZkZeGqumvCCGZ3IK11YqXtQIDAQAB"; String notify_url = "http://k19847n443.51mypc.cn:37169/static/index.html"; // 处理完,支付宝会将处理的结果返回到通知页面 String return_url = "http://k19847n443.51mypc.cn:37169/notifyalipay"; // return_url,返回页面,用户支付完,会跳到哪一个页面 String sign_type = "RSA2"; String charset = "utf-8"; String gatewayUrl = "https://openapidev.alipay.com/gateway.do"; //测试环境网关 String log_path = "C:\"; String uid="2088102175307622"; //获取对应的支付账户操作工具(可根据账户id) // PayResponse payResponse = service.getPayResponse(2); AliPayConfigStorage aliPayConfigStorage = new AliPayConfigStorage(); aliPayConfigStorage.setPid(uid); aliPayConfigStorage.setAppId(app_id); aliPayConfigStorage.setKeyPublic(ALIPAY_PUBLIC_KEY); aliPayConfigStorage.setKeyPrivate(APP_PRIVATE_KEY); aliPayConfigStorage.setNotifyUrl(return_url); aliPayConfigStorage.setSignType(sign_type); aliPayConfigStorage.setSeller(uid); aliPayConfigStorage.setInputCharset(charset); //是否为测试账号,沙箱环境 aliPayConfigStorage.setTest(true); return aliPayConfigStorage; } public void setAliPayConfigStorage(AliPayConfigStorage aliPayConfigStorage) { this.aliPayConfigStorage = aliPayConfigStorage; } }
9:使用vertx开发后台程序。
package aliPay;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.apache.logging.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.PayOrder;
import aliPay.test.AliPayTestStorageSetting;
import aliPay.utils.AliPayConfigStorage;
import aliPay.utils.AliPayService;
import aliPay.utils.AliTransactionType;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.StaticHandler;
public class AliPayVerticle extends AbstractVerticle { //verticle之间相互通信,所以可以将任务交给消费者。
// public static void main(String[] args) {
// Vertx vertx=Vertx.vertx();
//// vertx.deployVerticle(new AliPayStorageSetting());
// vertx.deployVerticle(new AliPayVerticle());
// }
// AliPayStorageSetting aliPayStorgeSetting=new AliPayStorageSetting();
AliPayConfigStorage aliPayConfigStorage = AliPayTestStorageSetting.getAliPayConfigStorage();
public void start() {
org.apache.logging.log4j.Logger logger1 = LogManager.getLogger(AliPayVerticle.class);
// Logger logger = LoggerFactory.getLogger(AliPayVerticle.class);
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create()); // 对请求体做一些限制
/*-----------即时到帐 WAP 网页支付-------------------*/
router.route("/AliInstanceWebPay").handler(ctx -> {
//
// AliPayStorageSetting aliPayStorgeSetting=new AliPayStorageSetting();
// AliPayConfigStorage aliPayConfigStorage = aliPayStorgeSetting.getAliPayConfigStorage();
logger1.debug("AliInstanceWebPay即时到帐 WAP 网页支付");
HttpServerRequest request = ctx.request();
// 支付服务
PayService service = new AliPayService(aliPayConfigStorage);
// logger1.debug(aliPayConfigStorage.getAppid()+"appid");
// 支付订单基础信息,new BigDecimal(0.01)价格
logger1.debug("AliInstanceWebPay创建订单开始");
PayOrder payOrder = new PayOrder("订单title", "摘要", new BigDecimal(0.01),
UUID.randomUUID().toString().replace("-", ""));
/*-----------即时到帐 WAP 网页支付-------------------*/
payOrder.setTransactionType(AliTransactionType.WAP); // WAP支付
payOrder.setSpbillCreateIp(request.getHeader("X-Real-IP"));
/*-----------即时到帐 WAP 网页支付-------------------*/
// StringBuffer requestURL = request.getRequestURL();
String requestURL = request.absoluteURI();
// 设置网页地址
payOrder.setWapUrl(requestURL.substring(0,
requestURL.indexOf("/") > 0 ? requestURL.indexOf("/") : requestURL.length()));
// 设置网页名称
payOrder.setWapName("在线充值");
logger1.debug("AliInstanceWebPay 创建订单结束");
// logger1.debug(payOrder.getBody());
Map directOrderInfo = service.orderInfo(payOrder);
// 获取表单提交对应的字符串,将其序列化到页面即可,
logger1.debug("向阿里发送请求");
String directHtml = service.buildRequest(directOrderInfo, MethodType.POST);
/*-----------/即时到帐 WAP 网页支付-------------------*/
// @RequestMapping(value = "toWxPay.html", produces =
// "text/html;charset=UTF-8")
logger1.debug("返回页面");
ctx.response().setStatusCode(400).putHeader("content-type", "text/html").end(directHtml); // 返回jsonP数据。
});
/*-----------扫码支付-------------------*/
router.route("/AliSweepPay").handler(ctx -> {
// AliPayStorageSetting aliPayStorgeSetting=new AliPayStorageSetting();
// AliPayConfigStorage aliPayConfigStorage = aliPayStorgeSetting.getAliPayConfigStorage();
logger1.debug("AliSweepPay 扫码支付");
HttpServerRequest request = ctx.request();
// 支付服务
PayService service = new AliPayService(aliPayConfigStorage);
// 支付订单基础信息,new BigDecimal(0.01)价格
logger1.debug("AliSweepPay 扫码支付创建订单开始");
PayOrder payOrder = new PayOrder("订单title", "摘要", new BigDecimal(0.01),
UUID.randomUUID().toString().replace("-", ""));
String requestURL = request.absoluteURI();
// 设置网页地址
payOrder.setWapUrl(requestURL.substring(0,
requestURL.indexOf("/") > 0 ? requestURL.indexOf("/") : requestURL.length()));
// 设置网页名称
payOrder.setWapName("在线充值");
/*-----------扫码付-------------------*/
payOrder.setTransactionType(AliTransactionType.SWEEPPAY);
logger1.debug("AliSweepPay 扫码支付创建订单结束");
// 获取扫码付的二维码
logger1.debug("AliSweepPay 生成扫码二维码");
BufferedImage image = service.genQrPay(payOrder);
/*-----------/扫码付-------------------*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "JPEG", baos);
} catch (Exception e) {
e.printStackTrace();
}
byte[] imageByteDataArray = baos.toByteArray();
Buffer buffer = Buffer.buffer();
buffer.appendBytes(imageByteDataArray);
logger1.debug("AliSweepPay 前台返回二维码");
ctx.response()
.setStatusCode(400)
.putHeader("Pragma", "No-cache")
.putHeader("Cache-Control", "no-cache")
.putHeader("Expires", "0")
.putHeader("content-type", "image/jpeg");
ctx.response().end(buffer); // 返回图片
});
/**
* alipay回调处理
*/
router.route("/notifyalipay").handler(ctx->{
logger1.debug("notifyalipay 回调服务");
// 支付服务
PayService service = new AliPayService(aliPayConfigStorage);
HttpServerRequest request = ctx.request();
/*-----------回调处理-------------------*/
Map params=new HashMap();
// // params = service.getParameter2Map(request.params(),System.in );
MultiMap params2 = request.params();
Set<String> names = params2.names();
Iterator<String> iterator = names.iterator();
int i=params2.size();
// logger1.debug("毁掉参数个数"+i);
while(iterator.hasNext()){ //将MultiMap转化为Map
String key=iterator.next();
// logger1.debug("key"+key+"value"+params2.get(key));
params.put(key, params2.get(key));
}
logger1.debug("notifyalipay 回调校验");
if (service.verify(params)){
logger1.info("notifyalipay 校验成功");
return;
}
logger1.error("notifyalipay 校验失败");
});
router.route("/html/*").handler(StaticHandler.create("html"));
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
}