#inderjeet
1 messages · Page 1 of 1 (latest)
Hi
Hi! Let me help you with this.
Thanks for your support
I would like to inform you that
I want to subscription process using react in frontend and php in backend
You should create a Subscription and not a Setup Intent: https://stripe.com/docs/billing/quickstart?lang=php&client=react
I also share the code
FIrst step - return response()->json([
'client_secret' => $setupIntent->client_secret,
]);
Pass the client secret from backedn to frontend
import { useState } from 'react';
import { CardElement, useStripe, useElements } from '@stripe/react-stripe-js';
const PaymentForm = () => {
const [error, setError] = useState(null);
const [processing, setProcessing] = useState(false);
const [succeeded, setSucceeded] = useState(false);
const [clientSecret, setClientSecret] = useState('');
const stripe = useStripe();
const elements = useElements();
useEffect(() => {
const fetchClientSecret = async () => {
const response = await fetch('/create-setup-intent');
const data = await response.json();
setClientSecret(data.client_secret);
};
fetchClientSecret();
}, []);
const handleSubmit = async (event) => {
event.preventDefault();
setProcessing(true);
const { error, setupIntent } = await stripe.confirmCardSetup(clientSecret, {
payment_method: {
card: elements.getElement(CardElement),
},
});
if (error) {
setError(error.message);
setProcessing(false);
} else {
setSucceeded(true);
setProcessing(false);
console.log(setupIntent);
}
};
return (
<form onSubmit={handleSubmit}>
<CardElement />
{error && <div>{error}</div>}
<button type="submit" disabled={processing || succeeded}>Save Card</button>
</form>
);
};
Above code is frontend
is it right way or not
?
It depends on what you do on the backend. Why are you using Setup Intent?
Actually, I manage payment subscriptions from the backend . I need a payment method id from the frontend user.
So, without client secret frontend user not able to pass me payment method id
I hope you got my concern
I understand. Could you please share your backend code?
Sure
$stripeCustomerId = $user->createAsStripeCustomer()->id;
$paymentMethod = $request->payment_method;
$paymentMethod = $user->addPaymentMethod($paymentMethod)->id;
// }
$plan = (!empty($request->plan_id )) ? $request->plan_id : '';
** $subscription = $user->newSubscription('default', $plan)->create($paymentMethod != null ? $paymentMethod: '');**
=================
when frontend user give me payment method id then i just need to pass it in newSubcription method then this method confirm then charge and do the subscription but frontend user not able to give me payment method id
Concern is that how using <paymentElement /> how frontend user give me the payment method id
Are you collecting the PM first and then creating the Subscription?
Yah you are right
That's not the recommended way to do this. Please follow the guide I shared earlier: https://stripe.com/docs/billing/quickstart?lang=php&client=react
In my api I collecting the payment method from frontend user.
There is no any another way to do this without using above way.
Because I try this another way also like
1st I create a api using payment intent and their response i got payment client secret which i give to the frontend developer and he confirms the payment.
Then the balance deducated from my account. but this is doing one time deduction not subscription
I am not sure you're following the guide I shared.
Here's another one maybe it's more clear. In this step you are creating a Subscription: https://stripe.com/docs/billing/subscriptions/build-subscriptions?ui=elements#create-subscription
Thanks for your time
Happy to help. Please, let me know if you have any other questions.
Can you tell me one more thing
Sure
<CardElement /> display the card in single input like card number,cvc etc.
if i want to use it into multiple input then how i can do this according to ui point of view
👋 taking over for my colleague. Let me catch up.
you can look into this tutorial video https://www.youtube.com/watch?v=gW8b0Y6Ae8o
In this episode, you'll learn from Matt about how use separate card input fields for number, expiry and cvc with Stripe.js and Elements.
Presenter
Matthew Ling - Developer Advocate at Stripe - https://twitter.com/mattling_dev
Resources
Official client library: https://github.com/stripe/stripe-ruby
Official documentation: https://stri...