支付宝支付流程
准备工作
下载支付宝开放平台开发助手 支付宝开放平台 申请自己的沙箱环境花生壳 4.客户端调试工具 手机安装 适用场景 在App内集成支付宝支付。APP调用支付宝提供的SDK,SDK再调用支付宝APP内的支付模块。如果用户已安装支付宝APP,商家APP会跳转到支付宝中完成支付,支付完后跳回到商家APP内。如果用户没有安装支付宝APP,商家APP内会调起支付宝网页支付收银台,用户登录支付宝账户,支付完后展示支付结果。
接入
创建应用并获取APPID APPID和支付宝网关都是其自动生成提供的,不需要配置,沙箱只需要在在这里配置密钥。记得用私钥签名公钥验证下其正确性。
1.只能填写和修改应用公钥,支付宝公钥会自动对应生成,两个公钥是不一样的(仅仅头尾像而已) 2.配置AlipayConfig.java的公钥属性使用的是支付宝公钥 3.这个支付宝网关都是沙箱测试环境的,真实线上环境的网关没有 dev,线上的APPID 是另外的。在创建应用时才有 快速接入文档 链接: link.
密钥配置
1. 支付宝密钥处理体系 所以需要获取到应用公钥、应用私钥以及支付宝公钥:
①应用公钥 由商户自己生成的RSA公钥(与应用私钥必须匹配),商户需上传应用公钥到支付宝开放平台,以便支付宝使用该公钥验证该交易是否是商户发起的。
②应用私钥 由商户自己生成的RSA私钥(与应用公钥必须匹配),商户开发者使用应用私钥对请求字符串进行加签。
③支付宝公钥 支付宝的RSA公钥,商户使用该公钥验证该结果是否是支付宝返回的。 打开支付宝开放平台开发助手 更改复制的公钥
Demo案例解析
package com
.example
.demo
.controller
;
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
.request
.AlipayTradeAppPayRequest
;
import com
.alipay
.api
.response
.AlipayTradeAppPayResponse
;
public class AliPayController {
private final static String APP_ID
="2021000119661427";
private final static String APP_PRIVATE_KEY
="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCGINGVpz/IFmLlmu5JB+OIY8chtITv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yHLXdeMyHnElZz3Coif/QKBgQDoLIWhsPROMp23l7LvvpAybvRpd2HnusHO2GoJieK942OQgwUdshIgH38htBaiEsBOdNvsAu5XC4s8ElQvQ/TVM5A8osCBr0ekRixYtjLA7tshgfFw4M3GCYxHcNQpdWNVyLPoa1e2BRBtJcw2OUV5q17gOCu/PkSXoASh7PMLswKBgQC2VXide5nkjzTmNjKP/CVWD2oav1AgFMW+J/kjZTP60+2GMgCSwfsvWQzTu3w9Crb0FQZcbu668JeeN6BgcObfeUKk9Af/aOSCG5kH0SkKNU82YdSELOjay4o2wSRfzskExVNtnxRWNhXMoYQ+qZD9k7ttk67OK2Ykrl4BiYArMwKBgEnEs6TfS5B5BqGpwoHoQ3qyxFeqReJR9XhaTL7BH9kDocPtzTdVN+sa9tLnnHVcjdufx6yidcYUdeXdUQA/kFAZdQ+/CnlqDtJENZzuLwwcGGX6GpOp6detevPMBq0xjiFYhsknKhQ8Ya68Xrkv00JIT/nf/w5ho3ulrTMhzUt/AoGBAKQFkWJBzTehZ9nsSWHjVobriOyUy3SAmz4rYWBo6vCp8tM0afWbUOXP+s0BrIqQ9/Hsc/N0y4cZZZsruTboeZfw0nXnHMazSlSMlQZGLuADAsYODYn1iGdCkIwfBSS2tXh4Nku3viirsMKLTZa9HTQv0HyLTMYknldDEEfbtJOnAoGBAMer/GF+/rag+uWW6JZnrAQNlqhy6BXxZlCuKX5so13VV/QZycYlBtgkBwFSz1DnUwKPd6og3v/0OqE+yq16ERz8Yb0H8wMsJW8g6Kg513AmBsPkGegaNbmiGzvQhdGuSKc2PtHdtHoz49CZW8FZJHoHhDXUiYLESC+pGqwtdnOp";
private final static String CHARSET
="UTF-8";
private final static String ALIPAY_PUBLIC_KEY
="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsRHT6u+jdzu/Z3Ccf39LUgQVj0vF40lGISRNSmCYqE/IpDhNAzZcfftBmJ0t3Gmj5Hi+QYNfQm0DgKwiV5xHEFGVU59JzHqJCsV5IDcCeB8WkIzpzkBmrK3EAw14q/Oibmm6f8PGZvfwgZge7owZx+SqlfnyMsEcfWmUQIgFvvkBEY7XozXEHMILAA6Mk3B1hs+hY+Y9hJF8LML7Hi0VUT4I1CbyMlh7oKpY1P+ySvifyf1MvZ6VO8zCyLpa6nbJyIWfFsC8jw3ZQExdme67VAVnvvJmogPnGVA5+HbUBRR0hkSp8wmuEusNYa2VFnx/DpWLbozgNQiJai9Dgxt0TwIDAQAB+5UZhLLv9lUfWBFhF2xfC23INvVowY3xFXpUu16ytCeDDarxWU8zyGQYCJuNBhW6rP4ZQk7vndCyFg3V+6G3W0TgBOHH9w50eP571Hfc9mACTQVjK7+4HlZ2ISPgUrrVOcH5D7WoiOai9o59jdKx+pb9W6ydzZK1VbZ5GxEHHDMJufJKszcIUR1kayvuHVMB/1u15tQFeDAa0RrPMAuNx5U1N9k5sDI0sdGC5GDsX+elnhtng3j8wneEKLzmtZVW+Kq+JlqQIDAQAB";
private final static String GATEWAY_URL
="https://openapi.alipaydev.com/gateway.do";
private final static String FORMAT
="json";
private final static String SIGN_TYPE
="RSA2";
public void getOrderInfo(){
AlipayClient alipayClient
= new DefaultAlipayClient(GATEWAY_URL
, APP_ID
, APP_PRIVATE_KEY
, "json", CHARSET
, ALIPAY_PUBLIC_KEY
, "RSA2");
AlipayTradeAppPayRequest request
= new AlipayTradeAppPayRequest();
AlipayTradeAppPayModel model
= new AlipayTradeAppPayModel();
model
.setBody("我是测试数据");
model
.setSubject("App支付测试Java");
model
.setOutTradeNo("老洪真帅");
model
.setTimeoutExpress("30m");
model
.setTotalAmount("520");
model
.setProductCode("QUICK_MSECURITY_PAY");
request
.setBizModel(model
);
request
.setNotifyUrl("商户外网可以访问的异步地址");
try {
AlipayTradeAppPayResponse response
= alipayClient
.sdkExecute(request
);
System
.out
.println(response
.getBody());
} catch (AlipayApiException e
) {
e
.printStackTrace();
}
}
public static void main
(String
[] args
){
AliPayController aliPayController
=new AliPayController();
aliPayController
.getOrderInfo();
}
}
这里都是一些配置参数,类似于properties文件的作用。 1)partner在自己的蚂蚁金服个人中心就可以看到,2088开头。 2)支付宝号就不用说了。 3)支付宝公钥也是支付包提供的,复制过来,这里客户蚂蚁金服账号已经收回去了,没法给大家一个一个找,自己细心点,个人中心的这些参数都有。 4)商户私钥就是我们之前生成贴过去的商户密钥。 5)扫码付款页面return_url ,我这里是测试,设置 return_url=”locahost:端口号create_direct_pay_by_user-JAVA-UTF-8/rerturn_url.jsp” 6)notify可以自己随便设置一个,用于付款结束后跳转到商户页面。 6)其他参数暂时不管。 所有静态变量都设置好,确保正确,没有空格,如果下面测试出问题,就从这几个参数里找,是不是哪里有空格,或者密钥贴错了的原因。先不要去把参数弄成配置文件形式再读出来,有些框架会报错。建议测试跑通再尝试配置化。 重启项目继续测试,再次点击确认支付。