支付宝支付

我的梦境 提交于 2021-02-12 06:41:41
支付宝开发分为生产环境和沙箱环境,沙箱环境是用来做测试用的
1:使用支付宝沙箱环境,会自动生成APPID,商户UID,支付宝网关为https://openapi.alipaydev.com/gateway.do。项目正式上线时网关为https://openapi.alipay.com/gateway.do
 

2:需要我们为项目添加应用公钥(由平台提供的秘钥工具生成,一般用RSA2,2048位),支付宝公钥由支付宝提供。主要用来加密和解密。

2.1:私钥加密,公钥解密,2.2:私钥签名,公钥延签
2.1.1 加密用公钥,解密用私钥
2.2.1:接收支付宝返回的数据需通过支付宝公钥进行验签,确认返回数据者身份为支付宝。  
2.2.2:客户端将商户的签名字符串用私钥签名上传到服务器,服务器端通过开发人员上传的(应用)公钥进行验签,确认身份。
商户后台需要传入支付宝公钥,对支付宝返回数据进行延签。
商户后台需要传入应用私钥,对数据进行加密。
公共平台有支付宝公钥,商户应用公钥。双方都可以获取。
支付宝后台获取应用公钥对商户数据进行延签和解密。
同样商户后台获取支付宝公钥,对返回数据进行延签和解密。
支付宝生成秘钥时,需要在公共平台验签,
 

 

 3:验证秘钥正确性,下载签名工具对秘钥进行验证
      

4:将与应用公钥对应的应用私钥,放到验签工具的左方框内,生成右边的签名。 

 

 

 5:将生成的签名放入到验签框内。
     验证,提示成功,保存
  

6:为应用添加应用网关和授权回调地址

应用网关为商户的服务器
授权回调地址需要公网能访问,商户后台需要对其拦截并验证。是否付款成功等。
7:网页应用创建成功。
 我们就可以编写商户后台程序,为支付宝准备相应的数据,进行支付。   https://github.com/egzosn/pay-java-parent
 
 8:后台对对接支付宝进行配置
   
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+VyMmAv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XocaPmYOi1jQkxNu3DQo1gusEuYVwUAfXt0WHol5b8VnEfSJzqsMA/TM6v8apq6osBsQ99PKqeUdEsfofxMEsCOVstpiAA+GXpcMHYlXOqVM8GgrmnOiUD+NwKBgE64wWxxVDkkLynHpWngbBg61vo7ubi0zH5OWNlg9l+43KBrc+GT9RB/t5JAwKr/F0n4joG0/5nZSN+sNQNE9rCNx9ZxxcYiNSFQdwg/JlBpCxu2a/jlCswFoZdhWooCb3LngMYp0WsqKMaJ0cs2Nx28ZomwgVVfEorYS94jmMpLAoGBAM8htyte48bv98JqQzCe453ajwq4XfB+hpDvz2DiiotX7tPmaPGnn1ugOSrfnPs5ZSCOZPEZOuCKofwDd4U/RH/7L60+a8QeBIfOYZf4YzGzfcVevhXmPBqfFHwM1oy1SGYPichx8YgeqkPGQ5CRSpGt62e7+LFV1QWELFK5VMHf";
     String ALIPAY_PUBLIC_KEY
= "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwt9rjnmpGrOQXsYzuNIs24cPgvFKpXRHRrvuKeViBDmsuk/xzhHGG3aFnN+TH2vXlqkzaDtjmohguRHRf1C8r27lPySmgb5SRoHBVSoxkxE5wt8GkItYfvAc2BtcFZtweWrkQaVVHC1smBxmfxhqyURZIObaAVrfzzEMBRiVy5pCugKhJB9YeIQsxnWTa8wwD9IiGWtfZJNKMmu95O0cpx1Vy0HRZEF0uABvhOnZxflqg4c1U9lffEQ/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);
}

}

 

 

 
 
 
 
 
 
 
 
 
 
 
 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!