受新冠病毒疫情影響,小程序又被推上風間浪頭,曾經的線下實體企業都開始紛紛的轉型線上,但目前線上最大的入口莫過於微信。因此小程序成了商家們轉型線上的首選。而由於微信自己的生態原因,小程序的在線支付只能使用微信小程序支付。這有讓微信支付也越來越火,最近有很多開發者都找我諮詢和要微信支付的源碼的事情。我今天也再說說這事。
微信小程序支付
說道小程序支付,我要稍稍吐槽一下,微信支付真的搞的很亂。如果你之前項目中已經接入了微信的掃碼和App支付,現在要接入小程序支付,大多數人的想法就是,我已經有微信支付的賬號了,直接接入使用,那我告訴你,你錯了。微信小程序支付是通過申請的小程序開通的微信支付,而微信的掃碼和App支付是通過公衆號開通的支付,兩個不可通用。真是夠麻煩,相當讓人反感,但我們還得乖乖的用,誰叫人家微信有如此大的生態呢?
不瞭解微信小程序支付的夥伴們,建議還是先看看開發者文檔,知道基礎的業務流程。
小程序支付的業務流程圖:
一,小程序支付開發源碼
首先創建自己的項目,我這裏創建的是SpringBoot的項目。
1,在resources下創建application.yml文件,並添加配置如下:
spring:
profiles:
active: dev
##pay config
payment:
##wechat config
wx:
##小程序支付
lte:
appid: ***
mchid: ***
key: ***
2,創建獲取配置屬性類PayConfig,代碼如下:
@Component
@ConfigurationProperties(prefix = "payment")
public class PayConfig {
//微信支付類型
//NATIVE--原生支付
public static final String TRADE_TYPE_NATIVE = "NATIVE";
//JSAPI--公衆號支付-小程序支付
public static final String TRADE_TYPE_JSAPI = "JSAPI";
//MWEB--H5支付
public static final String TRADE_TYPE_MWEB = "MWEB";
//APP -- app支付
public static final String TRADE_TYPE_APP = "APP";
//小程序支付參數
public static String WX_LTE_APP_ID;
public static String WX_LTE_MCH_ID;
public static String WX_LTE_KEY;
//微信支付API
public static final String WX_PAY_UNIFIED_ORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
@Value("${payment.wx.lte.appid}")
public void setWxLteAppId(String wxLteAppId) {
WX_LTE_APP_ID = wxLteAppId;
}
@Value("${payment.wx.lte.mchid}")
public void setWxLteMchId(String wxLteMchId) {
WX_LTE_MCH_ID = wxLteMchId;
}
@Value("${payment.wx.lte.key}")
public void setWxLteKey(String wxLteKey) {
WX_LTE_KEY = wxLteKey;
}
}
3,添加啓動類:
4,創建一個常量類,放支付涉及的常量信息
public interface PaymentConstants {
String COMPANY_NAME = "某某某科技有限公司";//用做收款方名稱
String COMPANY_NICK_NAME = "某某某";//收款方名稱簡稱
String COMPANY_PREFIX = "pay_";
//項目環境
String PROJECT_ENV_DEV = "dev";
String PROJECT_ENV_PRO = "pro";
String PAYMENT_TITLE = "商品購買";//支付title信息,也可用真實商品名稱
//支付類型,支付寶和微信
int PAY_TYPE_ALI = 1;
int PAY_TYPE_WX = 2;
//微信支付成功後回調url
String WX_PAY_CALLBACK_URL = "/api/payment/wxNotify";
//掃描支付
String PAY_TRADE_TYPE_QR = "QR";
//App支付
String PAY_TRADE_TYPE_APP = "APP";
//小程序支付
String PAY_TRADE_TYPE_LTE = "LTE";
String SUCCESS = "SUCCESS";
String OK = "OK";
5,創建服務接口PaymentService;
public interface PaymentService {
/**
* 小程序支付
* @param openId
* @param orderNo
* @param money
* @return
* @throws Exception
*/
Map<String, String> wxLtePayment(String openId, String orderNo, double money) throws Exception;
/**
* 微信支付回調
* @param map
* @return
* @throws Exception
*/
int wxNotify(Map<String, Object> map) throws Exception;
PaymentRecord queryPaymentStatusById(String orderNo);
}
6,創建服務接口PaymentServiceImpl;
@Service
public class PaymentServiceImpl implements PaymentService {
private static Logger LOGGER = LoggerFactory.getLogger(PaymentServiceImpl.class);
@Value("${spring.profiles.active}")
private String PROJECT_ENV;
@Value("${server.domain}")
private String SERVER_DOMAIN;
@Autowired
private PaymentRecordMapper paymentRecordMapper;
@Override
@Transactional(readOnly=false,rollbackFor={Exception.class})
public Map<String, String> wxLtePayment(String openId, String orderNo, double money) throws Exception {
LOGGER.info("【小程序支付】 統一下單開始, 訂單編號="+orderNo);
SortedMap<String, String> resultMap = new TreeMap<String, String>();
//生成支付金額
double payAmount = PayUtil.getPayAmountByEnv(PROJECT_ENV, money);
//添加或更新支付記錄
int flag = this.addOrUpdatePaymentRecord(orderNo, payAmount, PaymentConstants.PAY_TYPE_WX, PaymentConstants.PAY_TRADE_TYPE_LTE, false, null);
if(flag < 0){
resultMap.put("returnCode", "FAIL");
resultMap.put("returnMsg", "此訂單已支付!");
LOGGER.info("【小程序支付】 此訂單已支付!");
}else if(flag == 0){
resultMap.put("returnCode", "FAIL");
resultMap.put("returnMsg", "支付記錄生成或更新失敗!");
LOGGER.info("【小程序支付】 支付記錄生成或更新失敗!");
}else{
Map<String,String> resMap = this.wxUnifieldOrder(orderNo, PayConfig.TRADE_TYPE_JSAPI, payAmount,false, openId);
if(PaymentConstants.SUCCESS.equals(resMap.get("return_code")) && PaymentConstants.SUCCESS.equals(resMap.get("result_code"))){
resultMap.put("appId", PayConfig.WX_LTE_APP_ID);
resultMap.put("timeStamp", PayUtil.getCurrentTimeStamp());
resultMap.put("nonceStr", PayUtil.makeUUID(32));
resultMap.put("package", "prepay_id="+resMap.get("prepay_id"));
resultMap.put("signType", "MD5");
resultMap.put("sign", PayUtil.createSign(resultMap,PayConfig.WX_LTE_KEY));
resultMap.put("returnCode", "SUCCESS");
resultMap.put("returnMsg", "OK");
LOGGER.info("【小程序支付】統一下單成功,返回參數:"+resultMap);
}else{
resultMap.put("returnCode", resMap.get("return_code"));
resultMap.put("returnMsg", resMap.get("return_msg"));
LOGGER.info("【小程序支付】統一下單失敗,失敗原因:"+resMap.get("return_msg"));
}
}
return resultMap;
}
@Override
@Transactional(readOnly=false,rollbackFor={Exception.class})
public int wxNotify(Map<String,Object> map) throws Exception{
Integer flag = 0;
//支付訂單編號
String orderNo = (String)map.get("out_trade_no");
//檢驗是否需要再次回調刷新數據
if(this.isNotifyAgain(orderNo)){
PaymentRecordExample example = new PaymentRecordExample();
example.createCriteria().andOrderNoEqualTo(orderNo);
example.setOrderByClause("id DESC");
List<PaymentRecord> list = paymentRecordMapper.selectByExample(example);
if(list!=null && list.size()>0){
//當前時間
Date currentTime = new Date();
PaymentRecord record = list.get(0);
record.setTradeNo(String.valueOf(map.get("transaction_id")));
record.setStatus(Boolean.TRUE);
record.setUpdateTime(currentTime);
//更新條件
PaymentRecordExample where = new PaymentRecordExample();
where.createCriteria().andRecordIdEqualTo(record.getRecordId()).andStatusEqualTo(Boolean.FALSE);
flag = paymentRecordMapper.updateByExampleSelective(record,where);
LOGGER.info("【微信充值回調】 記錄更新成功,訂單值ID="+orderNo);
if(flag > 0){
PaymentNotify paymentNotify = new PaymentNotify();
paymentNotify.setRecordId(record.getRecordId());
paymentNotify.setOrderNo(record.getOrderNo());
paymentNotify.setTradeNo(record.getTradeNo());
paymentNotify.setCreateTime(new Date());
paymentNotify.setStatus(true);
if(paymentNotifyMapper.insert(paymentNotify) > 0){
LOGGER.info("【微信支付回調】 提醒信息生成成功!");
}
}else{
PaymentNotify paymentNotify = new PaymentNotify();
paymentNotify.setRecordId(record.getRecordId());
paymentNotify.setOrderNo(record.getOrderNo());
paymentNotify.setTradeNo(record.getTradeNo());
paymentNotify.setCreateTime(new Date());
paymentNotify.setStatus(false);
if(paymentNotifyMapper.insert(paymentNotify) > 0){
LOGGER.info("【微信支付回調】 提醒信息生成成功!");
}
}
LOGGER.info("【微信支付回調】 訂單支付成功,訂單號:"+orderNo);
}
}
return flag;
}
@Override
@Transactional(readOnly=true,rollbackFor={Exception.class})
public PaymentRecord queryPaymentStatusByNo(String OrderNo){
PaymentRecordExample example = new PaymentRecordExample();
example.createCriteria().andOrderNoEqualTo(OrderNo);
example.setOrderByClause("id DESC");
List<PaymentRecord> list = paymentRecordMapper.selectByExample(example);
if(list != null && list.size()>0 && list.get(0).getStatus()){
return list.get(0);
}
return null;
}
/**
* <p>微信支付統一下單</p>
*
* @param orderNo 訂單編號
* @param tradeType 支付類型
* @param payAmount 支付類型
* @param noLtePay 非小程序支付
* @return
* @throws Exception
*/
private Map<String,String> wxUnifieldOrder(String orderNo, String tradeType, double payAmount, boolean noLtePay, String openid) throws Exception{
//封裝參數
SortedMap<String,String> paramMap = new TreeMap<String,String>();
String appId = noLtePay?PayConfig.WX_APP_ID:PayConfig.WX_LTE_APP_ID;
String mchId = noLtePay?PayConfig.WX_MCH_ID:PayConfig.WX_LTE_MCH_ID;
paramMap.put("appid", appId);
paramMap.put("mch_id", mchId);
paramMap.put("nonce_str", PayUtil.makeUUID(32));
paramMap.put("body", PaymentConstants.COMPANY_NAME);
paramMap.put("out_trade_no", orderNo);
paramMap.put("total_fee", PayUtil.moneyToIntegerStr(payAmount));
paramMap.put("spbill_create_ip", PayUtil.getLocalIp());
paramMap.put("notify_url", this.getNotifyUrl(PaymentConstants.PAY_TYPE_WX));
paramMap.put("trade_type", tradeType);
if (!noLtePay) {
paramMap.put("openid",openid);
}
String payKey = noLtePay?PayConfig.WX_KEY:PayConfig.WX_LTE_KEY;
paramMap.put("sign", PayUtil.createSign(paramMap,payKey));
//轉換爲xml
String xmlData = PayUtil.mapToXml(paramMap);
//請求微信後臺,獲取預支付ID
String resXml = HttpUtils.postData(PayConfig.WX_PAY_UNIFIED_ORDER, xmlData);
LOGGER.info("【微信支付】 統一下單響應:\n"+resXml);
return PayUtil.xmlStrToMap(resXml);
}
/**
* <p>添加或更新支付記錄</p>
*
* @param orderNo
* @param payAmount
* @param payType
* @return
* @throws Exception
*/
private int addOrUpdatePaymentRecord(String orderNo, double payAmount, int payType, String tradeType, boolean isPayment, String tradeNo) throws Exception{
//添加或更新數據庫的支付記錄邏輯
// 寫自己的實現代碼
}
}
7,支付工具欄PayUtil
public class PayUtil {
static Logger log = LogManager.getLogger(PayUtil.class.getName());
/**
* 獲取當前機器的ip
*
* @return String
*/
public static String getLocalIp(){
InetAddress ia=null;
String localip = null;
try {
ia=ia.getLocalHost();
localip=ia.getHostAddress();
} catch (Exception e) {
e.printStackTrace();
}
return localip;
}
/**
* Map轉換爲 Xml
*
* @param map
* @return Xml
* @throws Exception
*/
public static String mapToXml(SortedMap<String, String> map) throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
//防止XXE攻擊
documentBuilderFactory.setXIncludeAware(false);
documentBuilderFactory.setExpandEntityReferences(false);
DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document = documentBuilder.newDocument();
org.w3c.dom.Element root = document.createElement("xml");
document.appendChild(root);
for (String key: map.keySet()) {
String value = map.get(key);
if (value == null) {
value = "";
}
value = value.trim();
org.w3c.dom.Element filed = document.createElement(key);
filed.appendChild(document.createTextNode(value));
root.appendChild(filed);
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
String output = writer.getBuffer().toString();
try {
writer.close();
}
catch (Exception ex) {
}
return output;
}
/**
* 創建簽名Sign
*
* @param key
* @param parameters
* @return
*/
public static String createSign(SortedMap<String,String> parameters,String key){
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();
Iterator<?> it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
if(entry.getValue() != null || !"".equals(entry.getValue())) {
String v = String.valueOf(entry.getValue());
if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
}
sb.append("key=" + key);
String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
return sign;
}
/**
* XML轉換爲Map
*
* @param strXML
* @return Map
* @throws Exception
*/
public static Map<String, Object> getMapFromXML(String strXML) throws Exception {
try {
Map<String, Object> data = new HashMap<String, Object>();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
//防止XXE攻擊
documentBuilderFactory.setXIncludeAware(false);
documentBuilderFactory.setExpandEntityReferences(false);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
org.w3c.dom.Document doc = documentBuilder.parse(stream);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes();
for (int idx = 0; idx < nodeList.getLength(); ++idx) {
Node node = nodeList.item(idx);
if (node.getNodeType() == Node.ELEMENT_NODE) {
org.w3c.dom.Element element = (org.w3c.dom.Element) node;
data.put(element.getNodeName(), element.getTextContent());
}
}
try {
stream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return data;
} catch (Exception ex) {
throw ex;
}
}
/**
* 生成隨機數
*
* @return
*/
public static String makeUUID(int len) {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);
}
/**
* 獲取當前的Timestamp
*
* @return
*/
public static String getCurrentTimeStamp() {
return Long.toString(System.currentTimeMillis()/1000);
}
/**
* 獲取當前的時間
* @return
*/
public static long getCurrentTimestampMs() {
return System.currentTimeMillis();
}
/**
* 生成訂單號
*
* @return
*/
public static String generateOrderNo() {
SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
return sdf.format(new Date())+makeUUID(16);
}
/**
* 獲取當前工程url
*
* @param request
* @return
*/
public static String getCurrentUrl(HttpServletRequest request){
return request.getScheme() +"://" + request.getServerName() + ":" +request.getServerPort() +request.getContextPath();
}
/**
* Xml字符串轉換爲Map
*
* @param xmlStr
* @return
*/
public static Map<String,String> xmlStrToMap(String xmlStr){
Map<String,String> map = new HashMap<String,String>();
Document doc;
try {
doc = DocumentHelper.parseText(xmlStr);
Element root = doc.getRootElement();
List children = root.elements();
if(children != null && children.size() > 0) {
for(int i = 0; i < children.size(); i++) {
Element child = (Element)children.get(i);
map.put(child.getName(), child.getTextTrim());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return map;
}
public static String testXml(){
return "<xml><appid><![CDATA[wx2421b1c4370ec43b]]></appid><attach><![CDATA[支付測試]]></attach><bank_type><![CDATA[CFT]]></bank_type><fee_type><![CDATA[CNY]]></fee_type> <is_subscribe><![CDATA[Y]]></is_subscribe><mch_id><![CDATA[10000100]]></mch_id><nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str><openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid> <out_trade_no><![CDATA[WeChat Pay test]]></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code> <return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign><sub_mch_id><![CDATA[10000100]]></sub_mch_id> <time_end><![CDATA[20140903131540]]></time_end><total_fee>1</total_fee><trade_type><![CDATA[JSAPI]]></trade_type><transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id></xml>";
}
public static String getSceneInfo(String wapUrl,String name){
Map<String,Map<String,String>> map = new HashMap<String, Map<String,String>>();
if(!StringUtils.isEmpty(wapUrl) && !StringUtils.isEmpty(name)){
/*{"h5_info": {"type":"Wap","wap_url": "https://pay.qq.com","wap_name": "騰訊充值"}}*/
Map<String,String> childmap = new TreeMap<String, String>();
childmap.put("type", "Wap");
childmap.put("wap_url",wapUrl);
childmap.put("wap_name", name);
map.put("h5_info", childmap);
return JSON.toJSONString(map);
}
return null;
}
/**
* 轉換金額型到整型
* @param money
* @return
*/
public static String moneyToIntegerStr(Double money){
BigDecimal decimal = new BigDecimal(money);
int amount = decimal.multiply(new BigDecimal(100))
.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
return String.valueOf(amount);
}
/**
* 除去數組中的空值和簽名參數
* @param sArray 簽名參數組
* @return 去掉空值與簽名參數後的新簽名參數組
*/
public static Map<String, String> paraFilter(Map<String, String> sArray) {
Map<String, String> result = new HashMap<String, String>();
if (sArray == null || sArray.size() <= 0) {
return result;
}
for (String key : sArray.keySet()) {
String value = sArray.get(key);
if (value == null || value.equals("") || key.equalsIgnoreCase("sign")
|| key.equalsIgnoreCase("sign_type")) {
continue;
}
result.put(key, value);
}
return result;
}
/**
* 把數組所有元素排序,並按照“參數=參數值”的模式用“&”字符拼接成字符串
* @param params 需要排序並參與字符拼接的參數組
* @return 拼接後字符串
*/
public static String createLinkString(Map<String, String> params) {
List<String> keys = new ArrayList<String>(params.keySet());
Collections.sort(keys);
String prestr = "";
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = params.get(key);
if (i == keys.size() - 1) {//拼接時,不包括最後一個&字符
prestr = prestr + key + "=" + value;
} else {
prestr = prestr + key + "=" + value + "&";
}
}
return prestr;
}
/**
* 根據不同環境生成支付金額
*
* @param env
* @param money
* @param money
* @return
*/
public static double getPayAmountByEnv(String env, Double money){
double pay_money = 0.01;
//測試環境
if("dev".equals(env)){
return 0.01;
}else{
//生成環境
return money;
}
}
}
8,Controller接口編寫
@RestController
@RequestMapping(value = "/api/payment/")
public class PaymentController {
private static Logger logger = LoggerFactory.getLogger(PaymentController.class);
@Value("${server.domain}")
private String serverDomain;
@Autowired
private PaymentService paymentService;
/**
* <p>微信小程序支付接口</p>
* 必傳參數:openId, orderNo, payAmount
*
* @param request
* @param paymentBo
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping(value="ltePay", method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public ResultData toPay(HttpServletRequest request, @RequestBody PaymentBo paymentBo) throws Exception {
if (paymentBo == null || paymentBo.getOpenId() == null || paymentBo.getOrderNo() == null || paymentBo.getPayAmount() == null) {
throw new ParamException();
} else if (paymentBo.getPayAmount() < 0.01) {
return ResultData.fail("訂單金額有誤,請確認!");
}else{
logger.info("【小程序支付服務】請求訂單編號: ["+paymentBo.getOrderNo()+"]");
Map<String, String> resMap = paymentService.wxLtePayment(paymentBo.getOpenId(), paymentBo.getOrderNo(), paymentBo.getPayAmount());
if("SUCCESS".equals(resMap.get("returnCode")) && "OK".equals(resMap.get("returnMsg"))){
//統一下單成功
resMap.remove("returnCode");
resMap.remove("returnMsg");
logger.info("【小程序支付服務】支付下單成功!");
return ResultData.ok(resMap);
}else{
logger.info("【小程序支付服務】支付下單失敗!原因:"+resMap.get("returnMsg"));
return ResultData.fail(resMap.get("returnMsg"));
}
}
}
/**
* 微信支付完成回調Api
*
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value="wxNotify")
public void wxNotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
InputStream inputStream = request.getInputStream();
//獲取請求輸入流
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
outputStream.close();
inputStream.close();
Map<String,Object> map = BeanToMap.getMapFromXML(new String(outputStream.toByteArray(),"utf-8"));
logger.info("【微信支付回調】 回調數據: \n"+map);
String resXml = "";
String returnCode = (String) map.get("return_code");
if ("SUCCESS".equalsIgnoreCase(returnCode)) {
String returnmsg = (String) map.get("result_code");
if("SUCCESS".equals(returnmsg)){
//支付成功
resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
+ "<return_msg><![CDATA[OK]]></return_msg>"+"</xml>";
}else{
resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
+ "<return_msg><![CDATA[報文爲空]></return_msg>" + "</xml>";
logger.info("支付失敗:"+resXml);
}
}else{
resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
+ "<return_msg><![CDATA[報文爲空]></return_msg>" + "</xml>";
logger.info("【訂單支付失敗】");
}
logger.info("【微信支付回調響應】 響應內容:\n"+resXml);
//做出響應
response.getWriter().print(resXml);
}
/**
* <p>查詢訂單支付是否完成</p>
*
* @param orderNo
* @return
* @throws Exception
*/
@ResponseBody
@GetMapping(value="queryPayStatus")
public ResultData queryPayStatus(String orderNo) throws Exception {
if (StringUtils.isEmpty(orderNo)) {
throw new ParamException();
}
PaymentRecord record = paymentService.queryPaymentStatusById(orderNo);
if(record != null){
Map<String,Object> map = new HashMap<String,Object>();
map.put("companyName", PaymentConstants.COMPANY_NAME);
map.put("OrderNo", record.getOrderNo());
map.put("payAmount",record.getPayAmont());
map.put("payMethod",PaymentConstants.getPayMethod(String.valueOf(record.getPayMethod())));
map.put("tradeNo",record.getTradeNo());
map.put("payTime",record.getUpdateTime());
map.put("payStatus",record.getStatus());
return ResultData.ok(map);
}else{
return ResultData.fail("未支付!");
}
}
}
9,Controller接收參數Bo
public class PaymentBo {
/**支付類型:1-支付寶;2-微信*/
private Integer payType;
/**客戶openId*/
private String openId;
/**訂單編號*/
private String orderNo;
/**支付金額*/
private Double payAmount;
public Integer getPayType() {
return payType;
}
public String getOpenId() {
return openId;
}
public String getOrderNo() {
return orderNo;
}
public Double getPayAmount() {
return payAmount;
}
public void setPayType(Integer payType) {
this.payType = payType;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public void setPayAmount(Double payAmount) {
this.payAmount = payAmount;
}
}
10,Controller接口返回類封裝:
public class ResultData<T> implements Serializable {
private static final long serialVersionUID = 1L;
private Integer code;
private String message;
private T data;
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
public void setCode(Integer code) {
this.code = code;
}
public void setMessage(String message) {
this.message = message;
}
public void setData(T data) {
this.data = data;
}
public ResultData() {}
public ResultData(ResultCodeEnums status, T data) {
this(status.getCode(), status.getMessage(), data);
}
public ResultData(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public static <T> ResultData<T> ok(String message){
return new ResultData<T>(ResultCodeEnums.SUCCESS, null);
}
public static <T> ResultData<T> ok(T data){
return new ResultData<T>(ResultCodeEnums.SUCCESS,data);
}
public static <T> ResultData<T> ok(String message,T data){
return new ResultData<T>(ResultCodeEnums.SUCCESS.getCode(), message, data);
}
public static <T> ResultData<T> fail(String message){
return fail(null);
}
public static <T> ResultData<T> fail(T data){
return new ResultData<T>(ResultCodeEnums.FAIL, data);
}
public static <T> ResultData<T> fail(String message,T data){
return new ResultData<T>(ResultCodeEnums.FAIL.getCode(), message, data);
}
}
二,小程序端(獲取統一下單返回參數發起支付)
在小程序端,發起支付請求到,Java後臺的統一下單接口返回prepay_id等參數,然後封裝調起微信的js方法:wx.requestPayment(OBJECT),具體參考文檔:官方文檔
測試一把:
本篇代碼是我們運行兩年電商項目的源碼,目前做的已經很完整健壯,由於依賴和工具類等很多,不能全部貼出來,還請諒解。如果需要,請掃描添加公衆號,聯繫本人獲取其他代碼或支付項目。