Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

126 řádky
2.9 KiB

  1. <template>
  2. <section class="shadowbox">
  3. <h2>Register</h2>
  4. <account v-if="step == 1" :err="err" @submit="create" />
  5. <billing v-if="step == 2 && !trial" :err="err" :sub="sub"/>
  6. <completed v-if="step == 3" :err="err" :status="sub.paymentStatus"/>
  7. </section>
  8. </template>
  9. <script setup>
  10. import { ref, onMounted } from "vue"
  11. import Account from "./account.vue"
  12. import Billing from "./billing.vue"
  13. import Completed from "./completed.vue"
  14. let err = ref("")
  15. const stripe = Stripe(process.env.STRIPE_KEY)
  16. const step = ref(1)
  17. const token = ref("")
  18. const user = ref(null)
  19. const sub = ref(null)
  20. const trial = ref(false)
  21. const clientSecret = new URLSearchParams(window.location.search).get(
  22. 'payment_intent_client_secret'
  23. );
  24. function getCookie(name) {
  25. var re = new RegExp(name + "=([^;]+)")
  26. var value = re.exec(document.cookie)
  27. return (value != null) ? unescape(value[1]) : null
  28. }
  29. function getUser() {
  30. token.value = getCookie("skouter")
  31. return fetch(`/api/user`,
  32. {method: 'GET',
  33. headers: {
  34. "Accept": "application/json",
  35. "Authorization": `Bearer ${token.value}`,
  36. },
  37. }).then(response => {
  38. if (response.ok) {
  39. return response.json()
  40. }
  41. })
  42. }
  43. function create(user) {
  44. console.log(user)
  45. fetch(`/api/user`,
  46. {method: 'POST',
  47. body: JSON.stringify(user),
  48. headers: {
  49. "Accept": "application/json",
  50. },
  51. }).then(resp => {
  52. if (resp.ok) {
  53. return resp.json().then(u => { err.value = "" }).
  54. then( () => {
  55. token.value = getCookie("skouter")
  56. intent(user)
  57. } )
  58. } else {
  59. resp.text().then( e => err.value = e)
  60. }
  61. })
  62. }
  63. function intent(u) {
  64. let path = `/api/user/subscribe`
  65. if (trial.value) {
  66. path = `/api/user/trial`
  67. }
  68. return fetch(path,
  69. {method: 'POST',
  70. body: JSON.stringify(u),
  71. headers: {
  72. "Accept": "application/json",
  73. "Authorization": `Bearer ${token.value}`,
  74. },
  75. }).then(resp => {
  76. if (resp.ok) {
  77. resp.json().then(s => {
  78. err.value = ""
  79. console.log(s)
  80. sub.value = s
  81. if (["processing", "succeeded"].includes(s.paymentStatus) &&
  82. clientSecret == s.clientSecret) {
  83. step.value = step.value + 2
  84. } else if (s.paymentStatus == "requires_payment_method") {
  85. step.value++
  86. } else {
  87. step.value = 0
  88. }
  89. })
  90. } else {
  91. resp.text().then( e => err.value = e)
  92. }
  93. })
  94. }
  95. onMounted(() => {
  96. if (window.location.pathname == "/free-trial") {
  97. trial.value = true
  98. }
  99. getUser().then( u => {
  100. if (u) { user.value = u; intent(u) }
  101. })
  102. })
  103. </script>
  104. <style scoped>
  105. section {
  106. max-width: 400px;
  107. margin: auto;
  108. }
  109. </style>