Skip to content

Commit

Permalink
Merge branch 'master' of github.com:paytaca/paytaca-pos
Browse files Browse the repository at this point in the history
  • Loading branch information
joemarct committed Nov 7, 2024
2 parents b5a47b0 + 9f0024f commit 74f477c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 79 deletions.
17 changes: 16 additions & 1 deletion src/components/MainFooter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@
import { useWalletStore } from 'src/stores/wallet';
import { useQuasar } from 'quasar'
import { useRouter } from 'vue-router'
import { defineComponent, computed } from 'vue'
import { defineComponent, computed, onMounted } from 'vue'
import SetAmountFormDialog from 'src/components/SetAmountFormDialog.vue'
import { Wallet } from 'src/wallet'
export default defineComponent({
Expand All @@ -61,6 +62,20 @@ export default defineComponent({
const selectedCurrency = computed(() => walletStore.preferences.selectedCurrency)
async function generateFirstReceivingAddress () {
const wallet = new Wallet({
xPubKey: walletStore.xPubKey,
walletHash: walletStore.walletHash,
posId: walletStore.posId,
})
const addressSet = await wallet.generateReceivingAddress(1, { skipSubscription: false })
return addressSet.receiving
}
onMounted(async () => {
const firstReceivingAddress = await generateFirstReceivingAddress()
walletStore.$patch({ firstReceivingAddress })
})
function promptAmount () {
$q.dialog({
component: SetAmountFormDialog,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,27 @@
/>
</div>
<div>
<div>Receiving address</div>
<div class="row items-center">
<div class="q-space">Receiving address</div>
<q-btn
flat
:loading="updatingReceivingAddress"
:disable="updatingReceivingAddress"
no-caps label="Change"
padding="xs sm"
class="text-underline q-r-mr-sm"
@click="() => updateReceivingAddress()"
/>
</div>
<q-input
readonly
dense outlined
:disable="loading"
autogrow
v-model="formData.receivingAddress"
:error="Boolean(formErrors?.receivingAddress)"
:error-message="formErrors?.receivingAddress"
>
<template v-slot:append>
<q-btn
flat
icon="refresh"
padding="sm"
@click="() => updateReceivingAddress()"
/>
</template>
</q-input>
</div>

Expand Down Expand Up @@ -92,9 +96,10 @@
import { Product } from 'src/marketplace/objects'
import { backend } from 'src/marketplace/backend'
import { errorParser } from 'src/marketplace/utils'
import { asyncSleep } from 'src/wallet/utils'
import { useMarketplaceStore } from 'src/stores/marketplace'
import { useAddressesStore } from 'src/stores/addresses'
import { defineComponent, ref } from 'vue'
import { defineComponent, onMounted, ref } from 'vue'
import UploadImageField from 'src/components/marketplace/UploadImageField.vue'
import ProductSearchPanel from 'src/components/marketplace/ProductSearchPanel.vue'
Expand Down Expand Up @@ -122,23 +127,49 @@ export default defineComponent({
subscribeProducts: [].map(Product.parse)
})
function updateReceivingAddress() {
let address = formData.value.receivingAddress
const opts = addressesStore.addressSets
.map(addressSet => addressSet?.receiving)
.filter((e, i, s) => s.indexOf(e) === i)
.filter(Boolean)
if (opts?.length <= 0) return
onMounted(() => {
if (!marketplaceStore.storefrontData?.id && !formData.value?.receivingAddress) {
updateReceivingAddress()
}
})
const index = opts.indexOf(address)
// we want to change address like it's rotating around the address sets stored
const rotatedOpts = [
...opts.slice(index+1),
...opts.slice(0, index+1),
]
formData.value.receivingAddress = rotatedOpts.find(addr => addr != address)
const updatingReceivingAddress = ref(false)
async function updateReceivingAddress() {
try {
updatingReceivingAddress.value = true
let address = formData.value.receivingAddress
if (!addressesStore.addressSets.length) await addressesStore.fillAddressSets().catch(console.error)
else await asyncSleep(250).catch(console.error)
const opts = addressesStore.addressSets
.map(addressSet => addressSet?.receiving)
.filter((e, i, s) => s.indexOf(e) === i)
.filter(Boolean)
if (opts?.length <= 0) return
const index = opts.indexOf(address)
// we want to change address like it's rotating around the address sets stored
const rotatedOpts = [
...opts.slice(index+1),
...opts.slice(0, index+1),
]
const newAddress = rotatedOpts.find(addr => addr != address)
const noNewAddressError = 'Unable to find new address'
if (!newAddress) {
if (!formErrors.value.receivingAddress) {
formErrors.value.receivingAddress = noNewAddressError
}
return
}
formData.value.receivingAddress = newAddress
if (noNewAddressError == formErrors.value.receivingAddress) {
formErrors.value.receivingAddress = ''
}
} finally {
updatingReceivingAddress.value = false
}
}
const formErrors = ref({
Expand Down Expand Up @@ -208,6 +239,7 @@ export default defineComponent({
loading,
formData,
updatingReceivingAddress,
updateReceivingAddress,
formErrors,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export default defineComponent({
onMounted(() => fetchTransactions())
onMounted(() => walletStore.refetchSalesReport())
onMounted(() => walletStore.refetchMerchantInfo())
// onMounted(() => walletStore.refetchMerchantInfo())
onMounted(() => walletStore.refetchDeviceInfo())
onMounted(() => walletStore.refetchPreferences())
watch(() => [walletStore.walletHash], () => walletStore.refetchMerchantInfo())
// watch(() => [walletStore.walletHash], () => walletStore.refetchMerchantInfo())
watch(() => [walletStore.walletHash, walletStore.posId], () => walletStore.refetchDeviceInfo())
watch(() => [walletStore.walletHash], () => walletStore.refetchPreferences())
Expand Down
31 changes: 7 additions & 24 deletions src/pages/ReceivePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export default defineComponent({
const expiryDuration = currencyRateUpdateRate / 1000
const expirationTimestamp = Math.floor(currentTimestamp + expiryDuration)
const diffSeconds = networkTimeDiff.value ? networkTimeDiff.value / 1000 : 0
const adjustedExpirationTimestamp = expirationTimestamp + diffSeconds
const adjustedExpirationTimestamp = expirationTimestamp + diffSeconds
paymentUri += `&expires=${adjustedExpirationTimestamp}`
}
Expand All @@ -436,7 +436,8 @@ export default defineComponent({
const websocketUrl = `${process.env.WATCHTOWER_WEBSOCKET}/watch/bch`
const merchantReceivingAddress = addressSet.value?.receiving
const websocketInits = [
merchantReceivingAddress
merchantReceivingAddress,
walletStore.firstReceivingAddress,
]
.filter(Boolean)
.map(address => {
Expand All @@ -446,7 +447,9 @@ export default defineComponent({
}
})
const websockets = ref(websocketInits)
const isZerothAddress = walletStore.firstReceivingAddress === merchantReceivingAddress
const websockets = ref(isZerothAddress ? websocketInits.slice(0,1) : websocketInits)
const websocketsReady = computed(() => {
const readySockets = websockets.value.filter((websocket) => websocket.instance?.readyState === 1)
return readySockets.length === websockets.value.length
Expand Down Expand Up @@ -479,6 +482,7 @@ export default defineComponent({
if (newVal) {
closeWebsocket()
stopQrExpirationCountdown()
addressesStore.dequeueAddress()
setTimeout(() => triggerSecondConfetti.value = true, 1500)
}
})
Expand Down Expand Up @@ -557,23 +561,6 @@ export default defineComponent({
displayReceivedTransaction(transaction)
}
function updateClaimTxnAttr (txid) {
const posId = walletStore.posId
const key = `voucher_claim_${posId}`
const payload = {
wallet_hash: walletStore.merchantInfo?.walletHash,
value: "Voucher Claim",
remove: false,
txid,
key
}
const watchtowerTxnAttrUrl = `${process.env.WATCHTOWER_API}/transactions/attributes/`
axios.post(watchtowerTxnAttrUrl, payload)
.then(response => console.log('Added transaction attribute as voucher claim: ', response))
.catch(err => console.log('Error on adding transaction attribute as voucher claim: ', err))
}
function processLiveUpdate (data) {
const updateType = data?.update_type
let message = null
Expand All @@ -594,10 +581,6 @@ export default defineComponent({
qrScanned.value = false
refreshQrCountdown()
}
else if (updateType === 'voucher_claimed') {
if (!data?.txid || !data?.category) return
updateClaimTxnAttr(data.txid)
}
if (message) {
$q.notify({
Expand Down
40 changes: 14 additions & 26 deletions src/stores/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
sha256,
decodePaymentUri,
getPubkeyAt,
pubkeyToCashAddress,
} from 'src/wallet/utils';


Expand All @@ -16,11 +15,13 @@ export const useWalletStore = defineStore('wallet', {
walletHash: null,
xPubKey: null,
linkCode: null,
firstReceivingAddress: null,

deviceInfo: {
name: '',
posId: -1,
walletHash: null,
merchantId: null,
branchId: null,
linkedDevice: {
linkCode: '',
Expand Down Expand Up @@ -51,14 +52,6 @@ export const useWalletStore = defineStore('wallet', {
country: '',
longitude: null,
latitude: null,
},
vault: {
receiving: {
address: '',
pubkey: '',
},
address: '',
tokenAddress: '',
}
},

Expand Down Expand Up @@ -224,8 +217,6 @@ export const useWalletStore = defineStore('wallet', {
* @param {String} data.wallet_hash
* @param {String} data.primary_contact_number
*
* @param {String} data.receiving_pubkey
*
* @param {Object} [data.location]
* @param {String} data.location.landmark
* @param {String} data.location.location
Expand All @@ -234,10 +225,6 @@ export const useWalletStore = defineStore('wallet', {
* @param {String} data.location.country
* @param {String} data.location.longitude
* @param {String} data.location.latitude
*
* @param {Object} [data.vault]
* @param {String} data.vault.address
* @param {String} data.vault.token_address
*/
setMerchantInfo(data) {
const merchantInfo = {
Expand All @@ -254,22 +241,16 @@ export const useWalletStore = defineStore('wallet', {
longitude: data?.location?.longitude,
latitude: data?.location?.latitude,
},
vault: {
receiving: {
address: pubkeyToCashAddress(data?.receiving_pubkey),
pubkey: data?.receiving_pubkey,
},
address: data?.vault?.address,
tokenAddress: data?.vault?.token_address,
}
}

this.merchantInfo = merchantInfo
},
refetchMerchantInfo() {
if (!this.walletHash) return this.setMerchantInfo(null)
if (!this.walletHash || Number.isNaN(this.deviceInfo.merchantId)) return this.setMerchantInfo(null)
if (!this.deviceInfo.merchantId) return this.setMerchantInfo(null)

const watchtower = new Watchtower()
return watchtower.BCH._api.get(`paytacapos/merchants/${this.walletHash}/`)
return watchtower.BCH._api.get(`paytacapos/merchants/${this.deviceInfo.merchantId}/`)
.then(response => {
if (response?.data?.wallet_hash == this.walletHash) {
this.setMerchantInfo(response.data)
Expand All @@ -280,14 +261,17 @@ export const useWalletStore = defineStore('wallet', {
.catch(error => {
if (error?.response.status === 404) {
this.setMerchantInfo(null)
return
}
return Promise.reject(error)
})
},
/**
* @param {Object} data
* @param {String} data.wallet_hash
* @param {Number} data.posid
* @param {Number} [data.branch_id]
* @param {Number} data.branch_id
* @param {Number} data.merchant_id
* @param {Object} [data.linked_device]
* @param {String} [data.linked_device.link_code]
* @param {String} [data.linked_device.name]
Expand All @@ -307,6 +291,7 @@ export const useWalletStore = defineStore('wallet', {
walletHash: data?.wallet_hash,
posId: data?.posid,
branchId: data?.branch_id,
merchantId: data?.merchant_id,
linkedDevice: {
linkCode: data?.linked_device?.link_code,
name: data?.linked_device?.name,
Expand Down Expand Up @@ -341,10 +326,13 @@ export const useWalletStore = defineStore('wallet', {
.catch(error => {
if (error?.response.status === 404) {
this.setDeviceInfo(null)
return
}
return Promise.reject(error)
})
.finally(() => {
this.refetchBranchInfo()
this.refetchMerchantInfo()
})
},
confirmUnlinkRequest() {
Expand Down

0 comments on commit 74f477c

Please sign in to comment.