My SMM panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

credits.vue 4.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <section v-if="!complete" class="select-credits">
  3. <div class="credits-pane"><h2>10 Credits</h2>
  4. <h3>$10.99</h3><div><span>Qty</span><input min="0" max="1000" v-model="packs.credits10" type="number"></div>
  5. </div>
  6. <div class="credits-pane"><div><h2>50 Credits</h2><span>+5 Free Credits</span></div>
  7. <h3>$54.99 </h3><div><span>Qty</span><input min="0" max="1000" v-model="packs.credits50" type="number"></div>
  8. </div>
  9. <div class="credits-pane"><div><h2>100 Credits</h2><span>+10 Free Credits</span></div>
  10. <h3>$109.99</h3> <div><span>Qty</span><input min="0" max="1000" v-model="packs.credits100" type="number"></div>
  11. </div>
  12. <div class="credits-pane"><div><h2>1000 Credits</h2><span>+150 Free Credits</span></div>
  13. <h3>$1010.00</h3> <div><span>Qty</span><input min="0" max="1000" v-model="packs.credits1000" type="number"></div>
  14. </div>
  15. <h3>Total: ${{total.toLocaleString('en')}}</h3>
  16. <div id="credits-errors"></div>
  17. </section>
  18. <section v-if="!complete" id="payment-section">
  19. <h4>Select a payment method</h4>
  20. <div class="sliding-menu">
  21. <a @click="selectSaved = true" :class="{selected: selectSaved}">Saved Card</a>
  22. <a @click="selectSaved = false" :class="{selected: !selectSaved}">New Card</a>
  23. <div :class="{right: !selectSaved}" class="menu-slider"><div></div></div>
  24. </div>
  25. <saved-cards :preferred="preferred" v-model:picked-card="picked" :cards="cards"
  26. :token="token" v-if="selectSaved"></saved-cards>
  27. <payment-card @set-card="(c) => {card = c}" @card-valid="(val) => {cardValid = val}"
  28. @update-billing-name="billingName = $event.target.value" :stripe="stripe"
  29. v-if="!selectSaved">
  30. </payment-card>
  31. <div id="payment-error"></div>
  32. </section>
  33. <section v-if="!complete" class="credits-confirm">
  34. <button @click="pay" :disabled="!ready"
  35. class="brand-btn">Buy<loading v-if="loading"></loading></button>
  36. </section>
  37. <PurchaseCompleted v-if="complete"></PurchaseCompleted>
  38. </template>
  39. <script>
  40. import Loading from '../icons/loading.vue'
  41. import PaymentCard from './payment-card.vue'
  42. import SavedCards from './saved-cards.vue'
  43. import PurchaseCompleted from './purchase-completed.vue'
  44. function total() {
  45. return this.packs.credits10*10.99 + this.packs.credits50*54.99
  46. + this.packs.credits100*109.99 + this.packs.credits1000*1010
  47. }
  48. //Gets the secret key specific to chosen payment amount and user
  49. function getSecret() {
  50. document.getElementById('credits-errors').textContent = ''
  51. this.loading = true
  52. return fetch('/panel/secret', {
  53. method: 'POST',
  54. headers: {'Content-Type': 'application/json',
  55. 'Accept': 'application/json',
  56. 'X-XSRF-TOKEN': this.token},
  57. body: JSON.stringify({'packs': this.packs})
  58. }).then((response) => {
  59. if (response.ok) {
  60. return response.text()
  61. } else {
  62. document.getElementById('credits-errors').textContent =
  63. `${response.status}: ${response.statusText}`
  64. }
  65. }).then(secret => {
  66. this.loading = false
  67. return secret
  68. })
  69. }
  70. //Gets key from the server then sends it with stripe
  71. //Maybe it should asl attach the user's receipt email
  72. function pay() {
  73. this.getSecret().then(secret => {
  74. if (!secret) {return}
  75. this.loading = true
  76. if (!this.selectSaved) {
  77. return this.stripe.confirmCardPayment(secret, {
  78. payment_method: {
  79. card: this.card,
  80. billing_details: {name:
  81. document.getElementById('billing-name').value},
  82. },
  83. setup_future_usage: document.querySelector(
  84. "#save-card input").value == 'on' ? 'off_session' : null
  85. })
  86. } else {
  87. return this.stripe.confirmCardPayment(secret, {
  88. payment_method: this.picked
  89. })
  90. }
  91. }).then(result => {
  92. if (result.error) {
  93. document.getElementById('payment-error').textContent =
  94. result.error.message
  95. } else if (result.paymentIntent.status === 'succeeded') {
  96. setTimeout(() => {this.$emit('purchaseComplete')}, 4000)
  97. setTimeout(() => {this.$emit('purchaseComplete')}, 10000)
  98. this.complete = true
  99. } else {
  100. document.getElementById('payment-error').textContent =
  101. 'An unknown error occured'
  102. }
  103. this.loading = false
  104. })
  105. }
  106. function getCards() {
  107. fetch('/panel/cards', {
  108. method: 'GET',
  109. headers: {'Content-Type': 'application/json',
  110. 'Accept': 'application/json',
  111. 'X-XSRF-TOKEN': this.token}
  112. }).then((response) => {response.json().then(data => {
  113. this.cards = data.data
  114. if (this.cards && this.cards.length > 0) {
  115. this.picked = this.cards[0].id
  116. }
  117. })})
  118. }
  119. function ready() {
  120. return this.total != 0 && !this.loading && ((this.cardValid &&
  121. this.billingName) || this.picked)
  122. }
  123. export default {
  124. components:{Loading, PaymentCard, SavedCards, PurchaseCompleted},
  125. data() {
  126. return {packs: {credits10: 0, credits50: 0,
  127. credits100: 0, credits1000: 0}, loading: false, stripe:
  128. window.Stripe(process.env.VUE_APP_STRIPE_KEY), card:
  129. null, billingName: null, selectSaved: true, cardValid: false, cards:
  130. null, picked: null, complete: false
  131. }
  132. },
  133. computed: {total, ready},
  134. methods: {getSecret, pay, getCards},
  135. props: ['preferred', 'token'],
  136. emits: ['purchaseComplete'],
  137. created() {
  138. this.getCards()
  139. }
  140. }
  141. </script>