Project payment access to Alipay [sandbox environment]

Three in a row

foreword

The order payment is connected to Alipay, and the sandbox mechanism provided by Alipay is used to simulate payment for the order. Here I mainly record how the sandbox environment is connected to the system, and the implementation of specific details. Just follow the official documentation.

1. Steps to use

There are several important data to get here, one is Alipay's public key and private key, the other is the payment gateway, and the payment APPID. These data are to be written into
the official manual of the code: Documentation

1.1 Configure the sandbox application environment

Open Platform Console > Recommended Development Tools, click Sandbox to enter the sandbox environment

insert image description here

insert image description here

1.2 Configure interface signature method

The access system uses a custom key , and the public key and private key will be added later.

insert image description here

1.2 Key Generator

You need to download the corresponding version of the key generator. This machine uses windows. Install the next step directly without thinking. address

insert image description hereinsert image description here

insert image description here

Fill in the generated public key and private key here

insert image description here

1.4 Sandbox account

It is the simulated account information and payment password at the time of payment. and receiving merchants

insert image description here

2. Order payment access to Alipay

2.1 pom add dependencies

        <!-- 支付宝sdk -->
        <!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
        <dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
            <version>4.9.28.ALL</version>
        </dependency>

2.2 Server code configuration

When debugging interfaces in the sandbox environment, developers need to adjust the following code configuration:

• Alipay gateway address is changed to: https://openapi.alipaydev.com/gateway.do
• APPID is switched to sandbox APPID
• The signature method uses RSA2
• According to the configured key/certificate, select the corresponding signing code to set up the merchant application Private key and Alipay public key.

These data are extracted into configuration files here. You can also write it directly in the code

#支付宝相关的配置
alipay.app_id=你的id
alipay.merchant_private_key= 你的私钥
alipay.alipay_public_key=你的公钥
alipay.notify_url=http://497n86m7k7.52http.net/payed/notify //这个是下单后的通知
alipay.return_url=http://member.zyz.com/memberOrder.html
alipay.sign_type=RSA2
alipay.charset=utf-8
alipay.gatewayUrl=https://openapi.alipaydev.com/gateway.do

Encapsulated interface, specific parameters can refer to the official document interface call description [Interface API call description]


@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    
    

    // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
    public String app_id;

    // 商户私钥,您的PKCS8格式RSA2私钥
    public String merchant_private_key;

    // 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key;

    // 服务器[异步通知]页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求,告诉我们支付成功的信息
    public String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
    //同步通知,支付成功,一般跳转到成功页
    public String return_url;

    // 签名方式
    private  String sign_type;

    // 字符编码格式
    private  String charset;

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关; https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl;

    public  String pay(PayVo vo) throws AlipayApiException {
    
    

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号,商户网站订单系统中唯一订单号,必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额,必填
        String total_amount = vo.getTotal_amount();
        //订单名称,必填
        String subject = vo.getSubject();
        //商品描述,可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应,响应的是一个页面,只要浏览器显示这个页面,就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应:"+result);

        return result;

    }
}

2.3 Call the payment interface

Call the encapsulated interface method: String pay = alipayTemplate.pay(payVo);For why this parameter is passed in, please check the sandbox case Demo by yourself. The meaning of each parameter

    /**
     * 用户下单:支付宝支付
     * 1、让支付页让浏览器展示
     * 2、支付成功以后,跳转到用户的订单列表页
     * @param orderSn
     * @return
     * @throws AlipayApiException
     */
    @ResponseBody
    @GetMapping(value = "/aliPayOrder",produces = "text/html")
    public String aliPayOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
    
    

        PayVo payVo = orderService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        System.out.println(pay);
        return pay;
    }

2.4 Individual Payment Cases

insert image description here

Download and run it directly, you can see
insert image description here

3. The effect achieved

3.1 Order payment page

insert image description here
insert image description here
insert image description here
insert image description here

alipay.notify_url=http://497n86m7k7.52http.net/payed/notify
//This is the notification after placing an order
This is the access address of your own intranet. That is, after the order payment is successful, call this interface method, and then modify the status of the order. So do an intranet penetration. It is to allow the external network to access the internal network

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43304253/article/details/130285832