Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions example/src/screens/TwintPaymentScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React, { useState } from 'react';
import { Alert, StyleSheet, TextInput } from 'react-native';
import { useConfirmPayment } from '@stripe/stripe-react-native';
import Button from '../components/Button';
import PaymentScreen from '../components/PaymentScreen';
import { API_URL } from '../Config';
import { colors } from '../colors';

export default function TwintPaymentScreen() {
const [email, setEmail] = useState('');
const { confirmPayment, loading } = useConfirmPayment();

const fetchPaymentIntentClientSecret = async () => {
const response = await fetch(`${API_URL}/create-payment-intent`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email,
currency: 'chf', // TWINT is Swiss, so CHF is typical
items: ['id-1'],
payment_method_types: ['twint'],
}),
});
const { clientSecret, error } = await response.json();
return { clientSecret, error };
};

const handlePayPress = async () => {
const { clientSecret, error: clientSecretError } =
await fetchPaymentIntentClientSecret();

if (clientSecretError) {
Alert.alert(`Error`, clientSecretError);
return;
}

const { error, paymentIntent } = await confirmPayment(clientSecret, {
paymentMethodType: 'Twint',
});

if (error) {
Alert.alert(`Error code: ${error.code}`, error.message);
} else if (paymentIntent) {
Alert.alert(
'Success',
`The payment was confirmed successfully! currency: ${paymentIntent.currency}`
);
}
};

return (
<PaymentScreen>
<TextInput
autoCapitalize="none"
placeholder="E-mail"
keyboardType="email-address"
onChange={(value) => setEmail(value.nativeEvent.text)}
style={styles.input}
/>
<Button
variant="primary"
onPress={handlePayPress}
title="Pay with TWINT"
accessibilityLabel="Pay with TWINT"
loading={loading}
/>
</PaymentScreen>
);
}

const styles = StyleSheet.create({
input: {
height: 44,
borderBottomColor: colors.slate,
borderBottomWidth: 1.5,
marginBottom: 20,
},
});
12 changes: 11 additions & 1 deletion src/types/PaymentIntent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export type ConfirmParams =
| PayPalParams
| AffirmParams
| CashAppParams
| RevolutPayParams;
| RevolutPayParams
| TwintParams;

export type ConfirmOptions = PaymentMethod.ConfirmOptions;

Expand Down Expand Up @@ -297,6 +298,15 @@ export type RevolutPayParams = {
};
};

export type TwintParams = {
paymentMethodType: 'Twint';
paymentMethodData?: {
billingDetails?: BillingDetails;
mandateData?: MandateData;
metadata?: MetaData;
};
};

export type CollectBankAccountParams = {
paymentMethodType: 'USBankAccount';
paymentMethodData: {
Expand Down