OrderPaystack.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import paystack from 'vue-paystack'
  2. // 保留复消 V1 的 SN/metadata 协议,补上脚本与 SDK 错误的恢复路径。
  3. export default {
  4. name: 'OrderPaystack',
  5. extends: paystack,
  6. beforeDestroy() {
  7. clearTimeout(this.loadTimer)
  8. },
  9. methods: {
  10. loadScript(callback) {
  11. if (window.PaystackPop?.setup) {
  12. callback()
  13. return
  14. }
  15. const script = document.createElement('script')
  16. const finish = error => {
  17. clearTimeout(this.loadTimer)
  18. this.loadError = error
  19. callback()
  20. }
  21. script.src = 'https://js.paystack.co/v1/inline.js'
  22. script.onload = () => finish(null)
  23. script.onerror = () => finish(new Error('Unable to load Paystack. Please try again.'))
  24. this.loadTimer = setTimeout(script.onerror, 15000)
  25. document.head.appendChild(script)
  26. },
  27. payWithPaystack() {
  28. return this.scriptLoaded
  29. .then(() => {
  30. if (this._isDestroyed) return
  31. if (this.loadError) throw this.loadError
  32. if (!this.paystackkey) {
  33. throw new Error('Paystack is unavailable. Please contact customer service.')
  34. }
  35. const callback = this.callback
  36. const close = this.close
  37. window.PaystackPop.setup({
  38. key: this.paystackkey,
  39. email: this.email,
  40. amount: this.amount,
  41. currency: 'NGN',
  42. ref: this.reference,
  43. metadata: this.metadata,
  44. channels: this.channels,
  45. callback,
  46. onClose: close
  47. }).openIframe()
  48. })
  49. .catch(error => {
  50. if (!this._isDestroyed) this.$emit('error', error)
  51. })
  52. }
  53. }
  54. }