alipay:
easy:
protocol: https
gatewayHost: openapi-sandbox.dl.alipaydev.com
# gatewayHost: openapi.alipay.com
signType: RSA2
appId:
merchantPrivateKey:
notifyUrl:
@Configuration
@Data
@ConfigurationProperties(prefix = "alipay.easy")
public class AliPayProperties {
private String protocol;
private String gatewayHost;
private String signType;
private String appId;
private String merchantPrivateKey;
private String alipayPublicKey;
private String notifyUrl;
private String encryptKey;
}
@Configuration
@Data
public class AlipayConfig {
@Bean
public Config config(AliPayProperties aliPayProperties) {
Config config = new Config();
config.protocol = aliPayProperties.getProtocol();
config.gatewayHost = aliPayProperties.getGatewayHost();
config.signType = aliPayProperties.getSignType();
config.appId = aliPayProperties.getAppId();
config.merchantPrivateKey = aliPayProperties.getMerchantPrivateKey();
config.alipayPublicKey = aliPayProperties.getAlipayPublicKey();
//可设置异步通知接受服务地址
config.notifyUrl = aliPayProperties.getNotifyUrl();
config.encryptKey = "";
return config;
}
}
@RestController
@Slf4j
@AllArgsConstructor
public class EasyPayController {
private final Config config;
//请求预下单给支付宝后台
@GetMapping("/pay")
public String pay() throws Exception {
// 1. 设置参数(全局只需设置一次)
Factory.setOptions(config);
//调用支付宝接口
AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace()
.preCreate("iphone13 256g", "20150320010101005", "12345");
// 3. 处理响应或异常
// if (ResponseChecker.success(response)) {
// System.out.println("调用成功");
// } else {
// System.out.println("调用失败");
// }
//解析结果
/*
* {
"alipay_trade_precreate_response": {
"code": "10000",
"msg": "Success",
"out_trade_no": "6823789339978248",
"qr_code": "https://qr.alipay.com/bavh4wjlxf12tper3a",
"share_code": "chb4n8A94Qc"
},
"sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}
* */
String body = response.getHttpBody();
//转json对象
JSONObject jsonObject = JSONObject.parseObject(body);
String qrUrl = jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();
//生成二维码
QrCodeUtil.generate(qrUrl, 300, 300, new File("E://img//pay.jpg"));
return body;
}
@GetMapping("/notify")
public String notify(HttpServletRequest request) {
log.info("收到支付成功通知");
String out_trade_no = request.getParameter("out_trade_no");
log.info("流水号{}", out_trade_no);
//todo 后续业务流程发货
return "success";
}
//主动查询接口
@GetMapping("/query")
public String query() throws Exception {
Factory.setOptions(config);
AlipayTradeQueryResponse response = Factory.Payment.Common().query("20150320010101001");
String body = response.getHttpBody();
JSONObject jsonObject = JSONObject.parseObject(body);
String msg = (String) jsonObject.getJSONObject("alipay_trade_query_response").get("msg");
log.info("返回信息:{}",msg);
return msg;
}
}