pay.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import sheep from '@/sheep';
  2. // #ifdef H5
  3. import $wxsdk from '@/sheep/libs/sdk-h5-weixin';
  4. // #endif
  5. import { getRootUrl } from '@/sheep/helper';
  6. import PayOrderApi from '@/sheep/api/pay/order';
  7. /**
  8. * 支付
  9. *
  10. * @param {String} payment = ['wechat','alipay','wallet','mock'] - 支付方式
  11. * @param {String} orderType = ['goods','recharge','groupon'] - 订单类型
  12. * @param {String} id - 订单号
  13. */
  14. export default class SheepPay {
  15. constructor(payment, orderType, id) {
  16. this.payment = payment;
  17. this.id = id;
  18. this.orderType = orderType;
  19. this.payAction();
  20. }
  21. payAction() {
  22. const payAction = {
  23. WechatOfficialAccount: {
  24. wechat: () => {
  25. this.wechatOfficialAccountPay();
  26. },
  27. alipay: () => {
  28. this.redirectPay(); // 现在公众号可以直接跳转支付宝页面
  29. },
  30. wallet: () => {
  31. this.walletPay();
  32. },
  33. mock: () => {
  34. this.mockPay();
  35. }
  36. },
  37. WechatMiniProgram: {
  38. wechat: () => {
  39. this.wechatMiniProgramPay();
  40. },
  41. alipay: () => {
  42. this.copyPayLink();
  43. },
  44. wallet: () => {
  45. this.walletPay();
  46. },
  47. mock: () => {
  48. this.mockPay();
  49. }
  50. },
  51. App: {
  52. wechat: () => {
  53. this.wechatAppPay();
  54. },
  55. alipay: () => {
  56. this.alipay();
  57. },
  58. wallet: () => {
  59. this.walletPay();
  60. },
  61. mock: () => {
  62. this.mockPay();
  63. }
  64. },
  65. H5: {
  66. wechat: () => {
  67. this.wechatWapPay();
  68. },
  69. alipay: () => {
  70. this.redirectPay();
  71. },
  72. wallet: () => {
  73. this.walletPay();
  74. },
  75. mock: () => {
  76. this.mockPay();
  77. }
  78. },
  79. };
  80. return payAction[sheep.$platform.name][this.payment]();
  81. }
  82. // 预支付
  83. prepay(channel) {
  84. return new Promise(async (resolve, reject) => {
  85. let data = {
  86. id: this.id,
  87. channelCode: channel,
  88. channelExtras: {}
  89. };
  90. console.log(data)
  91. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid
  92. if (['wx_pub', 'wx_lite'].includes(channel)) {
  93. const openid = await sheep.$platform.useProvider('wechat').getOpenid(true);
  94. console.log(openid)
  95. // 如果获取不到 openid,微信无法发起支付,此时需要引导
  96. if (!openid) {
  97. this.bindWeixin();
  98. return;
  99. }
  100. data.channelExtras.openid = openid;
  101. }
  102. // 发起预支付 API 调用
  103. PayOrderApi.submitOrder(data).then((res) => {
  104. // 成功时
  105. res.code === 0 && resolve(res);
  106. // 失败时
  107. if (res.code !== 0 && res.msg.indexOf('无效的openid') >= 0) {
  108. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid 不正确的情况
  109. if (res.msg.indexOf('无效的openid') >= 0 // 获取的 openid 不正确时,或者随便输入了个 openid
  110. || res.msg.indexOf('下单账号与支付账号不一致') >= 0) { // https://developers.weixin.qq.com/community/develop/doc/00008c53c347804beec82aed051c00
  111. this.bindWeixin();
  112. }
  113. }
  114. });
  115. });
  116. }
  117. // #ifdef H5
  118. // 微信公众号 JSSDK 支付
  119. async wechatOfficialAccountPay() {
  120. let { code, data } = await this.prepay('wx_pub');
  121. if (code !== 0) {
  122. return;
  123. }
  124. const payConfig = JSON.parse(data.displayContent);
  125. $wxsdk.wxpay(payConfig, {
  126. success: () => {
  127. this.payResult('success');
  128. },
  129. cancel: () => {
  130. sheep.$helper.toast('支付已手动取消');
  131. },
  132. fail: (error) => {
  133. if (error.errMsg.indexOf('chooseWXPay:没有此SDK或暂不支持此SDK模拟') >= 0) {
  134. sheep.$helper.toast('发起微信支付失败,原因:可能是微信开发者工具不支持,建议使用微信打开网页后支付');
  135. return
  136. }
  137. this.payResult('fail');
  138. },
  139. });
  140. }
  141. // 浏览器微信 H5 支付 TODO 非繁人:待接入
  142. async wechatWapPay() {
  143. const { error, data } = await this.prepay();
  144. if (error === 0) {
  145. const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment}&orderType=${this.orderType}`;
  146. location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`;
  147. }
  148. }
  149. // 支付链接 TODO 非繁人:待接入
  150. async redirectPay() {
  151. let { error, data } = await this.prepay();
  152. if (error === 0) {
  153. const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment}&orderType=${this.orderType}`;
  154. location.href = data.pay_data + encodeURIComponent(redirect_url);
  155. }
  156. }
  157. // #endif
  158. // 微信小程序支付
  159. async wechatMiniProgramPay() {
  160. // let that = this;
  161. let { code, data } = await this.prepay('wx_lite');
  162. if (code !== 0) {
  163. return;
  164. }
  165. // 调用微信小程序支付
  166. const payConfig = JSON.parse(data.displayContent);
  167. uni.requestPayment({
  168. provider: 'wxpay',
  169. timeStamp: payConfig.timeStamp,
  170. nonceStr: payConfig.nonceStr,
  171. package: payConfig.packageValue,
  172. signType: payConfig.signType,
  173. paySign: payConfig.paySign,
  174. success: (res) => {
  175. this.payResult('success');
  176. },
  177. fail: (err) => {
  178. if (err.errMsg === 'requestPayment:fail cancel') {
  179. sheep.$helper.toast('支付已手动取消');
  180. } else {
  181. this.payResult('fail');
  182. }
  183. },
  184. });
  185. }
  186. // 余额支付
  187. async walletPay() {
  188. const { code } = await this.prepay('wallet');
  189. code === 0 && this.payResult('success');
  190. }
  191. // 模拟支付
  192. async mockPay() {
  193. const { code } = await this.prepay('mock');
  194. code === 0 && this.payResult('success');
  195. }
  196. // 支付宝复制链接支付 TODO 非繁人:待接入
  197. async copyPayLink() {
  198. let that = this;
  199. let { error, data } = await this.prepay();
  200. if (error === 0) {
  201. // 引入showModal 点击确认 复制链接;
  202. uni.showModal({
  203. title: '支付宝支付',
  204. content: '复制链接到外部浏览器',
  205. confirmText: '复制链接',
  206. success: (res) => {
  207. if (res.confirm) {
  208. sheep.$helper.copyText(data.pay_data);
  209. }
  210. },
  211. });
  212. }
  213. }
  214. // 支付宝支付 TODO 非繁人:待接入
  215. async alipay() {
  216. let that = this;
  217. const { error, data } = await this.prepay();
  218. if (error === 0) {
  219. uni.requestPayment({
  220. provider: 'alipay',
  221. orderInfo: data.pay_data, //支付宝订单数据
  222. success: (res) => {
  223. that.payResult('success');
  224. },
  225. fail: (err) => {
  226. if (err.errMsg === 'requestPayment:fail [paymentAlipay:62001]user cancel') {
  227. sheep.$helper.toast('支付已手动取消');
  228. } else {
  229. that.payResult('fail');
  230. }
  231. },
  232. });
  233. }
  234. }
  235. // 微信支付 TODO 非繁人:待接入
  236. async wechatAppPay() {
  237. let that = this;
  238. let { error, data } = await this.prepay();
  239. if (error === 0) {
  240. uni.requestPayment({
  241. provider: 'wxpay',
  242. orderInfo: data.pay_data, //微信订单数据(官方说是string。实测为object)
  243. success: (res) => {
  244. that.payResult('success');
  245. },
  246. fail: (err) => {
  247. err.errMsg !== 'requestPayment:fail cancel' && that.payResult('fail');
  248. },
  249. });
  250. }
  251. }
  252. // 支付结果跳转,success:成功,fail:失败
  253. payResult(resultType) {
  254. sheep.$router.redirect('/pages/pay/result', {
  255. id: this.id,
  256. orderType: this.orderType,
  257. payState: resultType
  258. });
  259. }
  260. // 引导绑定微信
  261. bindWeixin() {
  262. uni.showModal({
  263. title: '微信支付',
  264. content: '请先绑定微信再使用微信支付',
  265. confirmText:'绑定',
  266. success: function (res) {
  267. if (res.confirm) {
  268. sheep.$platform.useProvider('wechat').bind();
  269. }
  270. },
  271. });
  272. }
  273. }
  274. export function getPayMethods(channels) {
  275. const payMethods = [
  276. {
  277. icon: '/static/img/shop/pay/wechat.png',
  278. title: '微信支付',
  279. value: 'wechat',
  280. disabled: true,
  281. },
  282. {
  283. icon: '/static/img/shop/pay/alipay.png',
  284. title: '支付宝支付',
  285. value: 'alipay',
  286. disabled: true,
  287. },
  288. // 20240420注释
  289. // {
  290. // icon: '/static/img/shop/pay/wallet.png',
  291. // title: '余额支付',
  292. // value: 'wallet',
  293. // disabled: true,
  294. // },
  295. // {
  296. // icon: '/static/img/shop/pay/apple.png',
  297. // title: 'Apple Pay',
  298. // value: 'apple',
  299. // disabled: true,
  300. // },
  301. // {
  302. // icon: '/static/img/shop/pay/wallet.png',
  303. // title: '模拟支付',
  304. // value: 'mock',
  305. // disabled: true,
  306. // }
  307. ];
  308. const platform = sheep.$platform.name
  309. // 1. 处理【微信支付】
  310. const wechatMethod = payMethods[0];
  311. if ((platform === 'WechatOfficialAccount' && channels.includes('wx_pub'))
  312. || (platform === 'WechatMiniProgram' && channels.includes('wx_lite'))
  313. || (platform === 'App' && channels.includes('wx_app'))) {
  314. wechatMethod.disabled = false;
  315. }
  316. wechatMethod.disabled = false; // TODO 非繁人:临时测试
  317. // 2. 处理【支付宝支付】
  318. const alipayMethod = payMethods[1];
  319. if ((platform === 'WechatOfficialAccount' && channels.includes('alipay_wap'))
  320. || platform === 'WechatMiniProgram' && channels.includes('alipay_wap')
  321. || platform === 'App' && channels.includes('alipay_app')) {
  322. alipayMethod.disabled = false;
  323. }
  324. // 3. 处理【余额支付】
  325. // const walletMethod = payMethods[2];
  326. // if (channels.includes('wallet')) {
  327. // walletMethod.disabled = false;
  328. // }
  329. // 4. 处理【苹果支付】TODO 非繁人:未来接入
  330. // 5. 处理【模拟支付】
  331. // const mockMethod = payMethods[4];
  332. // if (channels.includes('mock')) {
  333. // mockMethod.disabled = false;
  334. // }
  335. return payMethods;
  336. }