Spring Boot+WeChat公衆番号JSAPI支払い機能の実現


1、pom.xml依存配置

<!--      -->
<dependency>
  <groupId>com.egzosn</groupId>
  <artifactId>pay-java-wx</artifactId>
  <version>2.12.4</version>
</dependency>
2、appication.ymlファイル配置WeChat公衆番号の基礎情報

#         
wechatpay:
 mchId: #   Id
 appId: #  id
 storePassword: #      
 secretKey: #   
 notifyUrl: #      
 keyStore:  #       
3、設定プロファイル WechatPayConfig.java

package com.example.emoticon.wechat;
 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
/**
 * @ClassName WechatPayConfig
 * @Description       
 * @Author WangJing
 * @Date 2021/3/23 4:38   
 * @Version V1.1.0
 */
 
@Data
@Component
@ConfigurationProperties(prefix = "wechatpay")
public class WechatPayConfig {
 
  private String mchId;//   id(   
 
  private String appId;//  id
 
  private String secretKey;//  
 
  private String notifyUrl;
  private String keyStore;//              .p12     
  private String storePassword;
 
}
4、controller論理コード

package com.example.emoticon.controller;
 
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.RefundOrder;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.common.util.sign.SignUtils;
import com.egzosn.pay.wx.api.WxPayConfigStorage;
import com.egzosn.pay.wx.api.WxPayService;
import com.egzosn.pay.wx.bean.WxTransactionType;
import com.example.emoticon.wechat.WechatPayConfig;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
 
/**
 * @ClassName WechatPayController
 * @Description     Controller
 * @Author WangJing
 * @Date 2021/3/23 4:35   
 * @Version V1.1.0
 */
@RestController
@RequestMapping("/wechatPay")
@Slf4j
public class WechatPayController {
 
 
  @Autowired
  WechatPayConfig wechatPayConfig;
 
  private PayService service = null;
 
  @PostConstruct
  public void init() {
    WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
    wxPayConfigStorage.setMchId(wechatPayConfig.getMchId()); //    id(   
    wxPayConfigStorage.setAppid(wechatPayConfig.getAppId()); //   id
    wxPayConfigStorage.setSecretKey(wechatPayConfig.getSecretKey()); //   
    wxPayConfigStorage.setNotifyUrl(wechatPayConfig.getNotifyUrl()); //        http://  :   /   /      
    wxPayConfigStorage.setSignType(SignUtils.MD5.name());
    wxPayConfigStorage.setInputCharset("utf-8");
    //   api    ,        
    HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
    httpConfigStorage.setKeystore(wechatPayConfig.getKeyStore());//              .p12     
    httpConfigStorage.setStorePassword(wechatPayConfig.getStorePassword());
    //        
    httpConfigStorage.setPath(true);
    service = new WxPayService(wxPayConfigStorage, httpConfigStorage);
    //        
    //      
    httpConfigStorage.setMaxTotal(20);
    //              
    httpConfigStorage.setDefaultMaxPerRoute(10);
    service.setRequestTemplateConfigStorage(httpConfigStorage);
  }
 
  @ApiOperation("      ")
  @RequestMapping(value = "weixinpay", method = RequestMethod.POST)
  public Map<String, Object> weixinpay(HttpServletRequest request) {
    //  :            ,          ,           。            。
    init();
    //     ,        Id,         ,       
    PayOrder payOrder = new PayOrder();//          ,            (      )
    //            ,    。
    payOrder.setSubject("    ");
    payOrder.setBody("    ");
    payOrder.setAddition("    ");
    payOrder.setPrice(new BigDecimal(0.2));//   
    payOrder.setOutTradeNo("     ");
    payOrder.setBankType("     ");
    payOrder.setDeviceInfo("    ");
    payOrder.setSpbillCreateIp("    ip");//   IPUtils.getIpAddr(request)
    payOrder.setOpenid("    openid");
    payOrder.setTransactionType(WxTransactionType.JSAPI);//     
    Map orderInfo = service.orderInfo(payOrder);//          
    log.debug("           " + orderInfo.toString());
    //       ,               。      :signType appId timeStamp nonceStr package sign
    return orderInfo;
  }
 
  /**
   *          
   *
   * @param request
   * @return
   * @throws IOException
   */
  @ApiOperation("    ")
  @RequestMapping(value = "weixinpayBack")
  public String payBack(HttpServletRequest request) throws IOException {
    init();
    //             
    Map<String, Object> params = service.getParameter2Map(request.getParameterMap(), request.getInputStream());
    if (null == params) {
      log.debug("    ");
      return service.getPayOutMessage("failed", "    ").toMessage();
    }
    log.debug("           :" + params.toString());
    //   
    if (service.verify(params)) {
      //                     
      // ......       ........
      log.debug("      ");
      return service.getPayOutMessage("success", "    ").toMessage();
    }
    log.debug("      ");
    return service.getPayOutMessage("fail", "    ").toMessage();
  }
 
 
  @ApiOperation("       ")
  @RequestMapping(value = "weixinAccRefund")
  public String weixinRefund() {
    init();
    RefundOrder refundOrder = new RefundOrder();//      
    refundOrder.setRefundNo("    ,         ,    ");
    refundOrder.setTradeNo("       ,   ");
    refundOrder.setOutTradeNo("    ");
    refundOrder.setRefundAmount(new BigDecimal(0.2));//    
    refundOrder.setTotalAmount(new BigDecimal(0.5));//     
    refundOrder.setOrderDate(new Date());//      
    refundOrder.setDescription("    ");
    Map<String, Object> refund = service.refund(refundOrder);//    
    //     ,      
    log.debug("         :=" + refund.toString());
 
    return "Success";
 
  }
}
ここで、Spring Boot+WeChat公衆番号JSAPI支払機能の実現に関する記事を紹介します。Spring Boot WeChat公式アカウントのJSAPIに関する詳細なお支払い内容は、以前の記事を検索したり、下記の関連記事をご覧になったりしてください。これからもよろしくお願いします。