用java写调用微信支付接口的代码
时间: 2023-01-30 08:49:53 浏览: 72
很高兴为您解答。
调用微信支付接口的 Java 代码如下:
```
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
public class WeChatPayment {
private static final Logger logger = LoggerFactory.getLogger(WeChatPayment.class);
public static void main(String[] args) throws Exception {
// 加载配置文件
Map<String, String> config = new HashMap<>();
config.put("appId", "wx8888888888888888");
config.put("mchId", "1900000109");
config.put("key", "8934e7d15453e97507ef794cf7b0519d");
config.put("notifyUrl", "http://www.example.com/wechatpay/notify");
WXPay wxPay = new WXPay(config, WXPayConstants.SignType.MD5, false);
// 准备请求参数
Map<String, String> data = new HashMap<>();
data.put("body", "腾讯充值中心-QQ会员充值");
data.put("out_trade_no", "2016090910595900000012");
data.put("fee_type", "CNY");
data.put("total_fee", "888");
data.put("spbill_create_ip", "123.12.12.123");
data.put("notify_url", "http://www.example.com/wechatpay/notify");
data.put("trade_type", "NATIVE");
data.put("product_id", "12");
// 调用接口
Map<String, String> result = wxPay.unifiedOrder(data);
logger.info("微信支付统一下单接口返回结果: {}", result);
// 处理响应结果
if (WXPayUtil.isSignatureValid(result, config.get("key"), WXPayConstants.SignType.MD5)) {
String returnCode = result.get("return_code");
if (returnCode.equals("SUCCESS")) {
String resultCode = result.get("result_code");
if (resultCode.equals("SUCCESS")) {
String qrCodeUrl = result.get("code_url");
logger.info("微信支付二