-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d09fc6e
commit d2630a8
Showing
13 changed files
with
281 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
.../mobile/src/components/premium-track-purchase-drawer/StripePurchaseConfirmationButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useCallback } from 'react' | ||
|
||
import { accountSelectors } from '@audius/common' | ||
import { useSelector } from 'react-redux' | ||
|
||
import { Button } from 'app/components/core' | ||
import { useNavigation } from 'app/hooks/useNavigation' | ||
import { createStripeSession, getUSDCUserBank } from 'app/services/buyCrypto' | ||
import { useThemeColors } from 'app/utils/theme' | ||
|
||
const { getAccountERCWallet } = accountSelectors | ||
|
||
const messages = { | ||
buy: (price: string) => `Buy $${price}` | ||
} | ||
|
||
type StripePurchaseConfirmationButtonProps = { | ||
price: string | ||
} | ||
|
||
export const StripePurchaseConfirmationButton = ({ | ||
price | ||
}: StripePurchaseConfirmationButtonProps) => { | ||
const navigation = useNavigation() | ||
const { specialLightGreen1 } = useThemeColors() | ||
const ethWallet = useSelector(getAccountERCWallet) | ||
|
||
const handleBuyPress = useCallback(async () => { | ||
try { | ||
if (ethWallet === null) { | ||
throw new Error('Stripe session creation failed: no eth wallet found') | ||
} | ||
const usdcUserBank = await getUSDCUserBank(ethWallet) | ||
if (usdcUserBank === undefined) { | ||
throw new Error( | ||
'Stripe session creation failed: could not get USDC user bank' | ||
) | ||
} | ||
const res = await createStripeSession({ | ||
amount: price, | ||
destinationWallet: usdcUserBank.toString() | ||
}) | ||
if (res === undefined || res.client_secret === undefined) { | ||
throw new Error( | ||
'Stripe session creation failed: could not get client secret' | ||
) | ||
} | ||
navigation.navigate('StripeOnrampEmbed', { | ||
clientSecret: res.client_secret | ||
}) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
}, [ethWallet, navigation, price]) | ||
|
||
return ( | ||
<Button | ||
onPress={handleBuyPress} | ||
title={messages.buy(price)} | ||
variant={'primary'} | ||
size='large' | ||
color={specialLightGreen1} | ||
fullWidth | ||
/> | ||
) | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/mobile/src/components/stripe-onramp-embed/StripeOnrampEmbed.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { useCallback } from 'react' | ||
|
||
import { StyleSheet, View } from 'react-native' | ||
import { WebView } from 'react-native-webview' | ||
|
||
import { useIsUSDCEnabled } from 'app/hooks/useIsUSDCEnabled' | ||
import { useRoute } from 'app/hooks/useRoute' | ||
import { env } from 'app/services/env' | ||
|
||
const STRIPE_PUBLISHABLE_KEY = env.REACT_APP_STRIPE_CLIENT_PUBLISHABLE_KEY | ||
|
||
const styles = StyleSheet.create({ | ||
root: { | ||
height: '100%', | ||
width: '100%' | ||
} | ||
}) | ||
|
||
export const StripeOnrampEmbed = () => { | ||
const { params } = useRoute<'StripeOnrampEmbed'>() | ||
const { clientSecret } = params | ||
const isUSDCEnabled = useIsUSDCEnabled() | ||
|
||
const handleSessionUpdate = useCallback((event) => { | ||
if (event?.payload?.session?.status) { | ||
console.log(`Stripe Session Update ${event.payload.session.status}`) | ||
} | ||
}, []) | ||
|
||
const html = ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Crypto Onramp</title> | ||
<meta name="description" content="A demo of hosted onramp" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<script src="https://js.stripe.com/v3/"></script> | ||
<script type="text/javascript" src="https://crypto-js.stripe.com/crypto-onramp-outer.js"></script> | ||
</head> | ||
<body> | ||
<div id="onramp-element" /> | ||
<script type="text/javascript"> | ||
const handleSessionUpdate = (event) => { | ||
window.ReactNativeWebView.postMessage(event) | ||
} | ||
try { | ||
const onramp = new window.StripeOnramp("${STRIPE_PUBLISHABLE_KEY}") | ||
const session = onramp.createSession({clientSecret:"${clientSecret}"}) | ||
session.mount('#onramp-element') | ||
session.addEventListener('onramp_session_updated', handleSessionUpdate) | ||
} catch (e) { | ||
window.ReactNativeWebView.postMessage(e) | ||
} | ||
</script> | ||
</body> | ||
</html> | ||
` | ||
|
||
if (!STRIPE_PUBLISHABLE_KEY) { | ||
console.error('Stripe publishable key not found') | ||
return null | ||
} | ||
|
||
if (!clientSecret) { | ||
console.error('Stripe client secret not found') | ||
return null | ||
} | ||
|
||
if (!isUSDCEnabled) return null | ||
|
||
return ( | ||
<View style={styles.root}> | ||
<WebView | ||
source={{ html }} | ||
scrollEnabled={false} | ||
onError={(syntheticEvent) => { | ||
const { nativeEvent } = syntheticEvent | ||
console.error('Stripe WebView onError: ', nativeEvent) | ||
}} | ||
onMessage={handleSessionUpdate} | ||
/> | ||
</View> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './StripeOnrampEmbed' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.