pay.js 8.3 KB

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