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ů.
 
 
 
 
 
 

116 řádky
2.7 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" :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 clientSecret = new URLSearchParams(window.location.search).get(
  21. 'payment_intent_client_secret'
  22. );
  23. function getCookie(name) {
  24. var re = new RegExp(name + "=([^;]+)")
  25. var value = re.exec(document.cookie)
  26. return (value != null) ? unescape(value[1]) : null
  27. }
  28. function getUser() {
  29. token.value = getCookie("skouter")
  30. return fetch(`/api/user`,
  31. {method: 'GET',
  32. headers: {
  33. "Accept": "application/json",
  34. "Authorization": `Bearer ${token.value}`,
  35. },
  36. }).then(response => {
  37. if (response.ok) {
  38. return response.json()
  39. }
  40. })
  41. }
  42. function create(user) {
  43. console.log(user)
  44. fetch(`/api/user`,
  45. {method: 'POST',
  46. body: JSON.stringify(user),
  47. headers: {
  48. "Accept": "application/json",
  49. },
  50. }).then(resp => {
  51. if (resp.ok) {
  52. return resp.json().then(u => { err.value = "" }).
  53. then( () => {
  54. token.value = getCookie("skouter")
  55. intent(user)
  56. } )
  57. } else {
  58. resp.text().then( e => err.value = e)
  59. }
  60. })
  61. }
  62. function intent(u) {
  63. return fetch(`/api/user/subscribe`,
  64. {method: 'POST',
  65. body: JSON.stringify(u),
  66. headers: {
  67. "Accept": "application/json",
  68. "Authorization": `Bearer ${token.value}`,
  69. },
  70. }).then(resp => {
  71. if (resp.ok) {
  72. resp.json().then(s => {
  73. err.value = ""
  74. console.log(s)
  75. sub.value = s
  76. if (["processing", "succeeded"].includes(s.paymentStatus) &&
  77. clientSecret == s.clientSecret) {
  78. step.value = step.value + 2
  79. } else if (s.paymentStatus == "requires_payment_method") {
  80. step.value++
  81. } else {
  82. step.value = 0
  83. }
  84. })
  85. } else {
  86. resp.text().then( e => err.value = e)
  87. }
  88. })
  89. }
  90. onMounted(() => {
  91. getUser().then( u => {
  92. if (u) { user.value = u; intent(u) }
  93. })
  94. })
  95. </script>
  96. <style scoped>
  97. section {
  98. max-width: 400px;
  99. margin: auto;
  100. }
  101. </style>