支付宝登录

This commit is contained in:
Guoqs
2024-06-11 16:30:42 +08:00
parent 0aef83548c
commit d30be0215e
13 changed files with 202 additions and 127 deletions

View File

@@ -1,7 +1,11 @@
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipayEncrypt;
import com.alipay.easysdk.base.oauth.models.AlipaySystemOauthTokenResponse;
import com.alipay.easysdk.factory.Factory;
import com.aliyun.tea.TeaUnretryableException;
@@ -259,21 +263,38 @@ public class SpringBootTestController {
private NotificationService notificationService;
@Test
public void alipayLoginTest() {
String refreshToken = "";
String auth_code = "354109ecc25f4d9f943331578f56XD91";
try {
// AlipaySystemOauthTokenResponse alipaySystemOauthTokenResponse = Factory.Base.OAuth().refreshToken(refreshToken);
// System.out.println(JSON.toJSONString(alipaySystemOauthTokenResponse));
AlipaySystemOauthTokenResponse token = Factory.Base.OAuth().getToken(auth_code);
System.out.println(JSON.toJSONString(token));
} catch (Exception e) {
if (e instanceof TeaUnretryableException) {
TeaUnretryableException exception = (TeaUnretryableException) e;
logger.error("TeaUnretryableException异常:{}", exception.getLastRequest());
public void decryptPhoneNum(){
// 前端接口返回的加密信息
String response ="{\"response\":\"CunEGSbDxsQaKllc35Q+4lJdLiprZhrHFt9erZriETVHv2IrtZsmC8cA6DE5l8GgzvgHCPdGp1iUJQhNyKog==\",\"sign\":\"BlmgjdRvvifS1d9LGcVzq66P7vYrpRHrSCY1SX8zDfU4vEiN4Kz9otxGA4Hz6vG0SoBdlg+Nc58JVUP1IXzQUIFrvAYLC2Uty9J60RmvyWjUf+1Njt7ifX5JgqhMPGOoz3H1OQ1NiqjT6L93KTHYjLt8q37U1aTBO72lQQRgSk0Yf+z1RbalBgRLXHYrgQNORoqJZNN4emQ0a63B30FAFbbko1JbR5eToLK2EcT7vG5rN7wH+3XGTeQ0em81ryNXYUjf7sKLDTsPu7Irk3okOiWMVUr83d3wkST9JWjnO5H7Advh0DaHA1aQnT9oKm82lcUA0r9eE8kqfTmuQ==\"}";
//1. 获取解密所需要的参数
Map<String, String> openapiResult = com.alibaba.fastjson.JSON.parseObject(response, new TypeReference<Map<String, String>>() {}, Feature.OrderedField);
String charset = "UTF-8";
String encryptType = "AES";
String content = openapiResult.get("response");
// 判断是否为加密内容
boolean isDataEncrypted = !content.startsWith("{");
String decryptKey = "B_AES_KEY"; // 商家小程序 AES 密钥
//AES密钥这里参数不能写成固定的开发阶段需传入模板的AES密钥实例化后应传入商家小程序的AES密钥
// 解密
String plainData = null;
if (isDataEncrypted) {
try {
System.out.println("AlipayEncrypt");
plainData = AlipayEncrypt.decryptContent(content, encryptType, decryptKey,charset);
System.out.println("AlipayEncrypt Trance done");
} catch (AlipayApiException e) {
//解密异常, 记录日志
try {
throw new Exception("解密异常");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
logger.error("Exception", e);
} else {
plainData = content;
}
System.out.println(plainData);
}
@Test
@@ -2892,18 +2913,18 @@ public class SpringBootTestController {
@Test
public void testPay() {
String code = "081zIoGa11GamE0iVVIa1aaJ4G0zIoGE";
String openId = memberService.getOpenIdByCode(code);
Map<String, Object> pay = null;
try {
WeixinPayDTO dto = new WeixinPayDTO();
dto.setOpenId(openId);
dto.setAmount("0.01");
pay = orderService.weixinPayV3(dto);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(JSON.toJSONString(pay));
// String code = "081zIoGa11GamE0iVVIa1aaJ4G0zIoGE";
// String openId = memberService.getOpenIdByCode(code);
// Map<String, Object> pay = null;
// try {
// WeixinPayDTO dto = new WeixinPayDTO();
// dto.setOpenId(openId);
// dto.setAmount("0.01");
// pay = orderService.weixinPayV3(dto);
// } catch (Exception e) {
// e.printStackTrace();
// }
// System.out.println(JSON.toJSONString(pay));
}
@Test