pay.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. const openid = await sheep_index.sheep.$platform.useProvider("wechat").getOpenid(true);
  84. if (!openid) {
  85. this.bindWeixin();
  86. return;
  87. }
  88. data.channelExtras.openid = openid;
  89. }
  90. sheep_api_pay_order.PayOrderApi.submitOrder(data).then((res) => {
  91. res.code === 0 && resolve(res);
  92. if (res.code !== 0 && res.msg.indexOf("无效的openid") >= 0) {
  93. if (res.msg.indexOf("无效的openid") >= 0 || res.msg.indexOf("下单账号与支付账号不一致") >= 0) {
  94. this.bindWeixin();
  95. }
  96. }
  97. });
  98. });
  99. }
  100. // 微信小程序支付
  101. async wechatMiniProgramPay() {
  102. let { code, data } = await this.prepay("wx_lite");
  103. if (code !== 0) {
  104. return;
  105. }
  106. const payConfig = JSON.parse(data.displayContent);
  107. common_vendor.index.requestPayment({
  108. provider: "wxpay",
  109. timeStamp: payConfig.timeStamp,
  110. nonceStr: payConfig.nonceStr,
  111. package: payConfig.packageValue,
  112. signType: payConfig.signType,
  113. paySign: payConfig.paySign,
  114. success: (res) => {
  115. this.payResult("success");
  116. },
  117. fail: (err) => {
  118. if (err.errMsg === "requestPayment:fail cancel") {
  119. sheep_index.sheep.$helper.toast("支付已手动取消");
  120. } else {
  121. this.payResult("fail");
  122. }
  123. }
  124. });
  125. }
  126. // 余额支付
  127. async walletPay() {
  128. const { code } = await this.prepay("wallet");
  129. code === 0 && this.payResult("success");
  130. }
  131. // 模拟支付
  132. async mockPay() {
  133. const { code } = await this.prepay("mock");
  134. code === 0 && this.payResult("success");
  135. }
  136. // 支付宝复制链接支付 TODO 非繁人:待接入
  137. async copyPayLink() {
  138. let { error, data } = await this.prepay();
  139. if (error === 0) {
  140. common_vendor.index.showModal({
  141. title: "支付宝支付",
  142. content: "复制链接到外部浏览器",
  143. confirmText: "复制链接",
  144. success: (res) => {
  145. if (res.confirm) {
  146. sheep_index.sheep.$helper.copyText(data.pay_data);
  147. }
  148. }
  149. });
  150. }
  151. }
  152. // 支付宝支付 TODO 非繁人:待接入
  153. async alipay() {
  154. let that = this;
  155. const { error, data } = await this.prepay();
  156. if (error === 0) {
  157. common_vendor.index.requestPayment({
  158. provider: "alipay",
  159. orderInfo: data.pay_data,
  160. //支付宝订单数据
  161. success: (res) => {
  162. that.payResult("success");
  163. },
  164. fail: (err) => {
  165. if (err.errMsg === "requestPayment:fail [paymentAlipay:62001]user cancel") {
  166. sheep_index.sheep.$helper.toast("支付已手动取消");
  167. } else {
  168. that.payResult("fail");
  169. }
  170. }
  171. });
  172. }
  173. }
  174. // 微信支付 TODO 非繁人:待接入
  175. async wechatAppPay() {
  176. let that = this;
  177. let { error, data } = await this.prepay();
  178. if (error === 0) {
  179. common_vendor.index.requestPayment({
  180. provider: "wxpay",
  181. orderInfo: data.pay_data,
  182. //微信订单数据(官方说是string。实测为object)
  183. success: (res) => {
  184. that.payResult("success");
  185. },
  186. fail: (err) => {
  187. err.errMsg !== "requestPayment:fail cancel" && that.payResult("fail");
  188. }
  189. });
  190. }
  191. }
  192. // 支付结果跳转,success:成功,fail:失败
  193. payResult(resultType) {
  194. sheep_index.sheep.$router.redirect("/pages/pay/result", {
  195. id: this.id,
  196. orderType: this.orderType,
  197. payState: resultType
  198. });
  199. }
  200. // 引导绑定微信
  201. bindWeixin() {
  202. common_vendor.index.showModal({
  203. title: "微信支付",
  204. content: "请先绑定微信再使用微信支付",
  205. confirmText: "绑定",
  206. success: function(res) {
  207. if (res.confirm) {
  208. sheep_index.sheep.$platform.useProvider("wechat").bind();
  209. }
  210. }
  211. });
  212. }
  213. }
  214. function getPayMethods(channels) {
  215. const payMethods = [
  216. {
  217. icon: "/static/img/shop/pay/wechat.png",
  218. title: "微信支付",
  219. value: "wechat",
  220. disabled: true
  221. },
  222. {
  223. icon: "/static/img/shop/pay/alipay.png",
  224. title: "支付宝支付",
  225. value: "alipay",
  226. disabled: true
  227. }
  228. // 20240420注释
  229. // {
  230. // icon: '/static/img/shop/pay/wallet.png',
  231. // title: '余额支付',
  232. // value: 'wallet',
  233. // disabled: true,
  234. // },
  235. // {
  236. // icon: '/static/img/shop/pay/apple.png',
  237. // title: 'Apple Pay',
  238. // value: 'apple',
  239. // disabled: true,
  240. // },
  241. // {
  242. // icon: '/static/img/shop/pay/wallet.png',
  243. // title: '模拟支付',
  244. // value: 'mock',
  245. // disabled: true,
  246. // }
  247. ];
  248. const platform = sheep_index.sheep.$platform.name;
  249. const wechatMethod = payMethods[0];
  250. if (platform === "WechatOfficialAccount" && channels.includes("wx_pub") || platform === "WechatMiniProgram" && channels.includes("wx_lite") || platform === "App" && channels.includes("wx_app")) {
  251. wechatMethod.disabled = false;
  252. }
  253. wechatMethod.disabled = false;
  254. const alipayMethod = payMethods[1];
  255. if (platform === "WechatOfficialAccount" && channels.includes("alipay_wap") || platform === "WechatMiniProgram" && channels.includes("alipay_wap") || platform === "App" && channels.includes("alipay_app")) {
  256. alipayMethod.disabled = false;
  257. }
  258. return payMethods;
  259. }
  260. exports.SheepPay = SheepPay;
  261. exports.getPayMethods = getPayMethods;