当前位置:   article > 正文

node+vue实现微信支付(沙箱)完整版,亲测可用_前端微信沙箱支付 csdn

前端微信沙箱支付 csdn

网上很多资料文档都是残次不全的,没几个真正有用的,我这记录下可用的实现方式

微信的沙箱的区别就是在正常的所有api前面加一个sandboxnew。

demo 请狠狠的戳这里 ¥  https://download.lllomh.com/cliect/#/product/J302779150521235  如点击链接跳转失败请复制网址到浏览器打开

demo 请狠狠的戳这里 c  https://download.csdn.net/download/lllomh/15534415

支付宝支付请点这里 node+vue实现支付宝支付(沙箱)完整版,亲测可用_vue+node实现支付功能-CSDN博客

1,事前准备

这里的商户必须要跟商户绑定过才行,不然会报错,

1.1获取沙箱秘钥

  1. function getKey(){
  2. //首先拿到前端传过来的参数
  3. let orderCode = 'xy16143316973304295';
  4. let money = 1.01;
  5. //首先生成签名sign
  6. let mch_id = mchid;
  7. let nonce_str = wxpay.createNonceStr();
  8. let timestamp = wxpay.createTimeStamp();
  9. let body = '微信支付';
  10. let out_trade_no = orderCode;
  11. let total_fee = wxpay.getmoney(money);
  12. let spbill_create_ip = '49.235.115.11';//终端ip
  13. let notify_url = wxurl;
  14. let trade_type = 'NATIVE';
  15. let sign = wxpay.paysignjsapi(appid,body,mch_id,nonce_str,notify_url,out_trade_no,spbill_create_ip,total_fee,trade_type,mchkey);mchkey// 秘钥要是正式的秘钥才可以获取
  16. //组装xml数据
  17. var formData = "<xml>";
  18. formData += "<appid>"+appid+"</appid>"; //appid
  19. formData += "<body><![CDATA["+"微信支付"+"]]></body>";
  20. formData += "<mch_id>"+mch_id+"</mch_id>"; //商户号
  21. formData += "<nonce_str>"+nonce_str+"</nonce_str>"; //随机字符串,不长于32位。
  22. formData += "<notify_url>"+notify_url+"</notify_url>";
  23. formData += "<out_trade_no>"+out_trade_no+"</out_trade_no>";
  24. formData += "<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>";
  25. formData += "<total_fee>"+total_fee+"</total_fee>";
  26. formData += "<trade_type>"+trade_type+"</trade_type>";
  27. formData += "<sign>"+sign+"</sign>";
  28. formData += "</xml>";
  29. let promise = new Promise(function(resolve, reject) {
  30. let urls = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey"
  31. request({url:urls,method:'POST',body: formData},function(err,response,body){
  32. if(!err && response.statusCode == 200){
  33. xmlreader.read(body.toString("utf-8"), function (errors, response) {
  34. var sandbox_signkey = response.xml.sandbox_signkey.text()
  35. resolve(sandbox_signkey)
  36. });
  37. }
  38. })
  39. });
  40. return promise
  41. }

2,封装方法

  1. function wxpPys(sorder,smoney) {
  2. //首先拿到前端传过来的参数
  3. let orderCode = sorder;
  4. let money = smoney;
  5. //首先生成签名sign
  6. let mch_id = wechatConfig.mchid;
  7. let appid = wechatConfig.appid;
  8. let mchkey = wechatConfig.mchkey;
  9. let nonce_str = wxpay.createNonceStr();
  10. let timestamp = wxpay.createTimeStamp();
  11. let body = '微信支付';
  12. let out_trade_no = orderCode;
  13. let total_fee = wxpay.getmoney(money);
  14. let spbill_create_ip = '49.235.115.11';//终端ip
  15. let notify_url = wechatConfig.wxurl;
  16. let trade_type = 'NATIVE';
  17. let sign = wxpay.paysignjsapi(appid,body,mch_id,nonce_str,notify_url,out_trade_no,spbill_create_ip,total_fee,trade_type,mchkey);
  18. //组装xml数据
  19. let formData = "<xml>";
  20. formData += "<appid>"+appid+"</appid>"; //appid
  21. formData += "<body><![CDATA["+"微信支付"+"]]></body>";
  22. formData += "<mch_id>"+mch_id+"</mch_id>"; //商户号
  23. formData += "<nonce_str>"+nonce_str+"</nonce_str>"; //随机字符串,不长于32位。
  24. formData += "<notify_url>"+notify_url+"</notify_url>";
  25. formData += "<out_trade_no>"+out_trade_no+"</out_trade_no>";
  26. formData += "<spbill_create_ip>"+spbill_create_ip+"</spbill_create_ip>";
  27. formData += "<total_fee>"+total_fee+"</total_fee>";
  28. formData += "<trade_type>"+trade_type+"</trade_type>";
  29. formData += "<sign>"+sign+"</sign>";
  30. formData += "</xml>";
  31. var url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder';
  32. let promise = new Promise(function(resolve, reject) {
  33. request({url:url,method:'POST',body: formData},function(err,response,body){
  34. if(!err && response.statusCode == 200){
  35. console.log(body);
  36. xmlreader.read(body.toString("utf-8"), function (errors, response) {
  37. console.log(response.xml)
  38. var code_url = response.xml.code_url.text();
  39. resolve(code_url)
  40. });
  41. }
  42. })
  43. });
  44. return promise
  45. }

3,接口定义

  1. /**
  2. * 添加购物车支付微信 */
  3. router.post('/api/member/payOrder', (req, res) => {
  4. let orderId=req.body.orderId
  5. let reuct = new wxpPys(orderId,1.01)
  6. reuct.then(response=>{
  7. res.send(
  8. {
  9. "success": true,
  10. "message": "success",
  11. "code": 200,
  12. "timestamp": (new Date()).getTime(),
  13. "result": response
  14. }
  15. )
  16. })
  17. })

查询订单:

  1. /**
  2. * 添加购物车提交订单微信支付后查询订单状态是否成功 */
  3. router.post('/api/member/queryOrderWechat', (req, res) => {
  4. let orderId=req.body.orderId
  5. let reuct = new queryOrder(orderId,1.01)
  6. reuct.then(response=>{
  7. console.log(response)
  8. res.send(
  9. {
  10. "success": true,
  11. "message": "success",
  12. "code": 200,
  13. "timestamp": (new Date()).getTime(),
  14. "result": response
  15. }
  16. )
  17. })
  18. })

这里要强调沙箱版是不需要扫码的,等于弹出微信自动扫码支付了,直接看结果就行!

正式的时候就把 所有链接中的sandboxnew去掉就行 

4,结果

结果demo

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/186228
推荐阅读
相关标签
  

闽ICP备14008679号