usePay.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. import {
  2. ref,
  3. reactive
  4. } from "vue";
  5. import {
  6. orderPayAli,
  7. orderPayWx,
  8. orderCheck,
  9. orderPayApple
  10. } from "@/api/shop";
  11. import {
  12. toast
  13. } from "@/utils/common";
  14. export function usePay(opt = {}) {
  15. const {
  16. createOrderError, // 创建订单失败
  17. checkError, // 校验失败
  18. payError, // 支付失败
  19. paySuccess, //支付成功
  20. applePayError, // 苹果内购失败
  21. } = opt;
  22. const aliData = reactive({
  23. orderId: null, // 订单编号
  24. text: '', // 订单数据
  25. })
  26. const wxData = reactive({
  27. appid: '', // 应用ID(AppID)
  28. mchid: '',
  29. noncestr: '', // 随机字符串
  30. orderId: '',
  31. packageVal: '',
  32. partnerid: '', // 商户号(PartnerID)
  33. pid: '',
  34. prepayid: '', // 预支付交易会话ID
  35. timestamp: '', // 时间戳(单位:秒)
  36. sign: '', // 签名,这里用的 MD5 签名
  37. package: '', // 固定值
  38. })
  39. const appleData = reactive({
  40. chanpinId: null, // 产品ID
  41. paynum: "", // 支付号
  42. receipt: "", // 票据
  43. taocanId: null, // 套餐ID
  44. })
  45. function resetStatus() {
  46. Object.assign(aliData, {
  47. orderId: null, // 订单编号
  48. text: '', // 订单数据
  49. })
  50. Object.assign(wxData, {
  51. appid: '', // 应用ID(AppID)
  52. mchid: '',
  53. noncestr: '', // 随机字符串
  54. orderId: '',
  55. packageVal: '',
  56. partnerid: '', // 商户号(PartnerID)
  57. pid: '',
  58. prepayid: '', // 预支付交易会话ID
  59. timestamp: '', // 时间戳(单位:秒)
  60. sign: '', // 签名,这里用的 MD5 签名
  61. package: '', // 固定值
  62. })
  63. Object.assign(appleData, {
  64. chanpinId: null, // 产品ID
  65. paynum: "", // 支付号
  66. receipt: "", // 票据
  67. taocanId: null, // 套餐ID
  68. })
  69. }
  70. // 支付入口 type:业务类型:wx|ali|apple; options: 入参chanpinId,taocanId,applePid
  71. function OrderPay(type, options) {
  72. // 初始化支付状态
  73. resetStatus();
  74. const {
  75. chanpinId,
  76. taocanId,
  77. applePid, // 苹果商品Id ---> 用来获取支付号+票据
  78. } = options;
  79. if (!chanpinId) {
  80. toast('数据异常,产品信息丢失')
  81. return;
  82. }
  83. if (!taocanId) {
  84. toast('数据异常,套餐信息丢失')
  85. return;
  86. }
  87. if (type == 'wx') {
  88. uni.showLoading({
  89. title: '订单创建中...',
  90. mask: true
  91. });
  92. // 微信
  93. orderPayWx({
  94. chanpinId,
  95. taocanId
  96. }).then(res => {
  97. uni.hideLoading();
  98. if (res.data.code == 0) {
  99. Object.assign(wxData, res.data);
  100. // 开始支付
  101. wxPay(res.data);
  102. } else {
  103. toast('订单创建失败')
  104. // 业务异常
  105. createOrderError && createOrderError({
  106. type: 'CreateOrderError',
  107. msg: '业务异常,创建订单返回状态异常',
  108. err: new Error('业务异常,创建订单返回状态异常'),
  109. form: 'wx'
  110. })
  111. }
  112. }).catch(err => {
  113. uni.hideLoading();
  114. toast('订单创建失败')
  115. createOrderError && createOrderError({
  116. type: 'CreateOrderError',
  117. msg: '创建微信订单失败',
  118. err,
  119. form: 'wx'
  120. })
  121. })
  122. }
  123. if (type == 'ali') {
  124. uni.showLoading({
  125. title: '订单创建中...',
  126. mask: true
  127. });
  128. // 支付宝
  129. orderPayAli({
  130. chanpinId,
  131. taocanId
  132. }).then(res => {
  133. uni.hideLoading();
  134. if (res.data.code == 0) {
  135. Object.assign(aliData, res.data);
  136. // 开始支付
  137. aliPay(res.data);
  138. } else {
  139. toast('订单创建失败')
  140. // 业务异常
  141. createOrderError && createOrderError({
  142. type: 'CreateOrderError',
  143. msg: '业务异常,创建订单返回状态异常',
  144. err: new Error('业务异常,创建订单返回状态异常'),
  145. form: 'ali'
  146. })
  147. }
  148. }).catch(err => {
  149. uni.hideLoading();
  150. createOrderError && createOrderError({
  151. type: 'CreateOrderError',
  152. msg: '创建微信订单失败',
  153. err,
  154. form: 'ali'
  155. })
  156. })
  157. }
  158. if (type == 'apple') {
  159. if (!applePid) {
  160. toast('数据异常,商品信息丢失')
  161. return;
  162. }
  163. uni.showLoading({
  164. title: '订单创建中...',
  165. mask: true
  166. });
  167. applePay(options)
  168. }
  169. }
  170. // 微信支付
  171. function wxPay(options) {
  172. Object.assign(wxData, options);
  173. try {
  174. uni.requestPayment({
  175. provider: "wxpay",
  176. orderInfo: options,
  177. success: (res) => {
  178. uni.showLoading({
  179. title: '订单支付中...',
  180. mask: true
  181. });
  182. // 校验支付结果
  183. setTimeout(() => OrderCheckWx(), 1000)
  184. },
  185. fail: (err) => {
  186. payError && payError({
  187. type: 'wxPay',
  188. msg: '微信支付失败',
  189. err,
  190. from: 'uni.requestPayment.fail'
  191. })
  192. }
  193. })
  194. } catch (err) {
  195. payError && payError({
  196. type: 'wxPay',
  197. msg: '微信支付API唤起失败',
  198. err,
  199. from: 'uni.requestPayment'
  200. })
  201. }
  202. }
  203. // 校验
  204. function OrderCheckWx() {
  205. orderCheck({
  206. id: wxData.orderId
  207. }).then(res => {
  208. if (res.code == 0 && res.data) {
  209. // 校验通过,支付成功
  210. paySuccessResult();
  211. } else {
  212. setTimeout(() => {
  213. orderCheck({
  214. id: wxData.orderId
  215. }).then(res2 => {
  216. if (res2.code == 0 && res2.data) {
  217. // 校验通过,支付成功
  218. paySuccessResult();
  219. }
  220. }).catch(err1 => {
  221. uni.hideLoading()
  222. toast('支付二次查验失败,请联系管理员')
  223. checkError && checkError({
  224. type: 'OrderCheckWx',
  225. msg: '支付二次查验失败,请联系管理员',
  226. err: err1,
  227. form: 'wx'
  228. })
  229. })
  230. }, 5000)
  231. }
  232. }).catch(err => {
  233. uni.hideLoading()
  234. toast('支付查验失败,请联系管理员')
  235. checkError && checkError({
  236. type: 'OrderCheckWx',
  237. msg: '支付查验失败,请联系管理员',
  238. err: err,
  239. form: 'wx'
  240. })
  241. })
  242. }
  243. // 支付宝支付
  244. function aliPay(options) {
  245. Object.assign(aliData, options);
  246. try {
  247. uni.requestPayment({
  248. provider: "alipay",
  249. orderInfo: options.text, //此处为服务器返回的订单信息字符串
  250. success: (res) => {
  251. uni.showLoading({
  252. title: '订单支付中...',
  253. mask: true
  254. });
  255. // 校验支付结果
  256. setTimeout(() => OrderCheckAli(), 1000)
  257. },
  258. fail: (err) => {
  259. payError && payError({
  260. type: 'aliPay',
  261. msg: '支付宝支付失败',
  262. err,
  263. from: 'uni.requestPayment.fail'
  264. })
  265. }
  266. })
  267. } catch (err) {
  268. payError && payError({
  269. type: 'aliPay',
  270. msg: '支付宝API唤起失败',
  271. err,
  272. from: "uni.requestPayment"
  273. })
  274. }
  275. }
  276. // 校验
  277. function OrderCheckAli() {
  278. orderCheck({
  279. id: aliData.orderId
  280. }).then(res => {
  281. if (res.code == 0 && res.data) {
  282. // 校验通过,支付成功
  283. paySuccessResult();
  284. } else {
  285. setTimeout(() => {
  286. orderCheck({
  287. id: aliData.orderId
  288. }).then(res2 => {
  289. if (res2.code == 0 && res2.data) {
  290. // 校验通过,支付成功
  291. paySuccessResult();
  292. }
  293. }).catch(err1 => {
  294. uni.hideLoading()
  295. toast('支付二次查验失败,请联系管理员')
  296. checkError && checkError({
  297. type: 'OrderCheckAli',
  298. msg: '支付二次查验失败,请联系管理员',
  299. err: err1,
  300. form: 'ali'
  301. })
  302. })
  303. }, 5000)
  304. }
  305. }).catch(err => {
  306. uni.hideLoading()
  307. toast('支付查验失败,请联系管理员')
  308. checkError && checkError({
  309. type: 'OrderCheckAli',
  310. msg: '支付查验失败,请联系管理员',
  311. err: err,
  312. form: 'ali'
  313. })
  314. })
  315. }
  316. // 苹果支付 --- 无需check 直接购买成功
  317. function applePay(options) {
  318. Object.assign(appleData, options)
  319. try {
  320. plus.payment.getChannels(function(channels) {
  321. uni.hideLoading();
  322. // 获取 id 为 'appleiap' 的 channel
  323. // 开通了app应用内支付,在manifest.josn中设置,开通后需打自定议基座
  324. // iapChannel 为应用内支付对象
  325. const iapChannel = channels.find((item) => item.id == "appleiap");
  326. // ids 数组中的项为 App Store Connect 配置的内购买项目产品ID(productId)
  327. if (!iapChannel) {
  328. toast('未找到产品内购信息,请联系管理员')
  329. applePayError &&
  330. applePayError({
  331. type: "applePay",
  332. msg: "内购商品Id丢失,请确认是否已成功配置",
  333. err: new Error("内购商品Id丢失,请确认是否已成功配置"),
  334. from: "apple",
  335. });
  336. return;
  337. }
  338. const ids = [options.applePid];
  339. iapChannel.requestOrder(
  340. ids,
  341. function(e) {
  342. // 获取订单信息成功回调方法
  343. uni.requestPayment({
  344. provider: "appleiap",
  345. orderInfo: {
  346. productid: options.applePid, //产品id,来自于苹果
  347. quantity: 1, //产品数量
  348. manualFinishTransaction: true,
  349. },
  350. success: (e) => {
  351. // 苹果内购成功
  352. const {
  353. transactionIdentifier: paynum,
  354. transactionReceipt: receipt,
  355. } = e;
  356. Object.assign(appleData, {
  357. paynum,
  358. receipt,
  359. });
  360. orderPayApple({
  361. chanpinId: appleData.chanpinId, // 产品ID
  362. paynum: paynum, // 支付号
  363. receipt: receipt, // 票据
  364. taocanId: chanpinId.taocanId, // 套餐ID
  365. })
  366. .then((res) => {
  367. uni.hideLoading();
  368. if (res.code == 0 && res.data) {
  369. // 完成内购
  370. iapChannel.finishTransaction(data
  371. .transactionIdentifier);
  372. // 购买成功
  373. paySuccessResult();
  374. } else {
  375. iapChannel.finishTransaction(data
  376. .transactionIdentifier);
  377. toast("业务异常,订单支付失败,请联系管理员");
  378. applePayError &&
  379. applePayError({
  380. type: "orderPayApple",
  381. msg: "业务异常,订单支付失败",
  382. err: e,
  383. from: "apple",
  384. });
  385. }
  386. })
  387. .catch((err) => {
  388. uni.hideLoading();
  389. applePayError &&
  390. applePayError({
  391. type: "orderPayApple",
  392. msg: "订单支付校验失败",
  393. err: e,
  394. from: "apple",
  395. });
  396. });
  397. },
  398. fail: (e) => {
  399. uni.hideLoading();
  400. toast("苹果购买失败,请联系管理员");
  401. applePayError &&
  402. applePayError({
  403. type: "uni.requestPayment",
  404. msg: "苹果内购失败",
  405. err: e,
  406. from: "apple",
  407. });
  408. },
  409. });
  410. },
  411. function(e) {
  412. // 获取订单信息失败回调方法
  413. uni.hideLoading();
  414. toast('内购订单获取失败,请联系管理员')
  415. applePayError &&
  416. applePayError({
  417. type: "uni.requestPayment",
  418. msg: "苹果内购失败",
  419. err: e,
  420. from: "apple",
  421. });
  422. },
  423. );
  424. });
  425. } catch (err) {
  426. applePayError &&
  427. applePayError({
  428. type: "uni.requestPayment",
  429. msg: "苹果内购API唤起失败",
  430. err: e,
  431. from: "plus.payment.getChannels",
  432. });
  433. }
  434. }
  435. // 支付成功
  436. function paySuccessResult() {
  437. uni.hideLoading()
  438. toast('支付成功')
  439. paySuccess && paySuccess();
  440. }
  441. return {
  442. OrderPay
  443. }
  444. }
  445. /**
  446. * 错误
  447. * 1. 创建订单错误
  448. * 2. 支付SDK错误
  449. * 3. 支付成功后,后台错误
  450. *
  451. *
  452. *
  453. *
  454. *
  455. *
  456. *
  457. *
  458. *
  459. *
  460. *
  461. *
  462. */