tradePay.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { tradeNoForOrder, tradeResult, tradeNoQuery } from "../service/common";
  2. const tradePay = function (params, option = {}) {
  3. // eslint-disable-next-line no-param-reassign
  4. option = Object.assign(
  5. {
  6. useBalance: false,
  7. useMedicare: false,
  8. tradeType: "Appointment", // Outpatient: 门诊缴费; Appointment: 挂号、充值
  9. },
  10. option
  11. );
  12. console.log(params, option);
  13. if (option.tradeType === "Appointment") {
  14. /**
  15. * params 挂号 : { type: 1, idNum: '', depName: '' }
  16. * params 充值 : { type: 2, idNum: '', amount: 0 }
  17. */
  18. return appointment(params);
  19. } else {
  20. /**
  21. * params : { outTradeNo }
  22. */
  23. return outpatient(params);
  24. }
  25. }; // 挂号,充值类订单
  26. function appointment(params) {
  27. return tradeNoQuery(params).then(_tradePay);
  28. } // 门诊类订单
  29. function outpatient(params) {
  30. const {
  31. hisPatientId,
  32. age,
  33. sex,
  34. orderId,
  35. hisOrderNo,
  36. amount,
  37. deptName,
  38. doctorName,
  39. doctorCode,
  40. total,
  41. hisClinicCode,
  42. name,
  43. prescribeDate,
  44. } = params;
  45. return tradeNoForOrder({
  46. useBalance: params.useBalance,
  47. // 是否使用就诊卡余额
  48. useMedicare: params.useMedicare,
  49. // 是否使用医保卡余额
  50. outTradeNo: params.outTradeNo, // 商家订单号
  51. // 新增参数
  52. hisPatientId,
  53. age,
  54. sex,
  55. orderId,
  56. hisOrderNo,
  57. amount,
  58. deptName,
  59. doctorName,
  60. doctorCode,
  61. total,
  62. hisClinicCode,
  63. name,
  64. prescribeDate,
  65. }).then((tradeData) => {
  66. if (tradeData.tradeNo) {
  67. // 发起支付
  68. return _tradePay(tradeData);
  69. } else {
  70. return Promise.resolve(
  71. compareResult({
  72. success: true,
  73. })
  74. );
  75. }
  76. });
  77. } // 处理支付结果
  78. function compareResult(param) {
  79. console.log(param);
  80. if (!param.success) {
  81. my.showToast({
  82. type: "fail",
  83. content: param.payRes.memo || "订单支付失败",
  84. });
  85. }
  86. return param.success;
  87. }
  88. function _tradePay(
  89. tradeData = {
  90. tradeNo: "",
  91. outTradeNo: "",
  92. }
  93. ) {
  94. return new Promise((resolve, reject) => {
  95. my.tradePay({
  96. tradeNO: tradeData.tradeNo,
  97. success(payRes) {
  98. // 4000 订单处理失败
  99. // 6001 用途中途取消支付
  100. // 6002 网络链接出错
  101. if (
  102. payRes.resultCode === "4000" ||
  103. payRes.resultCode === "6002" ||
  104. payRes.resultCode === "6001"
  105. ) {
  106. // 支付失败
  107. reject(
  108. compareResult({
  109. success: false,
  110. payRes,
  111. })
  112. );
  113. } else {
  114. // 其他情况调用接口确认
  115. triggerPay({
  116. tradeNo: tradeData.tradeNo,
  117. outTradeNo: tradeData.outTradeNo,
  118. resultCode: payRes.resultCode,
  119. }).then((r) => {
  120. if (r.status === "TRADE_SUCCESS") {
  121. resolve(
  122. compareResult({
  123. success: true,
  124. payRes,
  125. })
  126. );
  127. } else {
  128. reject(
  129. compareResult({
  130. success: false,
  131. payRes,
  132. })
  133. );
  134. }
  135. });
  136. }
  137. },
  138. fail: (payRes) => {
  139. // 订单支付异常
  140. reject(
  141. compareResult({
  142. success: false,
  143. payRes,
  144. })
  145. );
  146. },
  147. });
  148. });
  149. } // 验证支付结果
  150. function triggerPay(param) {
  151. return tradeResult(param);
  152. }
  153. export default {
  154. tradePay,
  155. };