-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add klarna setup intent to example screen
- Loading branch information
1 parent
e4634f3
commit 09d50b7
Showing
1 changed file
with
68 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import React, { useState } from 'react'; | ||
import { Alert, StyleSheet, TextInput } from 'react-native'; | ||
import { useConfirmPayment } from '@stripe/stripe-react-native'; | ||
import { | ||
useConfirmPayment, | ||
useConfirmSetupIntent, | ||
} from '@stripe/stripe-react-native'; | ||
import Button from '../components/Button'; | ||
import PaymentScreen from '../components/PaymentScreen'; | ||
import { API_URL } from '../Config'; | ||
|
@@ -9,9 +12,10 @@ import { colors } from '../colors'; | |
export default function KlarnaPaymentScreen() { | ||
const [email, setEmail] = useState(''); | ||
const { confirmPayment, loading } = useConfirmPayment(); | ||
const { confirmSetupIntent, loading: setupLoading } = useConfirmSetupIntent(); | ||
|
||
const fetchPaymentIntentClientSecret = async () => { | ||
const response = await fetch(`${API_URL}/create-payment-intent`, { | ||
const fetchClientSecret = async (intentType: 'setup' | 'payment') => { | ||
const response = await fetch(`${API_URL}/create-${intentType}-intent`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
|
@@ -29,8 +33,9 @@ export default function KlarnaPaymentScreen() { | |
}; | ||
|
||
const handlePayPress = async () => { | ||
const { clientSecret, error: clientSecretError } = | ||
await fetchPaymentIntentClientSecret(); | ||
const { clientSecret, error: clientSecretError } = await fetchClientSecret( | ||
'payment' | ||
); | ||
|
||
if (clientSecretError) { | ||
Alert.alert(`Error`, clientSecretError); | ||
|
@@ -71,6 +76,56 @@ export default function KlarnaPaymentScreen() { | |
} | ||
}; | ||
|
||
const handleSetupPress = async () => { | ||
const { clientSecret, error: clientSecretError } = await fetchClientSecret( | ||
'setup' | ||
); | ||
|
||
if (clientSecretError) { | ||
Alert.alert(`Error`, clientSecretError); | ||
return; | ||
} | ||
|
||
const { error, setupIntent } = await confirmSetupIntent(clientSecret, { | ||
paymentMethodType: 'Klarna', | ||
paymentMethodData: { | ||
mandateData: { | ||
customerAcceptance: { | ||
online: { | ||
ipAddress: '1.1.1.1', | ||
userAgent: 'my-agent', | ||
}, | ||
}, | ||
}, | ||
shippingDetails: { | ||
address: { | ||
city: 'Houston', | ||
country: 'US', | ||
line1: '1459 Circle Drive', | ||
state: 'Texas', | ||
postalCode: '77063', | ||
}, | ||
email: '[email protected]', | ||
name: 'John Doe', | ||
}, | ||
billingDetails: { | ||
email: '[email protected]', | ||
address: { | ||
country: 'US', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (error) { | ||
Alert.alert(`Error code: ${error.code}`, error.message); | ||
console.log('Setup confirmation error', error.message); | ||
} else if (setupIntent) { | ||
Alert.alert('Success', `Status: ${setupIntent.status}`); | ||
console.log('Success from promise', setupIntent); | ||
} | ||
}; | ||
|
||
return ( | ||
<PaymentScreen> | ||
<TextInput | ||
|
@@ -88,6 +143,14 @@ export default function KlarnaPaymentScreen() { | |
accessibilityLabel="Pay" | ||
loading={loading} | ||
/> | ||
|
||
<Button | ||
variant="primary" | ||
onPress={handleSetupPress} | ||
title="Setup" | ||
accessibilityLabel="Setup" | ||
loading={setupLoading} | ||
/> | ||
</PaymentScreen> | ||
); | ||
} | ||
|