帮我写一段 springboot整合支付宝与微信支付的代码demo
时间: 2023-03-28 11:03:14 浏览: 115
springboot 集成支付宝支付的示例代码
好的,以下是 Spring Boot 整合支付宝与微信支付的代码示例:
首先,在 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.9.9.ALL</version>
</dependency>
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>3..9</version>
</dependency>
```
然后,在 application.properties 文件中添加以下配置:
```properties
# 支付宝配置
alipay.app-id=your_app_id
alipay.private-key=your_private_key
alipay.public-key=your_public_key
alipay.notify-url=your_notify_url
# 微信支付配置
wxpay.app-id=your_app_id
wxpay.mch-id=your_mch_id
wxpay.key=your_key
wxpay.notify-url=your_notify_url
```
接下来,编写支付宝支付的代码:
```java
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class AlipayService {
@Value("${alipay.app-id}")
private String appId;
@Value("${alipay.private-key}")
private String privateKey;
@Value("${alipay.public-key}")
private String publicKey;
@Value("${alipay.notify-url}")
private String notifyUrl;
public String pay(String outTradeNo, String subject, String totalAmount) throws AlipayApiException {
AlipayClient client = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", publicKey, "RSA2");
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
request.setReturnUrl("your_return_url");
request.setNotifyUrl(notifyUrl);
request.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
+ "\"total_amount\":\"" + totalAmount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
AlipayTradePagePayResponse response = client.pageExecute(request);
return response.getBody();
}
}
```
最后,编写微信支付的代码:
```java
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class WxpayService {
@Value("${wxpay.app-id}")
private String appId;
@Value("${wxpay.mch-id}")
private String mchId;
@Value("${wxpay.key}")
private String key;
@Value("${wxpay.notify-url}")
private String notifyUrl;
public String pay(String outTradeNo, String body, String totalFee) throws Exception {
WXPayConfig config = new WXPayConfig() {
@Override
public String getAppID() {
return appId;
}
@Override
public String getMchID() {
return mchId;
}
@Override
public String getKey() {
return key;
}
@Override
public InputStream getCertStream() {
return null;
}
@Override
public int getHttpConnectTimeoutMs() {
return ;
}
@Override
public int getHttpReadTimeoutMs() {
return ;
}
};
WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5);
Map<String, String> data = new HashMap<>();
data.put("body", body);
data.put("out_trade_no", outTradeNo);
data.put("total_fee", totalFee);
data.put("spbill_create_ip", "your_spbill_create_ip");
data.put("notify_url", notifyUrl);
data.put("trade_type", "NATIVE");
Map<String, String> resp = wxpay.unifiedOrder(data);
String codeUrl = resp.get("code_url");
return codeUrl;
}
}
```
以上就是 Spring Boot 整合支付宝与微信支付的代码示例,希望能对你有所帮助。
阅读全文