useDropInPayInfo.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { ref, reactive, nextTick } from 'vue'
  2. // #ifdef H5
  3. import { init, createElement } from '@airwallex/components-sdk';
  4. // #endif
  5. import { SHOP_SUBMIT_PAY } from '@/api'
  6. import { getLan } from "@/locale"
  7. import verConfig from '@/ver.config'
  8. export const useDropInPayInfo = () => {
  9. let dropInElement = ref(null);
  10. let createOptions = reactive({});
  11. // 初始化
  12. const handleInit = async (callback) => {
  13. try {
  14. await init({
  15. env: 'prod',
  16. enabledElements: ['payments'],
  17. locale: getLan()
  18. });
  19. dropInElement.value = await createElement('dropIn', {
  20. ...createOptions,
  21. appearance: {
  22. mode: 'light',
  23. variables: {
  24. colorBrand: '#612FFF',
  25. },
  26. }
  27. });
  28. nextTick(() => {
  29. dropInElement.value && dropInElement.value.mount('dropIn');
  30. callback && callback();
  31. })
  32. }
  33. catch (err) {
  34. console.log('--- err ---', err)
  35. }
  36. }
  37. // 卸载&销毁
  38. const handleUnmount = () => {
  39. if (dropInElement.value) {
  40. dropInElement.value.unmount();
  41. dropInElement.value.destroy();
  42. dropInElement.value = null;
  43. }
  44. }
  45. // 获取初始化配置数据
  46. const fetchPaymentInitData = (params = {}, callback) => {
  47. return new Promise((resolve, reject) => {
  48. SHOP_SUBMIT_PAY({
  49. payType: 'airwallex',
  50. ...params
  51. }).then(result => {
  52. const {
  53. id,
  54. client_secret,
  55. amount,
  56. currency,
  57. countryCode
  58. } = result.data;
  59. createOptions.intent_id = id;
  60. createOptions.client_secret = client_secret;
  61. /* createOptions.amount = {
  62. value: amount,
  63. currency,
  64. }; */
  65. createOptions.currency = currency
  66. createOptions.countryCode = countryCode
  67. nextTick(() => {
  68. console.log('---- createOptions ----', createOptions)
  69. handleInit(callback);
  70. })
  71. resolve(result.data)
  72. }).catch(err => {
  73. reject(err)
  74. })
  75. })
  76. }
  77. return {
  78. dropInElement,
  79. handleInit,
  80. handleUnmount,
  81. fetchPaymentInitData
  82. }
  83. }