StripeSubscriptionButton
This commit is contained in:
@@ -1,57 +1,85 @@
|
|||||||
import React from 'react';
|
import React from "react";
|
||||||
import { loadStripe } from '@stripe/stripe-js';
|
import { loadStripe } from "@stripe/stripe-js";
|
||||||
import { Elements, useStripe, useElements, CardElement } from '@stripe/react-stripe-js';
|
import {
|
||||||
import {MyProductData, StripeSubscriptionCreate} from '../../services/services';
|
Elements,
|
||||||
import {useQuery} from "@tanstack/react-query";
|
useStripe,
|
||||||
|
useElements,
|
||||||
|
CardElement,
|
||||||
|
} from "@stripe/react-stripe-js";
|
||||||
|
import {
|
||||||
|
MyProductData,
|
||||||
|
StripeSubscriptionCreate,
|
||||||
|
} from "../../services/services";
|
||||||
|
import { useQuery } from "@tanstack/react-query";
|
||||||
import queryKeys from "../../services/queryKeys";
|
import queryKeys from "../../services/queryKeys";
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from "react-router-dom";
|
||||||
//const stripePromise = loadStripe('your_stripe_publishable_key');
|
//const stripePromise = loadStripe('your_stripe_publishable_key');
|
||||||
const stripePromise = loadStripe('pk_test_51RqL5WLjZLojw6IZmEpwFidNZSl9lLlVUHNvuFZNEz1eTR9XXepnyyVhfvXe9cp4eMnqkDPpoe9wxLLRSV0dxRee00UfhayUOT');
|
const stripePromise = loadStripe(
|
||||||
|
"pk_test_51RqL5WLjZLojw6IZmEpwFidNZSl9lLlVUHNvuFZNEz1eTR9XXepnyyVhfvXe9cp4eMnqkDPpoe9wxLLRSV0dxRee00UfhayUOT",
|
||||||
|
);
|
||||||
|
|
||||||
const CheckoutForm = ({ priceId, customerId ,option_name }) => {
|
const CheckoutForm = ({
|
||||||
const stripe = useStripe();
|
priceId,
|
||||||
const elements = useElements();
|
customerId,
|
||||||
const navigate = useNavigate();
|
option_name,
|
||||||
const handleSubmit = async (event) => {
|
selected_display_name,
|
||||||
event.preventDefault();
|
}) => {
|
||||||
|
const stripe = useStripe();
|
||||||
|
const elements = useElements();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const handleSubmit = async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
if (!stripe || !elements) {
|
if (!stripe || !elements) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let reqData = {
|
|
||||||
priceId : priceId,
|
|
||||||
customerId: customerId,
|
|
||||||
option_name: option_name,
|
|
||||||
token: localStorage.getItem('token'), // USER TOKEN
|
|
||||||
uid: localStorage.getItem('uid') // USER UID
|
|
||||||
}
|
|
||||||
|
|
||||||
StripeSubscriptionCreate(reqData).then( (res)=>{
|
|
||||||
console.log(res);
|
|
||||||
console.log(res.data.stripe_session);
|
|
||||||
//navigate(res.data.stripe_session)
|
|
||||||
window.location.replace(res.data.stripe_session);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
let reqData = {
|
||||||
|
priceId: priceId,
|
||||||
|
customerId: customerId,
|
||||||
|
option_name: option_name,
|
||||||
|
token: localStorage.getItem("token"), // USER TOKEN
|
||||||
|
uid: localStorage.getItem("uid"), // USER UID
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
StripeSubscriptionCreate(reqData).then((res) => {
|
||||||
<form onSubmit={handleSubmit}>
|
console.log(res);
|
||||||
{/*<CardElement />*/}
|
console.log(res.data.stripe_session);
|
||||||
<button type="submit" disabled={!stripe} className="btn btn-primary text-uppercase">
|
//navigate(res.data.stripe_session)
|
||||||
Start Subscription
|
window.location.replace(res.data.stripe_session);
|
||||||
</button>
|
});
|
||||||
</form>
|
};
|
||||||
);
|
|
||||||
|
return (
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
{/*<CardElement />*/}
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={!stripe}
|
||||||
|
className="btn btn-primary text-uppercase"
|
||||||
|
>
|
||||||
|
Start {selected_display_name} Subscription
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const StripeSubscriptionButton = ({ priceId, customerId ,option_name}) => {
|
const StripeSubscriptionButton = ({
|
||||||
return (
|
priceId,
|
||||||
<Elements stripe={stripePromise}>
|
customerId,
|
||||||
<CheckoutForm priceId={priceId} customerId={customerId} option_name={option_name} />
|
option_name,
|
||||||
</Elements>
|
selected_display_name,
|
||||||
);
|
}) => {
|
||||||
|
return (
|
||||||
|
<Elements stripe={stripePromise}>
|
||||||
|
<CheckoutForm
|
||||||
|
priceId={priceId}
|
||||||
|
customerId={customerId}
|
||||||
|
option_name={option_name}
|
||||||
|
selected_display_name={selected_display_name}
|
||||||
|
/>
|
||||||
|
</Elements>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default StripeSubscriptionButton;
|
export default StripeSubscriptionButton;
|
||||||
@@ -8,9 +8,10 @@ import StripeSubscriptionButton from "./StripeSubscriptionButton";
|
|||||||
import SubscribeInfo from "./SubscribeInfo";
|
import SubscribeInfo from "./SubscribeInfo";
|
||||||
|
|
||||||
export default function Subscribe() {
|
export default function Subscribe() {
|
||||||
const {state: {selectedSubscription, customerId}} = useLocation()
|
const {state: {selectedSubscription, customerId, currentSubscription}} = useLocation()
|
||||||
|
|
||||||
console.log('selectedSubscription', selectedSubscription)
|
console.log('selectedSubscription', selectedSubscription)
|
||||||
|
console.log('currentSubscription', currentSubscription)
|
||||||
console.log('selectedSubscription.option_name', selectedSubscription.option_name)
|
console.log('selectedSubscription.option_name', selectedSubscription.option_name)
|
||||||
console.log('customerId', customerId)
|
console.log('customerId', customerId)
|
||||||
|
|
||||||
@@ -25,8 +26,8 @@ export default function Subscribe() {
|
|||||||
<div className="card card-statistics text-center py-3">
|
<div className="card card-statistics text-center py-3">
|
||||||
<div className="card-body pricing-content">
|
<div className="card-body pricing-content">
|
||||||
<div className="pricing-content-card">
|
<div className="pricing-content-card">
|
||||||
<h5>Current Subscription(s)</h5>
|
<h3>Your Current Subscription</h3>
|
||||||
{/*<h2 className="text-primary pt-3">{currentSubscription?.display_name}</h2>*/}
|
<h2 className="text-primary pt-3">{currentSubscription?.display_name}</h2>
|
||||||
{/*<SubcribePaymentOptions activePaymentType={activePaymentType}*/}
|
{/*<SubcribePaymentOptions activePaymentType={activePaymentType}*/}
|
||||||
{/* setActivePaymentType={setActivePaymentType}/>*/}
|
{/* setActivePaymentType={setActivePaymentType}/>*/}
|
||||||
{/*{activePaymentType == 'new' ?*/}
|
{/*{activePaymentType == 'new' ?*/}
|
||||||
@@ -38,7 +39,9 @@ export default function Subscribe() {
|
|||||||
<SubscribeInfo />
|
<SubscribeInfo />
|
||||||
<StripeSubscriptionButton priceId={selectedSubscription.stripe_price_id}
|
<StripeSubscriptionButton priceId={selectedSubscription.stripe_price_id}
|
||||||
customerId={customerId}
|
customerId={customerId}
|
||||||
option_name={selectedSubscription.option_name}/>
|
option_name={selectedSubscription.option_name}
|
||||||
|
selected_display_name={selectedSubscription.display_name}
|
||||||
|
/>
|
||||||
</>
|
</>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -91,7 +91,8 @@ export default function Subscription() {
|
|||||||
navigate(siteLinks.subscribe, {
|
navigate(siteLinks.subscribe, {
|
||||||
state: {
|
state: {
|
||||||
selectedSubscription: value,
|
selectedSubscription: value,
|
||||||
customerId: stripe_customer_id
|
customerId: stripe_customer_id,
|
||||||
|
currentSubscription: currentSubscription
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user