示例代码包下载地址:https://www.123865.com/s/fS5Cjv-EOGOd?pwd=zhif# 此示例小程序包含后台代码,主要是通过支付FM接入微信小程序支付,支持通过H5拉起小程序支付。你也可以直接修改对应参数后快速发布小程序,也可以参考这个代码包中写法自行对接。

【小程序源码说明】:
1.siteinfo.js 中的 域名 修改为你自己的后台地址
2.lionfish_comshop\pages\user\payfm.js 26行【接口根地址】修改为你支付FM账号的接口根地址。

1. 修改apiUrl

修改lionfish_comshop/pages/user/payfm.js 中的apiUrl地址为你自己的。【用户中心→API开发信息→接口根地址】 微信支付小程序接入示例源码 - 图1

2. 小程序平台设置

request合法域名
进入小程序,管理→开发管理→服务器域名→request合法域名,填写支付FM用户中心页面查看到的【接口根地址】 ,以及你的小程序后台域名。 微信支付小程序接入示例源码 - 图2

3. 发布小程序

发布小程序前,登录小程序的后台,上传你的经营类目的商品。小程序开发不要咨询我们客服。

4. 增值服务

对于没有技术人员的老板,我们可以提供小程序代发布服务,服务费收取价格:1000元/次

5. 自有小程序

如果您已有微信小程序,可以在您的小程序中手动竭诚payfm.wxml页面。并将在你小程序中的页面路径配置到支付FM后台微信JSAPI小程序中的“页面路径”。下文附上payfm.wxml相关的完整代码。
微信支付小程序接入示例源码 - 图3 微信支付小程序接入示例源码 - 图4

payfm.wxml
  1. <view style="text-align: center;">
  2. 支付成功后请关闭小程序
  3. </view>
payfm.js
  1. Page({
  2. data: {
  3. openid: '',
  4. orderId: ''
  5. },
  6. /**
  7. * 生命周期函数--监听页面加载
  8. */
  9. onLoad: function (options) {
  10. console.log("options=" + options); // 输出 {s: "fm", orderId: "123"}
  11. // console.log(userInfo);
  12. wx.showModal({
  13. title: "订单号",
  14. content: options.orderId
  15. });
  16. // wx.showToast({
  17. // title: options.orderId,
  18. // icon: 'none',
  19. // duration: 6000
  20. // })
  21. wx.showLoading();
  22. if (options.s && options.s == 'fm') {
  23. //fm请求过来 后登录openid然后再拉
  24. //接口根地址
  25. var apiUrl = "https://apismini.mingchengque.top/api";
  26. //登录授权
  27. wx.login({
  28. success: function (res) {
  29. if (res.code) {
  30. console.log("code=" + res.code);
  31. var openid = wx.getStorageSync("jscode2session");
  32. if (typeof openid !== 'undefined' && openid != 'undefined' && openid != '') {
  33. //发起网络请求 获得 prepayid 对接核心部分
  34. wx.request({
  35. url: apiUrl + '/wxminjsapi',
  36. data: {
  37. 'openid': openid,
  38. 'orderId': options.orderId
  39. },
  40. 'success': function (res) {
  41. if (!res.data.data.package) {
  42. wx.showModal({
  43. title: "支付拉起失败",
  44. content: "请检查JSAPI配置,入有疑问可联系客服" + JSON.stringify(res.data.data)
  45. });
  46. wx.exitMiniProgram({
  47. success: function () {
  48. console.log("退出成功");
  49. }
  50. });
  51. return;
  52. }
  53. wx.requestPayment({
  54. appId: res.data.data.appid,
  55. timeStamp: res.data.data.timestamp + "",
  56. nonceStr: res.data.data.nonceStr,
  57. package: res.data.data.package,
  58. paySign: res.data.data.paysign,
  59. signType: "RSA",
  60. success: function (wxres) {
  61. wx.exitMiniProgram({
  62. success: function () {
  63. console.log("退出成功");
  64. },
  65. fail: function () {
  66. console.log("退出失败");
  67. },
  68. complete: function () {
  69. console.log("调用结束");
  70. }
  71. });
  72. },
  73. fail: function (res) {
  74. wx.exitMiniProgram({
  75. success: function () {
  76. console.log("退出成功");
  77. }
  78. });
  79. console.log(res);
  80. }
  81. })
  82. },
  83. 'fail': function (res) {
  84. wx.showModal({
  85. title: "支付拉起失败",
  86. content: ",请检查JSAPI配置,入有疑问可联系客服" + JSON.stringify(res)
  87. });
  88. wx.exitMiniProgram({
  89. success: function () {
  90. console.log("退出成功");
  91. }
  92. });
  93. }
  94. })
  95. //发起网络请求 获得 prepayid 对接核心部分 end
  96. } else {
  97. wx.request({
  98. url: apiUrl + '/jscode2session',
  99. //code获取openid
  100. 'data': {
  101. 'orderId': options.orderId,
  102. 'code': res.code
  103. },
  104. dataType: 'json',
  105. success: function (res) {
  106. wx.setStorage({
  107. key: "jscode2session",
  108. data: res.data.data.openid
  109. })
  110. //发起网络请求 获得 prepayid 对接核心部分
  111. wx.request({
  112. url: apiUrl + '/wxminjsapi',
  113. data: {
  114. 'openid': res.data.data.openid,
  115. 'orderId': options.orderId
  116. },
  117. 'success': function (res) {
  118. debugger
  119. if (!res.data.data.package) {
  120. wx.showToast({
  121. title: '支付拉起失败,请检查JSAPI配置,入有疑问可联系客服',
  122. icon: 'none',
  123. duration: 2000
  124. })
  125. wx.exitMiniProgram({
  126. success: function () {
  127. console.log("退出成功");
  128. }
  129. });
  130. return;
  131. }
  132. wx.requestPayment({
  133. appId: res.data.data.appid,
  134. timeStamp: res.data.data.timestamp + "",
  135. nonceStr: res.data.data.nonceStr,
  136. package: res.data.data.package,
  137. paySign: res.data.data.paysign,
  138. signType: "RSA",
  139. success: function (wxres) {
  140. wx.exitMiniProgram({
  141. success: function () {
  142. console.log("退出成功");
  143. }
  144. });
  145. },
  146. fail: function (res) {
  147. console.log(res);
  148. }
  149. })
  150. }
  151. })
  152. //发起网络请求 获得 prepayid 对接核心部分 end
  153. }
  154. });
  155. }
  156. } else {
  157. //console.log('获取用户登录态失败!' + res.errMsg)
  158. }
  159. }
  160. })
  161. }
  162. wx.hideLoading();
  163. },
  164. /**
  165. * 生命周期函数--监听页面显示
  166. */
  167. onShow: function () {
  168. },
  169. /**
  170. * 生命周期函数--监听页面隐藏
  171. */
  172. onHide: function () {
  173. },
  174. })
payfm.json
  1. {
  2. "navigationBarTitleText": "支付中"
  3. }
app.json

小程序项目的app.json文件中增加payfm的路径,注意不要放在pages数组的第1行,否则小程序容易因为“功能体验不完整”被驳回哟。

  1. {
  2. "pages": [
  3. //其他已存在代码保持不变
  4. "lionfish_comshop/pages/user/payfm"
  5. //其他已存在代码保持不变
  6. ]
  7. }