|
|
@ -0,0 +1,200 @@ |
|
|
|
|
|
package com.cyl.wechat.pay; |
|
|
|
|
|
|
|
|
|
|
|
import com.cyl.wechat.pay.config.WxPay; |
|
|
|
|
|
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; |
|
|
|
|
|
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest; |
|
|
|
|
|
import com.github.binarywang.wxpay.constant.WxPayConstants; |
|
|
|
|
|
import com.github.binarywang.wxpay.exception.WxPayException; |
|
|
|
|
|
import com.github.binarywang.wxpay.service.WxPayService; |
|
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
|
|
|
|
@Component |
|
|
|
|
|
public class MpWxPayService { |
|
|
|
|
|
|
|
|
|
|
|
public boolean dev = true; |
|
|
|
|
|
public WxPay wxPay; |
|
|
|
|
|
public WxPayService wxPayService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 生成二维码 |
|
|
|
|
|
* @param codeUrl |
|
|
|
|
|
* @param logoFile |
|
|
|
|
|
* @param sideLength |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
public Object createScanPayQrcodeMode2(String codeUrl, File logoFile, Integer sideLength){ |
|
|
|
|
|
try { |
|
|
|
|
|
byte[] scanPayQrcodeMode2 = wxPayService.createScanPayQrcodeMode2(codeUrl, null, null); |
|
|
|
|
|
return scanPayQrcodeMode2; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return e.getLocalizedMessage(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 扫码支付 |
|
|
|
|
|
* @param productName |
|
|
|
|
|
* @param clientIp |
|
|
|
|
|
* @param productId |
|
|
|
|
|
* @param price |
|
|
|
|
|
* @param orderNo |
|
|
|
|
|
* @param openId |
|
|
|
|
|
* @param body |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
public Object createQrOrder(String productName, String clientIp, |
|
|
|
|
|
String productId, Integer price, String orderNo, String body) { |
|
|
|
|
|
WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); |
|
|
|
|
|
request.setDeviceInfo("WEB"); //设备号 |
|
|
|
|
|
request.setTradeType("NATIVE"); //交易类型 |
|
|
|
|
|
request.setBody(productName); //商品描述 |
|
|
|
|
|
request.setOutTradeNo(orderNo); //商户订单号 |
|
|
|
|
|
request.setDetail(productName); //商品详情 |
|
|
|
|
|
request.setTotalFee(price); //总金额|分计 |
|
|
|
|
|
request.setSpbillCreateIp(clientIp); //终端IP |
|
|
|
|
|
request.setNotifyUrl(wxPay.notifyUrl);//设置回调路径 |
|
|
|
|
|
request.setProductId(productId); //商品id |
|
|
|
|
|
if (dev) { |
|
|
|
|
|
request.setTotalFee(price); |
|
|
|
|
|
request.setNotifyUrl(wxPay.notifyUrlDev); |
|
|
|
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
Object order = wxPayService.unifiedOrder(request); |
|
|
|
|
|
return order; |
|
|
|
|
|
} catch (WxPayException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return e.getLocalizedMessage(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 微信吊起支付 |
|
|
|
|
|
* @param productName |
|
|
|
|
|
* @param clientIp |
|
|
|
|
|
* @param productId |
|
|
|
|
|
* @param price |
|
|
|
|
|
* @param orderNo |
|
|
|
|
|
* @param openId |
|
|
|
|
|
* @param body |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
public Object createOrder(String productName, String clientIp, |
|
|
|
|
|
String productId, Integer price, String orderNo, |
|
|
|
|
|
String openId, String body){ |
|
|
|
|
|
WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); |
|
|
|
|
|
request.setDeviceInfo("WEB"); //设备号 |
|
|
|
|
|
request.setTradeType("JSAPI"); //交易类型 |
|
|
|
|
|
request.setBody(productName); //商品描述 |
|
|
|
|
|
request.setOutTradeNo(orderNo); //商户订单号 |
|
|
|
|
|
request.setDetail(productName); //商品详情 |
|
|
|
|
|
request.setTotalFee(price); //总金额|分计 |
|
|
|
|
|
request.setSpbillCreateIp(clientIp); //终端IP |
|
|
|
|
|
request.setNotifyUrl(wxPay.notifyOneUrl);//设置回调路径 |
|
|
|
|
|
request.setProductId(productId); //商品id |
|
|
|
|
|
request.setOpenid(openId); //JSAPI OPENID |
|
|
|
|
|
if (dev){ |
|
|
|
|
|
request.setTotalFee(price); |
|
|
|
|
|
request.setNotifyUrl(wxPay.notifyUrlOneDev); |
|
|
|
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
Object order = wxPayService.createOrder(request); |
|
|
|
|
|
return order; |
|
|
|
|
|
} catch (WxPayException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return e.getLocalizedMessage(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public WxPayOrderNotifyResult notify(String requestBody){ |
|
|
|
|
|
WxPayOrderNotifyResult notify = null; |
|
|
|
|
|
try { |
|
|
|
|
|
notify = wxPayService.parseOrderNotifyResult(requestBody); |
|
|
|
|
|
} catch (WxPayException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
return notify; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Object createAppOrder(String productName, String clientIp, |
|
|
|
|
|
String productId, Integer price, |
|
|
|
|
|
String orderNo, String body, |
|
|
|
|
|
String openid){ |
|
|
|
|
|
WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); |
|
|
|
|
|
request.setDeviceInfo("WEB"); |
|
|
|
|
|
request.setBody(productName); |
|
|
|
|
|
request.setOutTradeNo(orderNo); |
|
|
|
|
|
request.setDetail(productName); |
|
|
|
|
|
request.setTotalFee(price); |
|
|
|
|
|
request.setSpbillCreateIp(clientIp); |
|
|
|
|
|
request.setNotifyUrl(wxPay.notifyUrl); |
|
|
|
|
|
request.setTradeType(WxPayConstants.TradeType.JSAPI); |
|
|
|
|
|
request.setProductId(productId); |
|
|
|
|
|
request.setAppid(wxPay.appId); |
|
|
|
|
|
request.setOpenid(openid); |
|
|
|
|
|
if (dev){ |
|
|
|
|
|
request.setTotalFee(price); |
|
|
|
|
|
request.setNotifyUrl(wxPay.notifyUrlDev); |
|
|
|
|
|
request.setAppid(wxPay.appId); |
|
|
|
|
|
request.setOpenid(openid); |
|
|
|
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
Object order = wxPayService.createOrder(request); |
|
|
|
|
|
return order; |
|
|
|
|
|
} catch (WxPayException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return e.getLocalizedMessage(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Object createWebOrder(String productName, String clientIp, |
|
|
|
|
|
String productId, Integer price, |
|
|
|
|
|
String orderNo, String body, |
|
|
|
|
|
String openid){ |
|
|
|
|
|
WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest(); |
|
|
|
|
|
request.setDeviceInfo("WEB"); |
|
|
|
|
|
request.setBody(productName); |
|
|
|
|
|
request.setOutTradeNo(orderNo); |
|
|
|
|
|
request.setDetail(productName); |
|
|
|
|
|
request.setTotalFee(price); |
|
|
|
|
|
request.setSpbillCreateIp(clientIp); |
|
|
|
|
|
request.setNotifyUrl(wxPay.notifyUrl); |
|
|
|
|
|
request.setTradeType(WxPayConstants.TradeType.MWEB); |
|
|
|
|
|
request.setProductId(productId); |
|
|
|
|
|
request.setAppid(wxPay.appId); |
|
|
|
|
|
request.setOpenid(openid); |
|
|
|
|
|
if (dev){ |
|
|
|
|
|
request.setTotalFee(price); |
|
|
|
|
|
request.setNotifyUrl(wxPay.notifyUrlDev); |
|
|
|
|
|
request.setAppid(wxPay.appId); |
|
|
|
|
|
request.setOpenid(openid); |
|
|
|
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
Object order = wxPayService.createOrder(request); |
|
|
|
|
|
return order; |
|
|
|
|
|
} catch (WxPayException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
return e.getLocalizedMessage(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public WxPayOrderNotifyResult appNotify(String requestBody){ |
|
|
|
|
|
WxPayOrderNotifyResult notify = null; |
|
|
|
|
|
try { |
|
|
|
|
|
notify = wxPayService.parseOrderNotifyResult(requestBody); |
|
|
|
|
|
} catch (WxPayException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
return notify; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |