pay.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const sheep_index = require("../index.js");
  4. require("../helper/index.js");
  5. const sheep_api_pay_order = require("../api/pay/order.js");
  6. class SheepPay {
  7. constructor(payment, orderType, id) {
  8. this.payment = payment;
  9. this.id = id;
  10. this.orderType = orderType;
  11. this.payAction();
  12. }
  13. payAction() {
  14. const payAction = {
  15. WechatOfficialAccount: {
  16. wechat: () => {
  17. this.wechatOfficialAccountPay();
  18. },
  19. alipay: () => {
  20. this.redirectPay();
  21. },
  22. wallet: () => {
  23. this.walletPay();
  24. },
  25. mock: () => {
  26. this.mockPay();
  27. }
  28. },
  29. WechatMiniProgram: {
  30. wechat: () => {
  31. this.wechatMiniProgramPay();
  32. },
  33. alipay: () => {
  34. this.copyPayLink();
  35. },
  36. wallet: () => {
  37. this.walletPay();
  38. },
  39. mock: () => {
  40. this.mockPay();
  41. }
  42. },
  43. App: {
  44. wechat: () => {
  45. this.wechatAppPay();
  46. },
  47. alipay: () => {
  48. this.alipay();
  49. },
  50. wallet: () => {
  51. this.walletPay();
  52. },
  53. mock: () => {
  54. this.mockPay();
  55. }
  56. },
  57. H5: {
  58. wechat: () => {
  59. this.wechatWapPay();
  60. },
  61. alipay: () => {
  62. this.redirectPay();
  63. },
  64. wallet: () => {
  65. this.walletPay();
  66. },
  67. mock: () => {
  68. this.mockPay();
  69. }
  70. }
  71. };
  72. return payAction[sheep_index.sheep.$platform.name][this.payment]();
  73. }
  74. // 预支付
  75. prepay(channel) {
  76. return new Promise(async (resolve, reject) => {
  77. let data = {
  78. id: this.id,
  79. channelCode: channel,
  80. channelExtras: {}
  81. };
  82. if (["wx_pub", "wx_lite"].includes(channel)) {
  83. console.log(sheep_index.sheep.$platform.useProvider("wechat").getOpenid);
  84. const openid = await sheep_index.sheep.$platform.useProvider("wechat").getOpenid(true);
  85. if (!openid) {
  86. this.bindWeixin();
  87. return;
  88. }
  89. data.channelExtras.openid = openid;
  90. }
  91. sheep_api_pay_order.PayOrderApi.submitOrder(data).then((res) => {
  92. res.code === 0 && resolve(res);
  93. if (res.code !== 0 && res.msg.indexOf("无效的openid") >= 0) {
  94. if (res.msg.indexOf("无效的openid") >= 0 || res.msg.indexOf("下单账号与支付账号不一致") >= 0) {
  95. this.bindWeixin();
  96. }
  97. }
  98. });
  99. });
  100. }
  101. // 微信小程序支付
  102. async wechatMiniProgramPay() {
  103. let { code, data } = await this.prepay("wx_lite");
  104. if (code !== 0) {
  105. return;
  106. }
  107. const payConfig = JSON.parse(data.displayContent);
  108. common_vendor.index.requestPayment({
  109. provider: "wxpay",
  110. timeStamp: payConfig.timeStamp,
  111. nonceStr: payConfig.nonceStr,
  112. package: payConfig.packageValue,
  113. signType: payConfig.signType,
  114. paySign: payConfig.paySign,
  115. success: (res) => {
  116. this.payResult("success");
  117. },
  118. fail: (err) => {
  119. if (err.errMsg === "requestPayment:fail cancel") {
  120. sheep_index.sheep.$helper.toast("支付已手动取消");
  121. } else {
  122. this.payResult("fail");
  123. }
  124. }
  125. });
  126. }
  127. // 余额支付
  128. async walletPay() {
  129. const { code } = await this.prepay("wallet");
  130. code === 0 && this.payResult("success");
  131. }
  132. // 模拟支付
  133. async mockPay() {
  134. const { code } = await this.prepay("mock");
  135. code === 0 && this.payResult("success");
  136. }
  137. // 支付宝复制链接支付 TODO 非繁人:待接入
  138. async copyPayLink() {
  139. let { error, data } = await this.prepay();
  140. if (error === 0) {
  141. common_vendor.index.showModal({
  142. title: "支付宝支付",
  143. content: "复制链接到外部浏览器",
  144. confirmText: "复制链接",
  145. success: (res) => {
  146. if (res.confirm) {
  147. sheep_index.sheep.$helper.copyText(data.pay_data);
  148. }
  149. }
  150. });
  151. }
  152. }
  153. // 支付宝支付 TODO 非繁人:待接入
  154. async alipay() {
  155. let that = this;
  156. const { error, data } = await this.prepay();
  157. if (error === 0) {
  158. common_vendor.index.requestPayment({
  159. provider: "alipay",
  160. orderInfo: data.pay_data,
  161. //支付宝订单数据
  162. success: (res) => {
  163. that.payResult("success");
  164. },
  165. fail: (err) => {
  166. if (err.errMsg === "requestPayment:fail [paymentAlipay:62001]user cancel") {
  167. sheep_index.sheep.$helper.toast("支付已手动取消");
  168. } else {
  169. that.payResult("fail");
  170. }
  171. }
  172. });
  173. }
  174. }
  175. // 微信支付 TODO 非繁人:待接入
  176. async wechatAppPay() {
  177. let that = this;
  178. let { error, data } = await this.prepay();
  179. if (error === 0) {
  180. common_vendor.index.requestPayment({
  181. provider: "wxpay",
  182. orderInfo: data.pay_data,
  183. //微信订单数据(官方说是string。实测为object)
  184. success: (res) => {
  185. that.payResult("success");
  186. },
  187. fail: (err) => {
  188. err.errMsg !== "requestPayment:fail cancel" && that.payResult("fail");
  189. }
  190. });
  191. }
  192. }
  193. // 支付结果跳转,success:成功,fail:失败
  194. payResult(resultType) {
  195. sheep_index.sheep.$router.redirect("/pages/pay/result", {
  196. id: this.id,
  197. orderType: this.orderType,
  198. payState: resultType
  199. });
  200. }
  201. // 引导绑定微信
  202. bindWeixin() {
  203. common_vendor.index.showModal({
  204. title: "微信支付",
  205. content: "请先绑定微信再使用微信支付",
  206. confirmText: "绑定",
  207. success: function(res) {
  208. if (res.confirm) {
  209. sheep_index.sheep.$platform.useProvider("wechat").bind();
  210. }
  211. }
  212. });
  213. }
  214. }
  215. function getPayMethods(channels) {
  216. const payMethods = [
  217. {
  218. icon: "/static/img/shop/pay/wechat.png",
  219. title: "微信支付",
  220. value: "wechat",
  221. disabled: true
  222. },
  223. {
  224. icon: "/static/img/shop/pay/alipay.png",
  225. title: "支付宝支付",
  226. value: "alipay",
  227. disabled: true
  228. }
  229. // 20240420注释
  230. // {
  231. // icon: '/static/img/shop/pay/wallet.png',
  232. // title: '余额支付',
  233. // value: 'wallet',
  234. // disabled: true,
  235. // },
  236. // {
  237. // icon: '/static/img/shop/pay/apple.png',
  238. // title: 'Apple Pay',
  239. // value: 'apple',
  240. // disabled: true,
  241. // },
  242. // {
  243. // icon: '/static/img/shop/pay/wallet.png',
  244. // title: '模拟支付',
  245. // value: 'mock',
  246. // disabled: true,
  247. // }
  248. ];
  249. const platform = sheep_index.sheep.$platform.name;
  250. const wechatMethod = payMethods[0];
  251. if (platform === "WechatOfficialAccount" && channels.includes("wx_pub") || platform === "WechatMiniProgram" && channels.includes("wx_lite") || platform === "App" && channels.includes("wx_app")) {
  252. wechatMethod.disabled = false;
  253. }
  254. wechatMethod.disabled = false;
  255. const alipayMethod = payMethods[1];
  256. if (platform === "WechatOfficialAccount" && channels.includes("alipay_wap") || platform === "WechatMiniProgram" && channels.includes("alipay_wap") || platform === "App" && channels.includes("alipay_app")) {
  257. alipayMethod.disabled = false;
  258. }
  259. return payMethods;
  260. }
  261. exports.SheepPay = SheepPay;
  262. exports.getPayMethods = getPayMethods;