pay.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /**
  7. * 支付
  8. *
  9. * @param {String} payment = ['wechat','alipay','wallet','offline'] - 支付方式
  10. * @param {String} orderType = ['goods','recharge','groupon'] - 订单类型
  11. * @param {String} orderSN - 订单号
  12. */
  13. export default class SheepPay {
  14. constructor(payment, orderType, orderSN) {
  15. this.payment = payment;
  16. this.orderSN = orderSN;
  17. this.orderType = orderType;
  18. this.payAction();
  19. }
  20. payAction() {
  21. const payAction = {
  22. WechatOfficialAccount: {
  23. wechat: () => {
  24. this.wechatOfficialAccountPay();
  25. },
  26. alipay: () => {
  27. this.redirectPay(); // 现在公众号可以直接跳转支付宝页面
  28. },
  29. money: () => {
  30. this.moneyPay();
  31. },
  32. offline: () => {
  33. this.offlinePay();
  34. }
  35. },
  36. WechatMiniProgram: {
  37. wechat: () => {
  38. this.wechatMiniProgramPay();
  39. },
  40. alipay: () => {
  41. this.copyPayLink();
  42. },
  43. money: () => {
  44. this.moneyPay();
  45. },
  46. offline: () => {
  47. this.offlinePay();
  48. }
  49. },
  50. App: {
  51. wechat: () => {
  52. this.wechatAppPay();
  53. },
  54. alipay: () => {
  55. this.alipay();
  56. },
  57. money: () => {
  58. this.moneyPay();
  59. },
  60. offline: () => {
  61. this.offlinePay();
  62. }
  63. },
  64. H5: {
  65. wechat: () => {
  66. this.wechatWapPay();
  67. },
  68. alipay: () => {
  69. this.redirectPay();
  70. },
  71. money: () => {
  72. this.moneyPay();
  73. },
  74. offline: () => {
  75. this.offlinePay();
  76. }
  77. },
  78. };
  79. return payAction[sheep.$platform.name][this.payment]();
  80. }
  81. // 预支付
  82. prepay() {
  83. return new Promise((resolve, reject) => {
  84. let data = {
  85. order_sn: this.orderSN,
  86. payment: this.payment,
  87. };
  88. if (uni.getStorageSync('openid')) {
  89. data.openid = uni.getStorageSync('openid');
  90. }
  91. sheep.$api.pay.prepay(data).then((res) => {
  92. res.error === 0 && resolve(res);
  93. if (res.error === -1 && res.msg === 'miss_openid') {
  94. uni.showModal({
  95. title: '微信支付',
  96. content: '请先绑定微信再使用微信支付',
  97. success: function (res) {
  98. if (res.confirm) {
  99. sheep.$platform.useProvider('wechat').bind();
  100. }
  101. },
  102. });
  103. }
  104. });
  105. });
  106. }
  107. // #ifdef H5
  108. // 微信公众号JSSDK支付
  109. async wechatOfficialAccountPay() {
  110. let that = this;
  111. let { error, data, msg } = await this.prepay();
  112. if (error !== 0) {
  113. console.log('支付错误', msg);
  114. return;
  115. }
  116. $wxsdk.wxpay(data.pay_data, {
  117. success: () => {
  118. that.payResult('success');
  119. },
  120. cancel: () => {
  121. sheep.$helper.toast('支付已手动取消');
  122. },
  123. fail: () => {
  124. that.payResult('fail');
  125. },
  126. });
  127. }
  128. //浏览器微信H5支付
  129. async wechatWapPay() {
  130. const { error, data } = await this.prepay();
  131. if (error === 0) {
  132. const redirect_url = `${getRootUrl()}pages/pay/result?orderSN=${this.orderSN}&payment=${this.payment
  133. }&orderType=${this.orderType}`;
  134. location.href = `${data.pay_data.h5_url}&redirect_url=${encodeURIComponent(redirect_url)}`;
  135. }
  136. }
  137. // 支付链接
  138. async redirectPay() {
  139. let { error, data } = await this.prepay();
  140. if (error === 0) {
  141. const redirect_url = `${getRootUrl()}pages/pay/result?orderSN=${this.orderSN}&payment=${this.payment
  142. }&orderType=${this.orderType}`;
  143. location.href = data.pay_data + encodeURIComponent(redirect_url);
  144. }
  145. }
  146. // #endif
  147. // 微信小程序支付
  148. async wechatMiniProgramPay() {
  149. let that = this;
  150. let result = await this.prepay();
  151. uni.requestPayment({
  152. provider: 'wxpay',
  153. ...result.data.pay_data,
  154. success: (res) => {
  155. that.payResult('success');
  156. },
  157. fail: (err) => {
  158. if (err.errMsg === 'requestPayment:fail cancel') {
  159. sheep.$helper.toast('支付已手动取消');
  160. } else {
  161. that.payResult('fail');
  162. }
  163. },
  164. });
  165. }
  166. // 余额支付
  167. async moneyPay() {
  168. const { error } = await this.prepay();
  169. error === 0 && this.payResult('success');
  170. }
  171. // 货到付款
  172. async offlinePay() {
  173. const { error } = await this.prepay();
  174. error === 0 && this.payResult('success');
  175. }
  176. // 支付宝复制链接支付
  177. async copyPayLink() {
  178. let that = this;
  179. let { error, data } = await this.prepay();
  180. if (error === 0) {
  181. // 引入showModal 点击确认 复制链接;
  182. uni.showModal({
  183. title: '支付宝支付',
  184. content: '复制链接到外部浏览器',
  185. confirmText: '复制链接',
  186. success: (res) => {
  187. if (res.confirm) {
  188. sheep.$helper.copyText(data.pay_data);
  189. }
  190. },
  191. });
  192. }
  193. }
  194. // 支付宝支付
  195. async alipay() {
  196. let that = this;
  197. const { error, data } = await this.prepay();
  198. if (error === 0) {
  199. uni.requestPayment({
  200. provider: 'alipay',
  201. orderInfo: data.pay_data, //支付宝订单数据
  202. success: (res) => {
  203. that.payResult('success');
  204. },
  205. fail: (err) => {
  206. if (err.errMsg === 'requestPayment:fail [paymentAlipay:62001]user cancel') {
  207. sheep.$helper.toast('支付已手动取消');
  208. } else {
  209. that.payResult('fail');
  210. }
  211. },
  212. });
  213. }
  214. }
  215. // 微信支付
  216. async wechatAppPay() {
  217. let that = this;
  218. let { error, data } = await this.prepay();
  219. if (error === 0) {
  220. uni.requestPayment({
  221. provider: 'wxpay',
  222. orderInfo: data.pay_data, //微信订单数据(官方说是string。实测为object)
  223. success: (res) => {
  224. that.payResult('success');
  225. },
  226. fail: (err) => {
  227. err.errMsg !== 'requestPayment:fail cancel' && that.payResult('fail');
  228. },
  229. });
  230. }
  231. }
  232. // 支付结果跳转,success:成功,fail:失败
  233. payResult(resultType) {
  234. sheep.$router.redirect('/pages/pay/result', {
  235. orderSN: this.orderSN,
  236. payment: this.payment, //重新支付的时候使用
  237. payState: resultType,
  238. orderType: this.orderType,
  239. });
  240. }
  241. }