Java代码实现支付宝扫码支付功能(配置沙箱环境)
程序员文章站
2022-06-04 12:07:42
...
支付宝沙箱配置环境
什么是沙箱呢?
协助开发者进行接口功能,独立于线上环境的配置。
支付接口需要签约生效后才能使用,有些签约几天才能通过,比如资料不全,导致无法立刻签约;沙箱环境提供了测试环境,解决了暂时无法签约导致的延期问题。
设置沙箱
首先我们需要登录到支付宝,然后选择沙箱环境,在完成登录后,需要填写正确的申请人信息
点击设置,下载生成秘钥所需的官方软件 或者 WINDOWS(windows版本工具请不要安装在含有空格的目录路径下,否则会导致公私钥乱码的问题)
准备工具
手机App:客户端测调试、支付宝沙箱支付
创建项目
使用IDEA创建一个maven项目在pom.xml中配置付宝沙箱环境依赖
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.10.111.ALL</version>
</dependency>
SDK生成
去支付宝官网找开发工具,JAVA服务端 SDK 生成 APP支付订单信息示例
创建Controller
放入我们刚复制的示例,自定义修改
@RestController
@RequestMapping("/pay/ali")
public class AliPayController {
/**
* APP_ID 应用id
*/
public final static String APP_ID ="2016093000630181";
/**
* 应用私钥
*/
public final static String APP_PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCTO5ERumMOQlwy2+2hrSUgqFd+PsQ5Au+sK7+xNrB6fmR2oSWSn+X8EEjQBWq21RWV1ekABWSsG2BtOWlnWxT77tfyXLOzejR3vujFhwLPshzA9lfR2R5fH/F3l0MGmWvb3GGYqp2q2SR7jXUtOU5dEEGHUYRd9zoy9CPEM2Iu1xWA5ks51C5XH2AJV62ZDCc82sAJt7xfrDLCZbwKGp0a4b6ZgA1OoZDn7Jzp160fFA81EbkfrKK+qOoyMV9dbHSotHHQfD3+49o1coz3vpPY9r/kD6FhZ3ZdU1SfYccaEpyMAbdfR/RAtHlRiA9JUgXXFiExavKUEwdEGizwug45AgMBAA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mq4CoM2qyqeDSdVCeRqQJ4VSwtsFsGRPXfrrx4CbTKDRiV04hJTVYtnpX67bo34GIXxW1RtLYeCk1lWBI7u3D398qMGcpbo3FzwJ+v8pD3uzTTnk88Xx2qUeghRoLvQd/KP0dcfpZLllBOFA==";
/**
* 编码
*/
public final static String CHARSET ="UTF-8";
/**
* 支付宝公钥
*/
public final static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjeZA25b4mJZpWWXHR0joM8eOSm1DuBDKKgq5ZYDkxKUPhLuXfK588EcFaSGlW9yzpvNkxBbq3QEzqx5K3zYaVHjzh0y+8vlfCcFINMpcYgy3q8NsQHgipO994TqIUwukac8GzezVAT7avtwBeWMNGJDnpQAJNkOD6se2sMu2IXFa7GkfRxhJCMNi/e/d16j0D/YfT2F1c1hRzH1Ey0I3Rkx1WxZTPYH+lCSVaIkxVtolYIpoktE70ZVcD1ERy5eipWCT4sdl/UMifgTnj+PyL+R6lzMUVPKQ+xk5G5PoJUiRSAOkqcaB+KTaerL0h0VzOXKqQ2O/3ppdrTn4X+mAgQIDAQAB";
/**
* (沙箱)网关
*/
public final static String GETEWAY_URL ="https://openapi.alipaydev.com/gateway.do";
/**
* 格式化
*/
public final static String FORMAT ="json";
/**
* 签名类型
*/
public final static String SIGN_TYPE ="RSA2";
/**
* 获取订单信息
*/
@GetMapping("/getOrderInfo")
public String getOrderInfo(String orderId){
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient(GETEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody("我是测试数据");
model.setSubject("App支付测试Java");
model.setOutTradeNo("87878978976"); //交易号
model.setTimeoutExpress("30m");
model.setTotalAmount("6.66");
model.setProductCode("QUICK_MSECURITY_PAY");
//将自己想要传递到异步接口的数据,set进去 pass_back_params
model.setPassbackParams(orderId);
request.setBizModel(model);
request.setNotifyUrl("http://3j383810f3.zicp.vip/pay/ali/notif_url");
try {
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
System.out.println("response.getBody():"+response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。
return response.getBody();
} catch (
AlipayApiException e) {
e.printStackTrace();
return null;
}
}
在我们支付宝网址沙箱环境中查看
APP_ID
2 APP_PRIVATE_KEY及商户私钥
开始测试
public static void main(String[] args) {
AliPayController aliPayController = new AliPayController();
aliPayController.getOrderInfo(null);
}
效果: