Merge branch 'clone' into 'main'
feat: add yandex metric, smartlook manage, add payment buttons See merge request witapp/aura-webapp!9
This commit is contained in:
commit
a120f4a387
123
src/components/StripePage/ApplePayButton/index.tsx
Normal file
123
src/components/StripePage/ApplePayButton/index.tsx
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import {
|
||||||
|
PaymentRequestButtonElement,
|
||||||
|
useStripe,
|
||||||
|
useElements,
|
||||||
|
} from "@stripe/react-stripe-js";
|
||||||
|
import { PaymentRequest } from "@stripe/stripe-js";
|
||||||
|
import { ISubscriptionPlan } from "@/api/resources/SubscriptionPlans";
|
||||||
|
import { useAuth } from "@/auth";
|
||||||
|
import { useApi } from "@/api";
|
||||||
|
import styles from "./styles.module.css";
|
||||||
|
|
||||||
|
interface ApplePayButtonProps {
|
||||||
|
activeSubPlan: ISubscriptionPlan | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ApplePayButton({ activeSubPlan }: ApplePayButtonProps) {
|
||||||
|
const stripe = useStripe();
|
||||||
|
const elements = useElements();
|
||||||
|
const api = useApi();
|
||||||
|
const { token } = useAuth();
|
||||||
|
const [paymentRequest, setPaymentRequest] = useState<PaymentRequest | null>(
|
||||||
|
null
|
||||||
|
);
|
||||||
|
// const [test, setTest] = useState<string>("");
|
||||||
|
|
||||||
|
const getAmountFromSubPlan = (subPlan: ISubscriptionPlan) => {
|
||||||
|
if (subPlan.trial) {
|
||||||
|
return subPlan.trial.price_cents;
|
||||||
|
}
|
||||||
|
return subPlan.price_cents;
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!stripe || !elements || !activeSubPlan) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pr = stripe.paymentRequest({
|
||||||
|
country: "US",
|
||||||
|
currency: "usd",
|
||||||
|
total: {
|
||||||
|
label: activeSubPlan.name || "Subscription",
|
||||||
|
amount: getAmountFromSubPlan(activeSubPlan),
|
||||||
|
},
|
||||||
|
requestPayerName: true,
|
||||||
|
requestPayerEmail: true,
|
||||||
|
});
|
||||||
|
console.log("paymentRequest: ", pr);
|
||||||
|
|
||||||
|
// Check the availability of the Payment Request API.
|
||||||
|
pr.canMakePayment().then((result) => {
|
||||||
|
console.log("canMakePayment: ", result);
|
||||||
|
// setTest("canMakePayment: " + JSON.stringify(result));
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
setPaymentRequest(pr);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
pr.on("paymentmethod", async (e) => {
|
||||||
|
// const { error: backendError, clientSecret } = await fetch(
|
||||||
|
// "/create-payment-intent",
|
||||||
|
// {
|
||||||
|
// method: "POST",
|
||||||
|
// headers: {
|
||||||
|
// "Content-Type": "application/json",
|
||||||
|
// },
|
||||||
|
// body: JSON.stringify({
|
||||||
|
// paymentMethodType: "card",
|
||||||
|
// currency: "usd",
|
||||||
|
// }),
|
||||||
|
// }
|
||||||
|
// ).then((r) => r.json());
|
||||||
|
|
||||||
|
const { subscription_receipt } = await api.createSubscriptionReceipt({
|
||||||
|
token,
|
||||||
|
way: "stripe",
|
||||||
|
subscription_receipt: {
|
||||||
|
sub_plan_id: activeSubPlan?.id || "stripe.7",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const { client_secret } = subscription_receipt.data;
|
||||||
|
|
||||||
|
if (!subscription_receipt) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { error: stripeError } = await stripe.confirmCardPayment(
|
||||||
|
client_secret,
|
||||||
|
{
|
||||||
|
payment_method: e.paymentMethod.id,
|
||||||
|
},
|
||||||
|
{ handleActions: false }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (stripeError) {
|
||||||
|
// Show error to your customer (e.g., insufficient funds)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show a success message to your customer
|
||||||
|
// There's a risk of the customer closing the window before callback
|
||||||
|
// execution. Set up a webhook or plugin to listen for the
|
||||||
|
// payment_intent.succeeded event that handles any business critical
|
||||||
|
// post-payment actions.
|
||||||
|
});
|
||||||
|
}, [stripe, elements, activeSubPlan, api, token]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{/* {test} */}
|
||||||
|
{paymentRequest && (
|
||||||
|
<PaymentRequestButtonElement
|
||||||
|
className={styles["stripe-element"]}
|
||||||
|
options={{ paymentRequest }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ApplePayButton;
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
.stripe-element {
|
||||||
|
width: 100%;
|
||||||
|
max-width: 300px;
|
||||||
|
}
|
||||||
@ -11,6 +11,7 @@ import { selectors } from "@/store";
|
|||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import routes from "@/routes";
|
import routes from "@/routes";
|
||||||
import SubPlanInformation from "../SubPlanInformation";
|
import SubPlanInformation from "../SubPlanInformation";
|
||||||
|
import ApplePayButton from "./ApplePayButton";
|
||||||
|
|
||||||
export function StripePage(): JSX.Element {
|
export function StripePage(): JSX.Element {
|
||||||
const api = useApi();
|
const api = useApi();
|
||||||
@ -25,6 +26,16 @@ export function StripePage(): JSX.Element {
|
|||||||
navigate(routes.client.priceList());
|
navigate(routes.client.priceList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// const appleReceipt = async () => {
|
||||||
|
// const { subscription_receipt } = await api.createSubscriptionReceipt({
|
||||||
|
// token,
|
||||||
|
// receiptData: "",
|
||||||
|
// autorenewable: true,
|
||||||
|
// sandbox: true,
|
||||||
|
// });
|
||||||
|
// console.log(subscription_receipt);
|
||||||
|
// };
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
const siteConfig = await api.getAppConfig({ bundleId: "auraweb" });
|
const siteConfig = await api.getAppConfig({ bundleId: "auraweb" });
|
||||||
@ -63,7 +74,10 @@ export function StripePage(): JSX.Element {
|
|||||||
{stripePromise && clientSecret && (
|
{stripePromise && clientSecret && (
|
||||||
<Elements stripe={stripePromise} options={{ clientSecret }}>
|
<Elements stripe={stripePromise} options={{ clientSecret }}>
|
||||||
<CheckoutForm>
|
<CheckoutForm>
|
||||||
{activeSubPlan && <SubPlanInformation subPlan={activeSubPlan} />}
|
<>
|
||||||
|
<ApplePayButton activeSubPlan={activeSubPlan} />
|
||||||
|
{activeSubPlan && <SubPlanInformation subPlan={activeSubPlan} />}
|
||||||
|
</>
|
||||||
</CheckoutForm>
|
</CheckoutForm>
|
||||||
</Elements>
|
</Elements>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user