useGoogleInfo.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { ref, reactive, nextTick } from 'vue'
  2. // #ifdef H5
  3. import { createElement, loadAirwallex, getElement } from 'airwallex-payment-elements'
  4. // #endif
  5. import { SHOP_SUBMIT_PAY } from '@/api'
  6. import { getLan } from "@/locale"
  7. import verConfig from '@/ver.config'
  8. let googleInstance = ref(null);
  9. let googleParams = reactive({})
  10. const init = (callback) => {
  11. loadAirwallex({
  12. env: 'prod',
  13. origin: window.location.origin,
  14. locale: getLan()
  15. }).then(() => {
  16. googleInstance.value = createElement('googlePayButton', {
  17. ...googleParams
  18. });
  19. googleInstance.value.mount('google_pay_card');
  20. callback && callback();
  21. })
  22. }
  23. const unmountCard = () => {
  24. let card = getElement('googlePayButton');
  25. card.destroy();
  26. card.unmount();
  27. googleInstance.value = null;
  28. }
  29. const fetchCardPaymentInfo = (params = {}, callback) => {
  30. return new Promise((resolve, reject) => {
  31. const query = {
  32. payType: 'airwallex',
  33. ...params
  34. }
  35. SHOP_SUBMIT_PAY(query).then(result => {
  36. const { id, client_secret, amount, currency, countryCode } = result.data;
  37. googleParams.intent_id = id;
  38. googleParams.client_secret = client_secret;
  39. googleParams.amount = {
  40. value: amount,
  41. currency,
  42. };
  43. googleParams.countryCode = countryCode
  44. nextTick(() => {
  45. console.log('---- googleParams ----', googleParams)
  46. init(callback);
  47. })
  48. resolve(result.data)
  49. }).catch(err => { reject(err) })
  50. })
  51. }
  52. export const useGoogleInfo = () => {
  53. return { googleInstance, init, unmountCard, fetchCardPaymentInfo }
  54. }