微信支付(Java)
目录
简介:
Springboot项目集成微信支付(JSAPI),用于微信公众号对接支付功能。
登录微信支付(JSAPI支付):
获取如下参数:
商户号、商户API私钥路径、商户证书序列号、商户APIV3密钥、appId
注意事项:
JDK版本建议11。
添加依赖:
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.12</version>
</dependency>
application.yaml:
添加微信支付相关配置。
weixin:
merchantId: XXXXXX # 商户号
privateKeyPath: /www/weixin/cert/apiclient_key.pem # 商户API私钥路径
merchantSerialNumber: XXXXXXXXXXXXXXXXXXXXXXXXXXXX #商户证书序列号
apiV3Key: XXXXXX # 商户APIV3密钥
appId: XXXXXX# 自己的AppId
notifyUrl: http://localhost:${server.port}/weixin/payment/notify # 自定义回调地址
WeixinPayController:
功能描述:
- 发起支付;
- 获取签名;
- 成功回调(需自测)。
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;
/**
* 微信支付
*
*/
@RequestMapping("weixin/payment")
@RestController
public class WeixinPayController {
@Autowired
private PaymentService paymentService;
/**
* 发起支付
* @param payment
* @return
*/
@PostMapping("send")
public String payment(@RequestBody PaymentJSAPI payment){
return paymentService.payment(payment);
}
/**
* 获取签名
* @param jsonObject
* @return
*/
@PostMapping("sign")
public String sign(@RequestBody JSONObject jsonObject) throws Exception {
return paymentService.sign(jsonObject);
}
}
PaymentService:
import com.alibaba.fastjson.JSONObject;
public interface PaymentService {
/**
* 发起支付
*
* @param payment
* @return
*/
String payment(PaymentJSAPI payment);
/**
* 生成签名
*
* @param jsonObject
* @return
*/
String sign(JSONObject jsonObject) throws Exception;
}
PaymentServiceImpl:
import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.Amount;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {
@Autowired
private JsapiService jsapiService;
@Autowired
private WeiXinConfig weiXinConfig;
@Override
public String payment(PaymentJSAPI payment) {
PrepayRequest request = new PrepayRequest();
Amount amount = new Amount();
amount.setTotal(weiXinConfig.amountVal);
request.setAmount(amount);
request.setAppid(weiXinConfig.appId);
request.setMchid(weiXinConfig.merchantId);
Payer payer = new Payer();
payer.setOpenid(payment.getOpenid());
request.setPayer(payer);
request.setDescription(payment.getDescription());
request.setNotifyUrl(weiXinConfig.notifyUrl);
request.setOutTradeNo("trade_no_" + System.currentTimeMillis()); // 一个用户就一次
// 调用下单方法,得到应答
PrepayResponse response = jsapiService.prepay(request);
log.info("----payment--response:{}---------", response);
return response.getPrepayId();
}
@Override
public String sign(JSONObject jsonObject) throws Exception {
return weiXinConfig.sign(jsonObject.toJSONString());
}
}
实体类PaymentJSAPI:
import lombok.Data;
import java.io.Serializable;
/**
* 发起账单参数
*/
@Data
public class PaymentJSAPI implements Serializable {
// 描述
private String description;
// 用户的openId
private String openid;
}
后续我们会更新订单查询等功能。
标签:JAVA,SpringBoot,java,微信,request,import,com,payment From: https://blog.csdn.net/qq_36507293/article/details/137426512