本文主要是介绍aliPay Easy版,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
引入依赖
<dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-easysdk</artifactId><version>2.2.2</version></dependency>
创建一个aliPay配置类
package com.jiusi.config;import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;@Data
@ConfigurationProperties(prefix = "alipay")
@Configuration
public class AliPayConfig {// 应用Idprivate String appId;// 应用私有private String appPrivateKey;// 支付宝公钥private String publiceKey;// 回调接口路径private String notifyUrl;// 支付宝网关地址private String gatewayHost;@PostConstructpublic void init(){Config config = new Config();config.protocol = "https";config.gatewayHost = this.gatewayHost;// 支付宝网关地址config.signType = "RSA2";// 业务配置config.appId = this.appId;config.merchantPrivateKey = this.appPrivateKey;config.alipayPublicKey = this.publiceKey;config.notifyUrl = this.notifyUrl;Factory.setOptions(config);System.out.println("支付宝相关配置完成");}
}
配置文件
alipay:appId: 9021000136681693appPrivateKey: 支付宝沙箱私钥publiceKey: 支付宝沙箱公钥notifyUrl: 公网地址+接口返回路径gatewayHost: openapi-sandbox.dl.alipaydev.com
业务代码
package com.jiusi.controller;import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RestController
public class AliPayController {@GetMapping("/api/pay")public String pay(String subject, String outTradeNo, String totalAmount, String returnUrl) throws Exception {//调用支付宝支付接口AlipayTradePagePayResponse pay = Factory.Payment.Page().pay(subject, outTradeNo, totalAmount, returnUrl);String body = pay.getBody();return body;}@PostMapping("/api/notify") //接口返回路径public String payOk(@RequestParam Map param) throws Exception {String tradeStatus = param.getOrDefault("trade_status", "").toString();//判断是否支付成功if(tradeStatus.trim().equals("TRADE_SUCCESS")){//判断是否从支付宝回调的参数if(Factory.Payment.Common().verifyNotify(param)){return "success";}}return "no";}
}
这篇关于aliPay Easy版的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!