Skip to content

Commit

Permalink
Attempt to fix subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
stevie.hartog committed Jun 4, 2024
1 parent 6adef25 commit d52c4b2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 73 deletions.
8 changes: 4 additions & 4 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ declare module '@/helpers/online' {
export default value
}

declare module '@/views/account/AccountSubscriptionPage.vue' {
const value: any
export default value
}
// declare module '@/views/account/AccountSubscriptionPage.vue' {
// const value: any
// export default value
// }

declare module '@/components/molecules/dialog/SubscribeToApp/SubscribeToApp.vue' {
const value: any
Expand Down
1 change: 1 addition & 0 deletions src/plugins/firebase/users-workouts-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default class UsersWorkoutsDB extends GenericDB {
}

if (lastVisible) {
// @ts-expect-error Custom field on DocumentSnapshot
const { updateTimestamp } = lastVisible
combinedQuery.push(startAfter(updateTimestamp))
}
Expand Down
155 changes: 86 additions & 69 deletions src/views/account/AccountSubscriptionPage.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script setup>
<script setup lang="ts">
import { useHead } from '@unhead/vue'
import { useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'
Expand All @@ -18,20 +18,25 @@ const canSubscribe = window.getDigitalGoodsService
const limit = 30
const PAYMENT_METHOD = 'https://play.google.com/billing'
let price = ''
let item = {}
let item: {
price?: { value: string; currency: string }
value?: string
currency?: string
[key: string]: any
} = {}
let buyStatus = ''
let logField = ''
let purchasesList = []
let purchasesList: PurchaseDetails[] = []
const progressValue = computed(() => {
// eslint-disable-next-line no-shadow
let time = 60
// eslint-disable-next-line no-unsafe-optional-chaining
if (!user || user?.value?.subscribed) time = 0
// eslint-disable-next-line no-unsafe-optional-chaining
const value = ((user?.value?.completed?.time / time) * 100) / limit
const completedTime = user?.value?.completed?.time ? user.value.completed.time : 0
const value = ((completedTime / time) * 100) / limit
return value < 100 ? value : 100
})
function log(contents) {
function log(contents: any) {
// eslint-disable-next-line no-console
console.log(contents)
logField += `${contents}\n`
Expand All @@ -41,7 +46,7 @@ function getChromeVersion() {
return raw ? parseInt(raw[2], 10) : false
}
function checkSupport() {
if (canSubscribe) {
if (canSubscribe !== undefined) {
log('Digital Goods Service is available.')
return
}
Expand All @@ -51,7 +56,7 @@ function checkSupport() {
log(`Chrome version: ${ver}`)
}
}
async function populatePrice(sku) {
async function populatePrice(sku: string): Promise<boolean> {
if (canSubscribe === undefined) {
// Digital Goods API is not supported in this context.
log("window doesn't have getDigitalGoodsService.")
Expand All @@ -73,14 +78,16 @@ async function populatePrice(sku) {
log(JSON.stringify(details, null, 2))
// eslint-disable-next-line prefer-destructuring
item = details[0]
const { value } = item.price
const { currency } = item.price
item.value = item.price.value
item.currency = item.price.currency
price = new Intl.NumberFormat(navigator.language, {
style: 'currency',
currency
}).format(value)
if (item.price) {
const { value } = item.price
const { currency } = item.price
item.value = item.price?.value
item.currency = item.price?.currency
price = new Intl.NumberFormat(navigator.language, {
style: 'currency',
currency
}).format(Number(value))
}
return true
} catch (error) {
// DGAPI 2.0 - Play Billing is not available. Use another payment flow.
Expand Down Expand Up @@ -120,7 +127,8 @@ async function listPurchases() {
log(error)
}
}
async function acknowledge(token, type = 'repeatable', onComplete = () => {}) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function acknowledge(token: any, type = 'repeatable', onComplete = () => {}) {
if (canSubscribe === undefined) {
// Digital Goods API is not supported in this context.
log("window doesn't have getDigitalGoodsService.")
Expand All @@ -136,7 +144,7 @@ async function acknowledge(token, type = 'repeatable', onComplete = () => {}) {
}
if ('acknowledge' in service) {
// DGAPI 1.0
await service.acknowledge(token, type)
log('error')
} else {
// DGAPI 2.0
await service.consume(token)
Expand All @@ -148,7 +156,8 @@ async function acknowledge(token, type = 'repeatable', onComplete = () => {}) {
log(error)
}
}
function trigger(sku, onToken = () => {}) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function trigger(sku: string, onToken = (token: string) => {}) {
// The PaymentRequest() constructor creates a new PaymentRequest object which will be used to handle the process of generating, validating, and submitting a payment request.
if (!window.PaymentRequest) {
log('No PaymentRequest object.')
Expand All @@ -171,11 +180,11 @@ function trigger(sku, onToken = () => {}) {
// The total amount of the payment request.
total: {
label: 'Subscription',
amount: { currency: item?.currency, value: item?.value }
amount: { currency: item.currency ?? '', value: item.value ?? '' }
}
}
const request = new PaymentRequest(supportedInstruments, details)
function handlePaymentResponse(response) {
function handlePaymentResponse(response: any) {
window.setTimeout(() => {
response
.complete('success')
Expand All @@ -189,8 +198,10 @@ function trigger(sku, onToken = () => {}) {
}
})
// eslint-disable-next-line func-names
.catch((e) => {
log(e.message)
.catch((error: unknown) => {
if (error instanceof Error) {
log(error.message)
}
log(JSON.stringify(response, undefined, 2))
})
// request = buildPaymentRequest();
Expand All @@ -202,69 +213,75 @@ function trigger(sku, onToken = () => {}) {
// eslint-disable-next-line func-names
.then((result) => {
log(result ? 'Can make payment' : 'Cannot make payment')
})
// eslint-disable-next-line func-names
.catch((e) => {
log(e.message)
})
}
// Checking for instrument presence.
if (request.hasEnrolledInstrument) {
request
.hasEnrolledInstrument()
// eslint-disable-next-line func-names
.then((result) => {
if (result) {
log('Has enrolled instrument')
} else {
log('No enrolled instrument')
request.show().then(handlePaymentResponse)
}
// Call show even if we don't have any enrolled instruments.
request
.show()
.then(handlePaymentResponse)
// eslint-disable-next-line func-names
.catch((e) => {
// log(JSON.stringify(e, undefined, 2));
log(e)
log("Maybe you've already purchased the item (try acknowledging first).")
})
})
// eslint-disable-next-line func-names
.catch((e) => {
log(e.message)
// Also call show if hasEnrolledInstrument throws.
request
.show()
.then(handlePaymentResponse)
// eslint-disable-next-line no-shadow,func-names
.catch((e) => {
log(JSON.stringify(e, undefined, 2))
log(e)
})
})
}
// Checking for instrument presence.
// if (request.hasEnrolledInstrument) {
// request
// .hasEnrolledInstrument()
// // eslint-disable-next-line func-names
// .then((result) => {
// if (result) {
// log('Has enrolled instrument')
// } else {
// log('No enrolled instrument')
// }
// // Call show even if we don't have any enrolled instruments.
// request
// .show()
// .then(handlePaymentResponse)
// // eslint-disable-next-line func-names
// .catch((e) => {
// // log(JSON.stringify(e, undefined, 2));
// log(e)
// log("Maybe you've already purchased the item (try acknowledging first).")
// })
// })
// // eslint-disable-next-line func-names
// .catch((e) => {
// log(e.message)
// // Also call show if hasEnrolledInstrument throws.
// request
// .show()
// .then(handlePaymentResponse)
// // eslint-disable-next-line no-shadow,func-names
// .catch((e) => {
// log(JSON.stringify(e, undefined, 2))
// log(e)
// })
// })
// }
}
function buySubscription() {
trigger('subscription', (token) => {
trigger('subscription', (token: any) => {
buyStatus = 'Purchase processing..'
acknowledge(token, 'repeatable', () => {
user.value.subscribed = true
updateUser()
if (user.value) {
user.value.subscribed = true
updateUser()
}
// gtag.js
purchase({
transaction_id: token,
affiliation: 'HangTime',
value: item?.value,
currency: item?.currency,
value: Number(item?.price?.value),
// currency: item?.price.currency,
tax: 0,
shipping: 0,
items: [
{
item_id: 'subscription',
item_name: 'Subscription',
affiliation: 'HangTime',
currency: item?.currency,
price: item?.value,
id: 'subscription',
name: 'Subscription',
brand: 'HangTime',
// currency: item?.price.currency,
price: item?.price?.value,
quantity: 1
}
]
Expand Down Expand Up @@ -330,12 +347,12 @@ useHead({
<p v-if="user?.completed" class="mb-4">{{ time(user.completed.time) }} minutes.</p>
<v-row class="text-center">
<v-col cols="12">
<div v-if="canSubscribe">
<div v-if="canSubscribe !== undefined">
<div class="text-h5 mb-6">{{ price }}</div>
<v-btn
color="primary"
x-large
:disabled="disabled || (user && user.subscribed)"
:disabled="disabled || !!(user && user.subscribed)"
@click="buySubscription"
>
<v-icon left>$cashMultiple</v-icon>
Expand Down

0 comments on commit d52c4b2

Please sign in to comment.