Skip to content

Commit

Permalink
adding capture action
Browse files Browse the repository at this point in the history
  • Loading branch information
jdivock-stripe committed Mar 11, 2022
1 parent 45e1708 commit b20b63a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions example/src/screens/CollectCardPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ import ListItem from '../components/ListItem';
import { LogContext } from '../components/LogContext';
import { API_URL } from '../Config';

const capturePaymentIntent = async (id: string) => {
try {
const response = await fetch(`${API_URL}/capture_payment_intent`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id }),
});
const pi = await response.json();
return { paymentIntent: pi, error: null };
} catch (e) {
return { error: e };
}
};

export default function CollectCardPaymentScreen() {
const [inputValues, setInputValues] = useState<{
amount: string;
Expand Down Expand Up @@ -218,6 +234,39 @@ export default function CollectCardPaymentScreen() {
},
],
});
_capturePayment(paymentIntentId);
}
};

const _capturePayment = async (paymentIntentId: string) => {
addLogs({
name: 'Capture Payment',
events: [{ name: 'Capture', description: 'terminal.capturePayment' }],
});

const { paymentIntent, error } = await capturePaymentIntent(
paymentIntentId
);
if (error) {
addLogs({
name: 'Capture Payment',
events: [
{
name: error.code,
description: error.message,
},
],
});
} else if (paymentIntent) {
addLogs({
name: 'Capture Payment',
events: [
{
name: 'Finished',
description: 'terminal.paymentIntentId: ' + paymentIntent.id,
},
],
});
}
};

Expand Down

0 comments on commit b20b63a

Please sign in to comment.