支付宝支付功能的实现
程序员文章站
2022-07-12 14:18:55
...
支付宝支付功能实现
一.环境准备
二.配置沙箱环境
1.浏览器搜索 支付宝开放平台
2.找到开发服务,选择沙箱
3.根据文档提示,一步步配置
4.配置完成后的样子
5.下载好支付宝手机开放平台助手,可以获取应用私钥
**注意:**应用公钥不是直接获取下那个,它是先复制应用公钥是,再到沙箱环境中获取
首先进入沙箱
把你从支付宝开发平台开发助手上复制的 应用公钥,粘贴上去
最后保存设置,就会出现这个界面,得到的就是,我们想要的公钥,是不是很简单呢
三,代码部分
1.新建项目,我用的是SpringBoot项目
导入支付需要的依赖
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.110.ALL</version>
</dependency>
2.编写控制器AlipayController
package com.ytzl.trade2020828.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.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@RestController
@RequestMapping("/pay/ali")
public class AliPayController{
/**
* 沙箱应用APP_ID应用id
*/
public final static String APP_ID="2021000118695484";
/**
* 应用私匙
*/
public final static String APP_PRIVATE_KEY="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCxm7nUK2aLPGrm3qNY0e8NJUBWXz4Ba92wDaqSd9Epk3rVdMJa4gOa/UG1lL/Ex/7IModX8oPCZGDydsUh+ldRPkLsxWoyo3Wtb9jLtv7AM4Fdmmhz0kAGEYlUTPBmsrr++SNDMk/IY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el/v9UCgniJHsQqrZCI4IAkSH4brcGpOpV611aob8x3sALLIotbCe9OAyJ+OuViWR4QI2k9vcKsEbM7643qHWVbMMYJFfwpT4i2fQUeCYrL89L9JXK4iNjkY0BJfmkafOnDWsjNwE2rgvfoAgX43gSHGQ4Or31lsK6P0R5y3AoGAQeW5Xo9uIO4K1BF4ppiCf22lar7Kj/+cCy/Q+zJwIX8eEPJq5NCchGP5zkDKLV4xXNw3+ftdOCfeKEtjcMbOJ4092TeB6qWUeKzkcbxKOepJO3NJfBTqi5btxixbgT8fJIFN6qqjOiHBIINOvSWY0AgWI52W7gXCp/Mc1V1O+qM=";
/**
* 编码
*/
public final static String CHARSET="UTF-8";
/**
* 支付宝公钥
*/
public final static String ALIPAY_PUBLIC_KEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp09reAC0u4oUuvcptaWSqLw6YMUHyIKW7i1pW+4bsYHHivDK2yEIGozfbYA+AB14V3gRPwPd2IEmTUGBGL2KdITMGy65kF0O53cN8Soyi8z0zjn/A0pvQ/aexrJJP1w0IXKsG1xXa6ckS9JDtD5aaUNc829igMhrXPs8LQUN0lsmSwgUqaOzkIXwncHEEf1hiU/9UPaA7S7IOZ8wSQazXMkMRkWeCLMfvGNzTDpKRGoZdKCtfv6/7kLHGl9rfGX8HTDRj479bieiCgxeeXv4MC7HFAwx/TRZbOUeQTri/GbqWZ7oXQLp/T2/QROKN1J3tCKZSRi/Z9lZrlNjWgk3+QIDAQAB";
/**
* 签名类型
*/
public final static String STRING="RSA2";
/**
* 沙箱网关
*/
public final static String GAIEWAY_URL="https://openapi.alipaydev.com/gateway.do";
public final static String FORMT="json";
/**
* 获取订单信息
* @param orderId
* @return
*/
@GetMapping("/getOrderInfo")
public String showOrderInfo(String orderId){
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient(GAIEWAY_URL, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
//实例化具体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("大白兔奶糖");
model.setOutTradeNo("3号");
model.setTimeoutExpress("30m");
model.setTotalAmount("10000");
model.setProductCode("QUICK_MSECURITY_PAY");
request.setBizModel(model);
//异步回调的地址
request.setNotifyUrl("http://3v3n846184.qicp.vip/pay/ali/notity_url");
try {
//这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
System.out.println(response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。
} catch (AlipayApiException e) {
e.printStackTrace();
}
return null;
}
@RequestMapping("/notity_url")
public String notify_url(HttpServletRequest request){
//获取支付宝POST过来反馈信息
Map<String,String> params = new HashMap<String,String>();
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
//乱码解决,这段代码在出现乱码时使用。
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
try {
//如果验签失败
boolean flag = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET,STRING);
if(!flag){
return "fail";
}
System.out.println("修改订单成功!");
return "success";
} catch (AlipayApiException e) {
e.printStackTrace();
return "fail";
}
}
public static void main(String[] args) {
AliPayController aliPayController=new AliPayController();
aliPayController.showOrderInfo(null);
}
}
3.测试,手机上下载 客户端调试工具
1.下载手机客户端
2.将服务器签名好的字符串复制进去
这样支付成功啦,是不是神奇呢
上一篇: (转)Git里设置大小写敏感
下一篇: (转)Git里设置大小写敏感