SpringBoot+위 챗 공식 번호 JSPI 결제 기능 의 실현

1.pom.xml 의존 설정

<!--      -->
<dependency>
  <groupId>com.egzosn</groupId>
  <artifactId>pay-java-wx</artifactId>
  <version>2.12.4</version>
</dependency>
2.application.yml 파일 설정 위 챗 공중 번호 의 기본 정보

#         
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";
 
  }
}
SpringBoot+위 챗 공식 번호 인 JSPI 결제 기능 의 실현 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 SpringBoot 위 챗 공식 번호 인 JSPI 결제 내용 은 저희 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 저 를 많이 사랑 해 주세요!

좋은 웹페이지 즐겨찾기