pay.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // 特殊逻辑:微信公众号、小程序支付时,必须传入 openid
  91. if (['wx_pub'].includes(channel)) {
  92. const openid = await sheep.$platform.useProvider('wechat').getOpenid();
  93. // 如果获取不到 openid,微信无法发起支付,此时需要引导
  94. if (!openid) {
  95. this.bindWeixin();
  96. return;
  97. }
  98. data.channelExtras.openid = openid;
  99. }
  100. PayOrderApi.submitOrder(data).then((res) => {
  101. // 成功时
  102. res.code === 0 && resolve(res);
  103. // 失败时
  104. // TODO 芋艿:这块需要在测试下哈;
  105. if (res.code !== 0 && res.msg === 'miss_openid') {
  106. this.bindWeixin();
  107. }
  108. });
  109. });
  110. }
  111. // #ifdef H5
  112. // 微信公众号 JSSDK 支付
  113. async wechatOfficialAccountPay() {
  114. let { code, data } = await this.prepay('wx_pub');
  115. if (code !== 0) {
  116. return;
  117. }
  118. // let that = this;
  119. $wxsdk.wxpay(data, {
  120. success: () => {
  121. this.payResult('success');
  122. },
  123. cancel: () => {
  124. this.$helper.toast('支付已手动取消');
  125. },
  126. fail: () => {
  127. this.payResult('fail');
  128. },
  129. });
  130. }
  131. // 浏览器微信H5支付 TODO 芋艿:待接入
  132. async wechatWapPay() {
  133. const { error, data } = await this.prepay();
  134. if (error === 0) {
  135. const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment
  136. }&orderType=${this.orderType}`;
  137. location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`;
  138. }
  139. }
  140. // 支付链接 TODO 芋艿:待接入
  141. async redirectPay() {
  142. let { error, data } = await this.prepay();
  143. if (error === 0) {
  144. const redirect_url = `${getRootUrl()}pages/pay/result?id=${this.id}&payment=${this.payment
  145. }&orderType=${this.orderType}`;
  146. location.href = data.pay_data + encodeURIComponent(redirect_url);
  147. }
  148. }
  149. // #endif
  150. // 微信小程序支付 TODO 芋艿:待接入
  151. async wechatMiniProgramPay() {
  152. let that = this;
  153. let result = await this.prepay();
  154. uni.requestPayment({
  155. provider: 'wxpay',
  156. ...result.data.pay_data,
  157. success: (res) => {
  158. that.payResult('success');
  159. },
  160. fail: (err) => {
  161. if (err.errMsg === 'requestPayment:fail cancel') {
  162. sheep.$helper.toast('支付已手动取消');
  163. } else {
  164. that.payResult('fail');
  165. }
  166. },
  167. });
  168. }
  169. // 余额支付
  170. async walletPay() {
  171. const { code } = await this.prepay('wallet');
  172. code === 0 && this.payResult('success');
  173. }
  174. // 模拟支付
  175. async mockPay() {
  176. const { code } = await this.prepay('mock');
  177. code === 0 && this.payResult('success');
  178. }
  179. // 支付宝复制链接支付 TODO 芋艿:待接入
  180. async copyPayLink() {
  181. let that = this;
  182. let { error, data } = await this.prepay();
  183. if (error === 0) {
  184. // 引入showModal 点击确认 复制链接;
  185. uni.showModal({
  186. title: '支付宝支付',
  187. content: '复制链接到外部浏览器',
  188. confirmText: '复制链接',
  189. success: (res) => {
  190. if (res.confirm) {
  191. sheep.$helper.copyText(data.pay_data);
  192. }
  193. },
  194. });
  195. }
  196. }
  197. // 支付宝支付 TODO 芋艿:待接入
  198. async alipay() {
  199. let that = this;
  200. const { error, data } = await this.prepay();
  201. if (error === 0) {
  202. uni.requestPayment({
  203. provider: 'alipay',
  204. orderInfo: data.pay_data, //支付宝订单数据
  205. success: (res) => {
  206. that.payResult('success');
  207. },
  208. fail: (err) => {
  209. if (err.errMsg === 'requestPayment:fail [paymentAlipay:62001]user cancel') {
  210. sheep.$helper.toast('支付已手动取消');
  211. } else {
  212. that.payResult('fail');
  213. }
  214. },
  215. });
  216. }
  217. }
  218. // 微信支付 TODO 芋艿:待接入
  219. async wechatAppPay() {
  220. let that = this;
  221. let { error, data } = await this.prepay();
  222. if (error === 0) {
  223. uni.requestPayment({
  224. provider: 'wxpay',
  225. orderInfo: data.pay_data, //微信订单数据(官方说是string。实测为object)
  226. success: (res) => {
  227. that.payResult('success');
  228. },
  229. fail: (err) => {
  230. err.errMsg !== 'requestPayment:fail cancel' && that.payResult('fail');
  231. },
  232. });
  233. }
  234. }
  235. // 支付结果跳转,success:成功,fail:失败
  236. payResult(resultType) {
  237. sheep.$router.redirect('/pages/pay/result', {
  238. id: this.id,
  239. orderType: this.orderType,
  240. payState: resultType
  241. });
  242. }
  243. // 引导绑定微信
  244. bindWeixin() {
  245. uni.showModal({
  246. title: '微信支付',
  247. content: '请先绑定微信再使用微信支付',
  248. success: function (res) {
  249. if (res.confirm) {
  250. sheep.$platform.useProvider('wechat').bind();
  251. }
  252. },
  253. });
  254. }
  255. }
  256. export function getPayMethods(channels) {
  257. const payMethods = [
  258. {
  259. icon: '/static/img/shop/pay/wechat.png',
  260. title: '微信支付',
  261. value: 'wechat',
  262. disabled: true,
  263. },
  264. {
  265. icon: '/static/img/shop/pay/alipay.png',
  266. title: '支付宝支付',
  267. value: 'alipay',
  268. disabled: true,
  269. },
  270. {
  271. icon: '/static/img/shop/pay/wallet.png',
  272. title: '余额支付',
  273. value: 'wallet',
  274. disabled: true,
  275. },
  276. {
  277. icon: '/static/img/shop/pay/apple.png',
  278. title: 'Apple Pay',
  279. value: 'apple',
  280. disabled: true,
  281. },
  282. {
  283. icon: '/static/img/shop/pay/wallet.png',
  284. title: '模拟支付',
  285. value: 'mock',
  286. disabled: true,
  287. }
  288. ];
  289. const platform = sheep.$platform.name
  290. // 1. 处理【微信支付】
  291. const wechatMethod = payMethods[0];
  292. if ((platform === 'WechatOfficialAccount' && channels.includes('wx_pub'))
  293. || (platform === 'WechatMiniProgram' && channels.includes('wx_lite'))
  294. || (platform === 'App' && channels.includes('wx_app'))) {
  295. wechatMethod.disabled = false;
  296. }
  297. wechatMethod.disabled = false; // TODO 芋艿:临时测试
  298. // 2. 处理【支付宝支付】
  299. const alipayMethod = payMethods[1];
  300. if ((platform === 'WechatOfficialAccount' && channels.includes('alipay_wap'))
  301. || platform === 'WechatMiniProgram' && channels.includes('alipay_wap')
  302. || platform === 'App' && channels.includes('alipay_app')) {
  303. alipayMethod.disabled = false;
  304. }
  305. // 3. 处理【余额支付】
  306. const walletMethod = payMethods[2];
  307. if (channels.includes('wallet')) {
  308. walletMethod.disabled = false;
  309. }
  310. // 4. 处理【苹果支付】TODO 芋艿:未来接入
  311. // 5. 处理【模拟支付】
  312. const mockMethod = payMethods[4];
  313. if (channels.includes('mock')) {
  314. mockMethod.disabled = false;
  315. }
  316. return payMethods;
  317. }