pay.js 5.9 KB

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