wechat-sdk-java

概览

  • 微信支付API v3
  • 微信公众平台
  • 微信小程序
  • 跨城容灾
  • 自动更新微信支付平台证书
  • 自动更新公众平台AccessToken

环境要求

  • Java 11+

安装

Maven

<dependency>
    <groupId>io.github.aapplet</groupId>
    <artifactId>wechat-sdk-all</artifactId>
    <version>0.0.4</version>
</dependency>

初始化

@Configuration
public class WeChatConfiguration {

    @Bean
    WeChatClient client() {
        WeChatConfig weChatConfig = new WeChatConfig();
        weChatConfig.setAppId(".........Appid............");
        weChatConfig.setAppSecret(".....AppSecret........");
        weChatConfig.setMchId(".........MchId............");
        weChatConfig.setMchKey("........MchKey...........");
        weChatConfig.setServiceId(".....ServiceId........");
        weChatConfig.setSerialNo("......SerialNo.........");
        weChatConfig.loadPrivateKey("...PrivateKey.pem...");
        return new DefaultWeChatClient(weChatConfig);
    }

}

示例:JSAPI下单

        WeChatClient client=client();
        WeChatPaymentJsapiRequest request=new WeChatPaymentJsapiRequest();
        request.setDescription("JSAPI下单");
        request.setOutTradeNo("OutTradeNo");
        request.setNotifyUrl("https://xx.xx.xx/notify");
        request.setAmount(new Amount().setTotal(1));
        request.setPayer(new Payer().setOpenId("o4mxp5PnCt6TLrPVmUtG0Xsj8N-I"));
        WeChatPaymentJsapiResponse response=client.execute(request);
        WeChatPaymentJsapiResponse.JsapiResult jsapi=response.jsapi(client.getWeChatConfig());
        System.out.println(jsapi);

回调通知的验签与解密

@RestController
@RequiredArgsConstructor
public class NotifyController {

    private final WeChatClient weChatClient;

    @PostMapping("/notify")
    public void notify(@RequestHeader HttpHeaders headers, @RequestBody String body) {
        final Map<String, String> map = headers.toSingleValueMap();
        final WeChatConfig weChatConfig = weChatClient.getWeChatConfig();
        final WeChatNotifyHandler handler = new WeChatNotifyHandler(weChatConfig, map, body);
        final WeChatPaymentNotify paymentNotify = handler.transform(WeChatPaymentNotify.class);
        System.out.println(paymentNotify);
    }

}

联系我们

如果你发现了BUG或者有任何疑问、建议,请通过issue进行反馈。