9.3 李雨欣测试购买记录接口 15:97
parent
99a1941854
commit
2646eded7f
|
@ -4,8 +4,13 @@ import lombok.Data;
|
|||
|
||||
@Data
|
||||
public class AliPay {
|
||||
//订单编号
|
||||
private String traceNo;
|
||||
private String totalAmount;
|
||||
//商品金额
|
||||
private double totalAmount;
|
||||
//商品名称
|
||||
private String subject;
|
||||
|
||||
//订单追踪号,商户自己生成,可已不使用
|
||||
private String alipayTraceNo;
|
||||
}
|
||||
|
|
|
@ -8,22 +8,30 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
|
||||
|
||||
@Data
|
||||
@Component
|
||||
//读取yml文件中alipay 开头的配置
|
||||
@ConfigurationProperties(prefix = "alipay")
|
||||
public class MyAliPayConfig {
|
||||
public class AliPayConfig {
|
||||
|
||||
|
||||
private String appId;
|
||||
private String appPrivateKey;
|
||||
private String alipayPublicKey;
|
||||
//异步通知回调地址(可选)
|
||||
private String notifyUrl;
|
||||
//支付成功后的回调地址
|
||||
private String returnUrl;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
// 设置参数(全局只需设置一次)
|
||||
Config config = new Config();
|
||||
config.protocol = "https";
|
||||
config.gatewayHost = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
|
||||
config.gatewayHost = "openapi.alipaydev.com";
|
||||
config.signType = "RSA2";
|
||||
config.appId = this.appId;
|
||||
config.merchantPrivateKey = this.appPrivateKey;
|
||||
|
@ -32,5 +40,4 @@ public class MyAliPayConfig {
|
|||
Factory.setOptions(config);
|
||||
System.out.println("=======支付宝SDK初始化成功=======");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,47 +1,50 @@
|
|||
package com.muyu.market.server.controller;
|
||||
|
||||
import com.alipay.easysdk.factory.Factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
|
||||
import com.muyu.data.AliPay;
|
||||
import com.muyu.market.server.config.AliPayConfig;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import com.alipay.api.AlipayApiException;
|
||||
import com.alipay.api.AlipayClient;
|
||||
import com.alipay.api.DefaultAlipayClient;
|
||||
import com.alipay.api.request.AlipayTradePagePayRequest;
|
||||
|
||||
import com.muyu.data.AliPay;
|
||||
import com.muyu.market.server.config.MyAliPayConfig;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/alipay")
|
||||
@RequestMapping("alipay")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class AliPayController {
|
||||
//-支付宝网关地址
|
||||
|
||||
@Resource
|
||||
AliPayConfig aliPayConfig;
|
||||
|
||||
private static final String GATEWAY_URL ="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
|
||||
private static final String FORMAT_JSON = "JSON";
|
||||
private static final String CHARSET_UTF8 = "UTF-8";
|
||||
private static final String SIGN_TYPE_RSA2 = "RSA2";
|
||||
@Autowired
|
||||
private MyAliPayConfig myAliPayConfig;
|
||||
private static final String FORMAT ="JSON";
|
||||
private static final String CHARSET ="utf-8";
|
||||
private static final String SIGN_TYPE ="RSA2";
|
||||
|
||||
@GetMapping("/pay")
|
||||
|
||||
public void pay(@Validated AliPay aliPay, HttpServletResponse httpResponse) throws Exception{
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, myAliPayConfig.getAppId(),
|
||||
myAliPayConfig.getAppPrivateKey(), FORMAT_JSON, CHARSET_UTF8, myAliPayConfig.getAlipayPublicKey(), SIGN_TYPE_RSA2);
|
||||
@ResponseBody
|
||||
public Result pay(@Validated AliPay aliPay, HttpServletResponse httpResponse) throws Exception {
|
||||
aliPay.setTraceNo(UUID.randomUUID().toString().replace("-", ""));
|
||||
AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, aliPayConfig.getAppId(),
|
||||
aliPayConfig.getAppPrivateKey(), FORMAT, CHARSET, aliPayConfig.getAlipayPublicKey(), SIGN_TYPE);
|
||||
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
|
||||
request.setNotifyUrl(myAliPayConfig.getNotifyUrl());
|
||||
//-商品的内容可以从系统中传递过来,目前这里是手写的
|
||||
aliPay.setTraceNo(UUID.randomUUID().toString().replaceAll("-",""));
|
||||
aliPay.setTotalAmount("100");
|
||||
aliPay.setSubject("笔记本华硕");
|
||||
|
||||
request.setNotifyUrl(aliPayConfig.getNotifyUrl());
|
||||
request.setReturnUrl(aliPayConfig.getReturnUrl());
|
||||
|
||||
request.setBizContent("{\"out_trade_no\":\"" + aliPay.getTraceNo() + "\","
|
||||
+ "\"total_amount\":\"" + aliPay.getTotalAmount() + "\","
|
||||
|
@ -49,19 +52,19 @@ public class AliPayController {
|
|||
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
|
||||
String form = "";
|
||||
try {
|
||||
form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
|
||||
// 调用SDK生成表单
|
||||
form = alipayClient.pageExecute(request).getBody();
|
||||
} catch (AlipayApiException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
httpResponse.setContentType("text/html;charset=" + CHARSET_UTF8);
|
||||
httpResponse.setContentType("text/html;charset=" + CHARSET);
|
||||
// 直接将完整的表单html输出到页面
|
||||
httpResponse.getWriter().write(form);
|
||||
httpResponse.getWriter().flush();
|
||||
httpResponse.getWriter().close();;
|
||||
httpResponse.getWriter().close();
|
||||
return Result.success(aliPay);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("/notify") // 注意这里必须是POST接口
|
||||
public String payNotify(HttpServletRequest request) throws Exception {
|
||||
if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
|
||||
|
@ -71,15 +74,14 @@ public class AliPayController {
|
|||
Map<String, String[]> requestParams = request.getParameterMap();
|
||||
for (String name : requestParams.keySet()) {
|
||||
params.put(name, request.getParameter(name));
|
||||
// System.out.println(name + " = " + request.getParameter(name));
|
||||
}
|
||||
|
||||
String tradeNo = params.get("out_trade_no");
|
||||
String gmtPayment = params.get("gmt_payment");
|
||||
String alipayTradeNo = params.get("trade_no");
|
||||
|
||||
// (不需要)支付宝验签
|
||||
//if (Factory.Payment.Common().verifyNotify(params)) {
|
||||
// 支付宝验签
|
||||
if (Factory.Payment.Common().verifyNotify(params)) {
|
||||
// 验签通过
|
||||
System.out.println("交易名称: " + params.get("subject"));
|
||||
System.out.println("交易状态: " + params.get("trade_status"));
|
||||
|
@ -90,10 +92,11 @@ public class AliPayController {
|
|||
System.out.println("买家付款时间: " + params.get("gmt_payment"));
|
||||
System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
|
||||
|
||||
// 更新订单未已支付
|
||||
// ordersMapper.updateState(tradeNo, "已支付", gmtPayment, alipayTradeNo);
|
||||
//}
|
||||
// 更新订单已支付的逻辑代码
|
||||
|
||||
}
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -60,3 +60,4 @@ alipay:
|
|||
appPrivateKey: MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCWxZholdxU/C9Vh+4hjD4yWH5MUn3ZK7zkFKB09KY1Kz5k6t0fhcdMRbTaJulyQVL21wneb83f+5hhAzk9+j6RLrvIbE1YZyvt9uoUIpljMkEFyVL2L97/m/Bc+D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F4edAExYJhAoGAUUKT/OarBh4eHUBae7gYXNU76oCO8oqPVWRpZCl/Luu1aNTO8dbLGSOAcX6kTZrP8S4ybP539XDyNoQxjfg7G8vLIZ8WYmge/S5X2qW4gZkWJ1KESzmixOes4/zWhyfV07DI6zF36DwBO0YxxVq8Ksr3GwV1g34O9MMvdQxjdd0CgYBu00EaXBQaxANpO+6oH3KECxvxspKZvk4exI72OFbZ1mZayHNG7BVyfuAMNNp/SRoFXGC4/dVq+tycIOQutvRHrBSRRsv1db8No1j8oEYHvQ5br7OxjS941S/KWeIZefBAyajyCzUjIDZBRTBMGVyJ2p2c/SdUmPs2XTMQnG+mUg==
|
||||
alipayPublicKey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvAodOIG6vaqMsl5DXjcRYPRro+C/SeTswVo5gqQdikK3qy118GWkVwL9gvEovq23EMCTmSV+KAFsHz8f0UxLEK9FZcfRuM5E7GfnP7sdY0ctjfmUhQ7fWVOTGLuH0h5DiQ+4voO/2Hi6SElvtLXAUBSqMKw6J0PsJ0uUY9CBZCQnZkFJWAAroUWw5ARUMy62KPZSAW2s8Qg4gacdhSk7SBACNGBFcJZHCS09xQJi5SxCHaTRLhhIckLBvuKEWhBsh+jU75BKYAC4ovoWhMAFnIWKwCmubhXqmYjuJ4egUQTQBbD1hasK99I1YzdXRH8OKwKD4Lufb42B9buZ/2hOywIDAQAB
|
||||
notifyUrl: http://12.2.1.1:8080/market2/alipay/notify
|
||||
returnUrl: http://12.2.1.1:8080/market2/apitype
|
||||
|
|
Loading…
Reference in New Issue