useSplitCardInfo.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { reactive, ref } from 'vue'
  2. // #ifdef H5
  3. import { createElement, init } 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. let cardNumber = '4035501000000008' // 测试用卡号
  9. export const useSplitCardInfo = () => {
  10. let paymentInfo = ref(null)
  11. let intent_id = ref('')
  12. let client_secret = ref('')
  13. const elementTypeList = reactive({
  14. cardNumber: {
  15. instance: null,
  16. type: "cardNumber",
  17. container: "split_card_number",
  18. options: {}
  19. },
  20. expiry: {
  21. instance: null,
  22. type: "expiry",
  23. container: "split_card_expiry",
  24. options: {}
  25. },
  26. cvc: {
  27. instance: null,
  28. type: "cvc",
  29. container: "split_card_cvc",
  30. options: {}
  31. },
  32. })
  33. // 初始化
  34. const onInit = () => {
  35. const options = {
  36. env: 'prod', // 'staging | 'demo' | 'prod'
  37. enabledElements: ['payments'],
  38. locale: getLan(),
  39. }
  40. init(options).then(() => {
  41. for (let key in elementTypeList) {
  42. onMountElement(elementTypeList[key])
  43. }
  44. });
  45. }
  46. // 挂载元素
  47. const onMountElement = async (elementInfo) => {
  48. elementInfo.instance = await createElement(elementInfo.type, elementInfo.options);
  49. elementInfo.instance && elementInfo.instance.mount(elementInfo.container);
  50. }
  51. // 卸载元素
  52. const onUnmountElement = () => {
  53. for (let key in elementTypeList) {
  54. const info = elementTypeList[key]
  55. if (info.instance) {
  56. info.instance.destroy();
  57. info.instance.unmount();
  58. info.instance = null;
  59. }
  60. }
  61. }
  62. // 支付信息
  63. const fetchPaymentInfo = (params = {}) => {
  64. return new Promise((resolve, reject) => {
  65. const query = {
  66. payType: 'airwallex',
  67. ...params
  68. }
  69. SHOP_SUBMIT_PAY(query).then((result) => {
  70. paymentInfo.value = result.data ? result.data : null
  71. intent_id.value = paymentInfo.value ? paymentInfo.value.id : ''
  72. client_secret.value = paymentInfo.value ? paymentInfo.value.client_secret : ''
  73. resolve(result.data)
  74. }).catch(err => {
  75. reject(err)
  76. })
  77. })
  78. }
  79. return {
  80. elementTypeList,
  81. intent_id,
  82. client_secret,
  83. onInit,
  84. onUnmountElement,
  85. fetchPaymentInfo
  86. }
  87. }