useAppleInfo.js 1.5 KB

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