Skip to content

Commit 41cc4af

Browse files
authored
[PAY-1629] Purchase flow cleanup (#3873)
1 parent 65b2945 commit 41cc4af

File tree

20 files changed

+135
-98
lines changed

20 files changed

+135
-98
lines changed

packages/common/src/store/buy-usdc/sagas.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import { initializeStripeModal } from 'store/ui/stripe-modal/slice'
1717
import {
1818
buyUSDCFlowFailed,
1919
buyUSDCFlowSucceeded,
20-
onRampCanceled,
21-
onRampOpened,
22-
onPurchaseStarted,
23-
onRampSucceeded
20+
onrampCanceled,
21+
onrampOpened,
22+
purchaseStarted,
23+
onrampSucceeded
2424
} from './slice'
2525
import { USDCOnRampProvider } from './types'
2626
import { getUSDCUserBank } from './utils'
@@ -69,12 +69,12 @@ function* purchaseStep({
6969
)
7070
const initialBalance = initialAccountInfo.amount
7171

72-
yield* put(onPurchaseStarted())
72+
yield* put(purchaseStarted())
7373

7474
// Wait for on ramp finish
7575
const result = yield* race({
76-
success: take(onRampSucceeded),
77-
canceled: take(onRampCanceled)
76+
success: take(onrampSucceeded),
77+
canceled: take(onrampCanceled)
7878
})
7979

8080
// If the user didn't complete the on ramp flow, return early
@@ -119,7 +119,7 @@ function* doBuyUSDC({
119119
provider,
120120
purchaseInfo: { desiredAmount }
121121
}
122-
}: ReturnType<typeof onRampOpened>) {
122+
}: ReturnType<typeof onrampOpened>) {
123123
const reportToSentry = yield* getContext('reportToSentry')
124124
const { track, make } = yield* getContext('analytics')
125125

@@ -136,8 +136,8 @@ function* doBuyUSDC({
136136
amount: (desiredAmount / 100).toString(),
137137
destinationCurrency: 'usdc',
138138
destinationWallet: userBank.toString(),
139-
onRampCanceled,
140-
onRampSucceeded
139+
onrampCanceled,
140+
onrampSucceeded
141141
})
142142
)
143143

@@ -201,7 +201,7 @@ function* doBuyUSDC({
201201
}
202202

203203
function* watchOnRampOpened() {
204-
yield takeLatest(onRampOpened, doBuyUSDC)
204+
yield takeLatest(onrampOpened, doBuyUSDC)
205205
}
206206

207207
export default function sagas() {

packages/common/src/store/buy-usdc/slice.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const slice = createSlice({
3131
name: 'buy-usdc',
3232
initialState,
3333
reducers: {
34-
onRampOpened: (
34+
onrampOpened: (
3535
state,
3636
action: PayloadAction<{
3737
purchaseInfo: PurchaseInfo
@@ -44,19 +44,18 @@ const slice = createSlice({
4444
state.provider = action.payload.provider
4545
state.onSuccess = action.payload.onSuccess
4646
},
47-
onPurchaseStarted: (state) => {
47+
purchaseStarted: (state) => {
4848
state.stage = BuyUSDCStage.PURCHASING
4949
},
50-
onRampCanceled: (state) => {
50+
onrampCanceled: (state) => {
5151
if (state.stage === BuyUSDCStage.PURCHASING) {
5252
state.stage = BuyUSDCStage.CANCELED
5353
}
5454
},
55-
onRampSucceeded: (state) => {
55+
onrampSucceeded: (state) => {
5656
state.stage = BuyUSDCStage.CONFIRMING_PURCHASE
5757
},
5858
buyUSDCFlowFailed: (state) => {
59-
// TODO: Probably want to pass error in action payload
6059
state.error = new Error('USDC purchase failed')
6160
},
6261
buyUSDCFlowSucceeded: (state) => {
@@ -74,10 +73,10 @@ const slice = createSlice({
7473
export const {
7574
buyUSDCFlowFailed,
7675
buyUSDCFlowSucceeded,
77-
onRampOpened,
78-
onPurchaseStarted,
79-
onRampSucceeded,
80-
onRampCanceled,
76+
onrampOpened,
77+
purchaseStarted,
78+
onrampSucceeded,
79+
onrampCanceled,
8180
stripeSessionStatusChanged
8281
} = slice.actions
8382

packages/common/src/store/purchase-content/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export {
55
export * as purchaseContentSelectors from './selectors'
66
export { default as purchaseContentSagas } from './sagas'
77
export * from './types'
8+
export * from './utils'

packages/common/src/store/purchase-content/sagas.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { accountSelectors } from 'store/account'
1414
import {
1515
buyUSDCFlowFailed,
1616
buyUSDCFlowSucceeded,
17-
onRampOpened,
18-
onRampCanceled
17+
onrampOpened,
18+
onrampCanceled
1919
} from 'store/buy-usdc/slice'
2020
import { USDCOnRampProvider } from 'store/buy-usdc/types'
2121
import { getUSDCUserBank } from 'store/buy-usdc/utils'
@@ -29,10 +29,11 @@ import { pollPremiumTrack } from '../premium-content/sagas'
2929
import { updatePremiumTrackStatus } from '../premium-content/slice'
3030

3131
import {
32-
onBuyUSDC,
33-
onPurchaseConfirmed,
34-
onPurchaseSucceeded,
35-
onUSDCBalanceSufficient,
32+
buyUSDC,
33+
purchaseCanceled,
34+
purchaseConfirmed,
35+
purchaseSucceeded,
36+
usdcBalanceSufficient,
3637
purchaseContentFlowFailed,
3738
startPurchaseContentFlow
3839
} from './slice'
@@ -159,9 +160,9 @@ function* doStartPurchaseContentFlow({
159160

160161
// buy USDC if necessary
161162
if (initialBalance.lt(new BN(price).mul(BN_USDC_CENT_WEI))) {
162-
yield* put(onBuyUSDC())
163+
yield* put(buyUSDC())
163164
yield* put(
164-
onRampOpened({
165+
onrampOpened({
165166
provider: USDCOnRampProvider.STRIPE,
166167
purchaseInfo: {
167168
desiredAmount: price
@@ -171,17 +172,22 @@ function* doStartPurchaseContentFlow({
171172

172173
const result = yield* race({
173174
success: take(buyUSDCFlowSucceeded),
174-
canceled: take(onRampCanceled),
175+
canceled: take(onrampCanceled),
175176
failed: take(buyUSDCFlowFailed)
176177
})
177178

178-
if (result.canceled || result.failed) {
179-
// Return early for failure or cancellation
179+
// Return early for failure or cancellation
180+
if (result.canceled) {
181+
yield* put(purchaseCanceled())
182+
return
183+
}
184+
if (result.failed) {
185+
yield* put(purchaseContentFlowFailed())
180186
return
181187
}
182188
}
183189

184-
yield* put(onUSDCBalanceSufficient())
190+
yield* put(usdcBalanceSufficient())
185191

186192
const { blocknumber, splits } = yield* getPurchaseConfig({
187193
contentId,
@@ -195,13 +201,13 @@ function* doStartPurchaseContentFlow({
195201
splits,
196202
type: 'track'
197203
})
198-
yield* put(onPurchaseSucceeded())
204+
yield* put(purchaseSucceeded())
199205

200206
// confirm purchase
201207
yield* pollForPurchaseConfirmation({ contentId, contentType })
202208

203209
// finish
204-
yield* put(onPurchaseConfirmed())
210+
yield* put(purchaseConfirmed())
205211

206212
yield* put(
207213
setVisibility({

packages/common/src/store/purchase-content/slice.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,35 @@ const slice = createSlice({
4242
state.contentType = action.payload.contentType || ContentType.TRACK
4343
state.onSuccess = action.payload.onSuccess
4444
},
45-
onBuyUSDC: (state) => {
45+
buyUSDC: (state) => {
4646
state.stage = PurchaseContentStage.BUY_USDC
4747
},
48-
onUSDCBalanceSufficient: (state) => {
48+
usdcBalanceSufficient: (state) => {
4949
state.stage = PurchaseContentStage.PURCHASING
5050
},
51-
onPurchaseCanceled: (state) => {
52-
state.error = new Error('Content purchase canceled')
51+
purchaseCanceled: (state) => {
5352
state.stage = PurchaseContentStage.CANCELED
5453
},
55-
onPurchaseSucceeded: (state) => {
54+
purchaseSucceeded: (state) => {
5655
state.stage = PurchaseContentStage.CONFIRMING_PURCHASE
5756
},
58-
onPurchaseConfirmed: (state) => {
57+
purchaseConfirmed: (state) => {
5958
state.stage = PurchaseContentStage.FINISH
6059
},
61-
6260
purchaseContentFlowFailed: (state) => {
63-
// TODO: Probably want to pass error in action payload
6461
state.error = new Error('Content purchase failed')
65-
}
62+
},
63+
cleanup: () => initialState
6664
}
6765
})
6866

6967
export const {
7068
startPurchaseContentFlow,
71-
onBuyUSDC,
72-
onUSDCBalanceSufficient,
73-
onPurchaseSucceeded,
74-
onPurchaseConfirmed,
75-
onPurchaseCanceled,
69+
buyUSDC,
70+
usdcBalanceSufficient,
71+
purchaseSucceeded,
72+
purchaseConfirmed,
73+
purchaseCanceled,
7674
purchaseContentFlowFailed
7775
} = slice.actions
7876

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { PurchaseContentStage } from './types'
2+
3+
export const isContentPurchaseInProgress = (stage: PurchaseContentStage) => {
4+
return [
5+
PurchaseContentStage.BUY_USDC,
6+
PurchaseContentStage.PURCHASING,
7+
PurchaseContentStage.CONFIRMING_PURCHASE
8+
].includes(stage)
9+
}

packages/common/src/store/ui/buy-audio/slice.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ const slice = createSlice({
129129
state.provider = action.payload.provider
130130
state.onSuccess = action.payload.onSuccess
131131
},
132-
onRampOpened: (state, _action: PayloadAction<PurchaseInfo>) => {
132+
onrampOpened: (state, _action: PayloadAction<PurchaseInfo>) => {
133133
state.stage = BuyAudioStage.PURCHASING
134134
},
135-
onRampCanceled: (state) => {
135+
onrampCanceled: (state) => {
136136
if (state.stage === BuyAudioStage.PURCHASING) {
137137
state.error = true
138138
}
139139
},
140-
onRampSucceeded: (state) => {
140+
onrampSucceeded: (state) => {
141141
state.stage = BuyAudioStage.CONFIRMING_PURCHASE
142142
},
143143
swapStarted: (state) => {
@@ -175,9 +175,9 @@ export const {
175175
cacheTransactionFees,
176176
clearFeesCache,
177177
startBuyAudioFlow,
178-
onRampOpened,
179-
onRampSucceeded,
180-
onRampCanceled,
178+
onrampOpened,
179+
onrampSucceeded,
180+
onrampCanceled,
181181
swapStarted,
182182
swapCompleted,
183183
transferStarted,

packages/common/src/store/ui/stripe-modal/sagas.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,20 @@ function* handleStripeSessionChanged({
3030
payload: { status }
3131
}: ReturnType<typeof stripeSessionStatusChanged>) {
3232
if (status === 'fulfillment_complete') {
33-
const { onRampSucceeded } = yield* select(getStripeModalState)
34-
if (onRampSucceeded) {
35-
yield* put(onRampSucceeded)
33+
const { onrampSucceeded } = yield* select(getStripeModalState)
34+
if (onrampSucceeded) {
35+
yield* put(onrampSucceeded)
3636
}
3737
yield* put(setVisibility({ modal: 'StripeOnRamp', visible: false }))
3838
}
3939
}
4040

4141
function* handleCancelStripeOnramp() {
42-
const { onRampCanceled } = yield* select(getStripeModalState)
42+
const { onrampCanceled } = yield* select(getStripeModalState)
4343
yield* put(setVisibility({ modal: 'StripeOnRamp', visible: false }))
4444

45-
if (onRampCanceled) {
46-
yield* put(onRampCanceled)
45+
if (onrampCanceled) {
46+
yield* put(onrampCanceled)
4747
}
4848
}
4949

packages/common/src/store/ui/stripe-modal/slice.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ type InitializeStripeModalPayload = {
1010
amount: string
1111
destinationCurrency: StripeDestinationCurrencyType
1212
destinationWallet: string
13-
onRampSucceeded: Action
14-
onRampCanceled: Action
13+
onrampSucceeded: Action
14+
onrampCanceled: Action
1515
}
1616

1717
const initialState: StripeModalState = {}
@@ -25,8 +25,8 @@ const slice = createSlice({
2525
action: PayloadAction<InitializeStripeModalPayload>
2626
) => {
2727
state.stripeSessionStatus = 'initialized'
28-
state.onRampSucceeded = action.payload.onRampSucceeded
29-
state.onRampCanceled = action.payload.onRampCanceled
28+
state.onrampSucceeded = action.payload.onrampSucceeded
29+
state.onrampCanceled = action.payload.onrampCanceled
3030
},
3131
stripeSessionCreated: (
3232
state,

packages/common/src/store/ui/stripe-modal/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export type StripeSessionStatus =
1010
export type StripeDestinationCurrencyType = 'sol' | 'usdc'
1111

1212
export type StripeModalState = {
13-
onRampSucceeded?: Action
14-
onRampCanceled?: Action
13+
onrampSucceeded?: Action
14+
onrampCanceled?: Action
1515
stripeSessionStatus?: StripeSessionStatus
1616
stripeClientSecret?: string
1717
}

packages/web/src/components/buy-audio-modal/components/CoinbaseBuyAudioButton.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import { getRootSolanaAccount } from 'services/audius-backend/BuyAudio'
1717
import styles from './CoinbaseBuyAudioButton.module.css'
1818

1919
const {
20-
onRampOpened,
21-
onRampCanceled,
22-
onRampSucceeded,
20+
onrampOpened,
21+
onrampCanceled,
22+
onrampSucceeded,
2323
calculateAudioPurchaseInfo
2424
} = buyAudioActions
2525
const { getAudioPurchaseInfo, getAudioPurchaseInfoStatus } = buyAudioSelectors
@@ -43,10 +43,10 @@ export const CoinbaseBuyAudioButton = () => {
4343
const isDisabled = purchaseInfoStatus === Status.LOADING || belowSolThreshold
4444

4545
const handleExit = useCallback(() => {
46-
dispatch(onRampCanceled())
46+
dispatch(onrampCanceled())
4747
}, [dispatch])
4848
const handleSuccess = useCallback(() => {
49-
dispatch(onRampSucceeded())
49+
dispatch(onrampSucceeded())
5050
}, [dispatch])
5151

5252
const handleClick = useCallback(() => {
@@ -60,7 +60,7 @@ export const CoinbaseBuyAudioButton = () => {
6060
onSuccess: handleSuccess,
6161
onExit: handleExit
6262
})
63-
dispatch(onRampOpened(purchaseInfo))
63+
dispatch(onrampOpened(purchaseInfo))
6464
coinbasePay.open()
6565
} else if (purchaseInfoStatus === Status.IDLE) {
6666
// Generally only possible if `amount` is still undefined,

0 commit comments

Comments
 (0)