import React, { useRef, useEffect } from "react"; import { client, hostedFields, Client, HostedFields } from "braintree-web"; const BT_STYLES = { ":focus": { color: "blue", }, ".valid": { color: "green", }, ".invalid": { color: "red", }, }; const BT_PAYMENT_FIELDS = { number: { container: "#card-number", placeholder: "4111 1111 1111 1111", }, cvv: { container: "#cvv", placeholder: "123", }, expirationDate: { container: "#expiration-date", placeholder: "10/2022", }, }; const PaymentModal: React.FC<{}> = () => { const bt = useRef(); const form = useRef(); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (!form.current || !bt.current) return; const response = await form.current.tokenize(); console.log({ response }); }; useEffect(() => { const initializeBt = async () => { try { bt.current = await client.create({ authorization: "sandbox_246jdjxq_8h7hm5rvngkykjds", }); form.current = await hostedFields.create({ client: bt.current!!, fields: BT_PAYMENT_FIELDS, styles: BT_STYLES, }); } catch (err) { console.error(err); } }; initializeBt(); }, []); return (
); }; export default PaymentModal;