2023-12-25 14:57

支付宝沙箱支付-模拟支付宝支付

码自答

JavaEE

(457)

(0)

收藏

        下载服务端SDK的通用版

      image.png



  • 步骤三

    Controller代码

    需要用到4个参数

    参数一:

    支付宝网关地址

    image.png

    参数二:

    沙箱应用的id

    image.png

   参数三:

    接口密钥的私钥

    image.png

    参数四:

    支付宝公钥

    image.png


代码如下:

package com.wanmait.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//支付宝支付接口
@Controller
@RequestMapping("/pay")
public class PayTest {


    private String serverUrl="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private String appId="**********";
    private String privateKey="MIIEvgIBADANB********YfsmTSKGs0A02OdWgREATh8eMLsChZaQC2EdUfEjvrAbY8idyd0f252QPf7duMLjbJhTEc/S8QEKHeU8EzMdLK9GmVuU2tvm9r18pfYZO27w5v+6/WBoP6dYO2zqpAwSTDbMrq3dkwQ2EAiABeagRqCC4cgVnq4TGmdwHIhiqy1j3fD4vtDiZS0KEtsw4RgfalzqslZeoPbgn+tTD0RGdHDNM1kCMebR4MRz0i2wXwhTdEbLrMjnRbIWJdtnuR/3+1Pn95febKZIhVsezdFzQpkzqK2BhlSaWGMfPrd4qknVxqyKjLOC5zN0m94QblnvQbmQmJgqFAgMBAAECggEAVTrhr6RHo+BndJ6VLWz5GAeLPhuwEdWm6MnFv/E02unm+q47Ess6vPhC7r3RIdAdY3GQE34B2sgvpL2GqutJLvR2Bf6teEnyiIZtT66Qes/DUBveMmBMX3rpP4DViWCedo6Sts59vAmo9D/nlk1wY3gI5yhWUX8R1H8w24fCuUvLJ3Wmbc+FbOeNvZQVw8n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bOHnkT0bebt0CqEBoUB6X/9gRLAV3lFcp3Fw4MxG2JnAYUhhf03a9F8GISDm9KD4jpth//O2Q1oOHK300W5JHJh8tsVqsf8QParA0K0VwgdUQPIJTATtE+03TSsUesvMZEbeOM/850Ei1buhFOFv3wfkgVgx1gQArYMenrMxpB2Akf8VXxYjEwB//4Y7uajQkAGWkeweJ5gw/HFIcAwHx6EVwuaZHNoSb0yG63a9XUDyV56feX1K03P0sDXe1p6HuRzY+f3Rjg6n3cAe41dJDySy0+tFyj+xO5hDa9thspcP6qQi8tN0jKmQPM1GrhMkWQtry/kr4F9N9mTFTGQIDAQAB";

    //创建订单接口
    @ResponseBody
    @GetMapping("/create")
    public String create(){
        //创建连接
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,"json", "UTF-8",alipayPublicKey,"RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

        //订单信息
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "202100012260166");//订单号
        bizContent.put("total_amount", 5.00);//订单金额
        bizContent.put("subject", "test");//订单名称
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

        request.setBizContent(bizContent.toString());

        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){

            //成功
            return  response.getBody();
        } else {
            //失败
            return  "调用失败";
        }

    }
}

看到如下网页,表示成功

image.png














0条评论

点击登录参与评论