index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { createOrderApi } from "../../api/order.api";
  2. import { throttle } from "../../utils/helper";
  3. import { getSystemInfo } from "../../utils/wechat";
  4. /**
  5. * @description: 内部支付函数
  6. * @param {*} payParams
  7. * @return {*}
  8. */
  9. const _payment = async (payParams) => {
  10. try {
  11. const orderInfo = {
  12. cp_order_id: payParams.cp_order_id,
  13. money: payParams.money, // 元单位
  14. role_id: payParams.role_id,
  15. role_name: payParams.role_name,
  16. server_id: payParams.server_id,
  17. server_name: payParams.server_name,
  18. ext: payParams.ext ? payParams.ext : "",
  19. role_level: payParams.role_level,
  20. product_id: payParams.product_id,
  21. product_name: payParams.product_name,
  22. };
  23. tt.showLoading({
  24. title: "支付中...",
  25. mask: true,
  26. });
  27. // 下单
  28. const createOrderResult = await createOrderApi(orderInfo);
  29. tt.hideLoading({});
  30. let dyPayConfig = typeof createOrderResult.dy_pay_config !== "undefined" ? createOrderResult.dy_pay_config : {}
  31. await requestDyPayment(orderInfo, dyPayConfig);
  32. return Promise.resolve(null);
  33. } catch (error) {
  34. return Promise.reject(error);
  35. }
  36. };
  37. /**
  38. * @description: 安卓虚拟支付
  39. * @param {object}
  40. * @return {*}
  41. */
  42. export const requestGamePayment = (data) => {
  43. return new Promise((resolve, reject) => {
  44. console.log("虚拟支付开始:", data);
  45. tt.requestGamePayment({
  46. ...data,
  47. success: (res) => {
  48. resolve(res);
  49. },
  50. fail: (error) => {
  51. console.log("虚拟支付失败:", error);
  52. reject(error);
  53. },
  54. });
  55. });
  56. };
  57. /**
  58. * @description: iOS 虚拟支付 发起钻石支付
  59. * @param {object}
  60. * @return {*}
  61. */
  62. export const openAwemeCustomerService = (data) => {
  63. return new Promise((resolve, reject) => {
  64. console.log("发起钻石支付:", data);
  65. tt.openAwemeCustomerService({
  66. ...data,
  67. success: (res) => {
  68. resolve(res);
  69. },
  70. fail: (error) => {
  71. console.log("钻石支付失败:", error);
  72. reject(error);
  73. },
  74. });
  75. });
  76. };
  77. /**
  78. * 拉起抖音支付
  79. */
  80. const requestDyPayment = async (orderInfo, dyPayConfig) => {
  81. let platform = await getSystemInfo("platform");
  82. try {
  83. var setGoodType = 0;
  84. let res;
  85. // 小额支付描述 https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/guide/open-ability/payment/micropayment-guide
  86. // 安卓 requestGamePayment
  87. if(platform==="android"){
  88. // 判断是否小额支付
  89. if (tt.canIUse('requestGamePayment.object.goodType')) {
  90. setGoodType = 2;
  91. res = await requestGamePayment({
  92. goodType: setGoodType, // 道具直购
  93. orderAmount:orderInfo.money * 100, // 单位分
  94. goodName: orderInfo.product_name,
  95. currencyType: "CNY",
  96. zoneId: String(dyPayConfig.zoneId),
  97. customId: orderInfo.cp_order_id,
  98. mode: "game",
  99. env: 0,
  100. platform: platform, // 目前仅为"android"
  101. extraInfo: String(setGoodType)
  102. });
  103. }else{
  104. res = await requestGamePayment({
  105. mode: "game",
  106. env: 0,
  107. platform: platform, // 目前仅为"android"
  108. currencyType: "CNY",
  109. buyQuantity: dyPayConfig.buyQuantity,
  110. zoneId: String(dyPayConfig.zoneId),
  111. customId: orderInfo.cp_order_id,
  112. });
  113. }
  114. }else{
  115. // 判断是否小额支付
  116. if (tt.canIUse('openAwemeCustomerService.object.goodType')) {
  117. setGoodType = 2;
  118. // iOS openAwemeCustomerService
  119. res = await openAwemeCustomerService({
  120. goodType: setGoodType, // 道具直购
  121. orderAmount: orderInfo.money * 100, // 单位分
  122. goodName: orderInfo.product_name,
  123. currencyType: "DIAMOND",
  124. zoneId: String(dyPayConfig.zoneId),
  125. customId: orderInfo.cp_order_id,
  126. extraInfo: String(setGoodType)
  127. });
  128. }else{
  129. // iOS openAwemeCustomerService
  130. res = await openAwemeCustomerService({
  131. zoneId: String(dyPayConfig.zoneId),
  132. customId: orderInfo.cp_order_id,
  133. currencyType: "DIAMOND",
  134. buyQuantity: dyPayConfig.buyQuantity,
  135. });
  136. }
  137. }
  138. return Promise.resolve();
  139. } catch (error) {
  140. return Promise.reject(error);
  141. }
  142. }
  143. /**
  144. * @description:拉起支付(节流版本,避免短时间内重复点击)
  145. * @param {*} payParams
  146. * @return {*}
  147. */
  148. export const payment = throttle(_payment, 2000);