GuestPaystack.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import paystack from 'vue-paystack'
  2. // 复用现有组件;其 V1 SDK 会忽略 access_code,游客交易改由 V2 恢复。
  3. export default {
  4. name: 'GuestPaystack',
  5. extends: paystack,
  6. data() {
  7. return { PaystackPopup: null, scriptError: null, opening: false }
  8. },
  9. methods: {
  10. loadScript(callback) {
  11. const script = document.createElement('script')
  12. script.src = 'https://js.paystack.co/v2/inline.js'
  13. script.onload = () => {
  14. this.PaystackPopup = window.PaystackPop
  15. callback()
  16. }
  17. script.onerror = () => {
  18. this.scriptError = new Error('Unable to load Paystack. Please close the dialog and try again.')
  19. callback()
  20. this.handleError(this.scriptError)
  21. }
  22. document.head.appendChild(script)
  23. },
  24. handleError(error) {
  25. this.opening = false
  26. this.$emit('error', error)
  27. },
  28. payWithPaystack() {
  29. if (this.opening) return
  30. this.opening = true
  31. return this.scriptLoaded.then(() => {
  32. if (this.scriptError) throw this.scriptError
  33. if (!this.accessCode) throw new Error('Payment access code is missing.')
  34. const popup = new this.PaystackPopup()
  35. popup.resumeTransaction(this.accessCode, {
  36. onSuccess: response => {
  37. this.opening = false
  38. this.callback(response)
  39. },
  40. onCancel: () => {
  41. this.opening = false
  42. this.close()
  43. },
  44. onError: this.handleError
  45. })
  46. }).catch(this.handleError)
  47. }
  48. }
  49. }