#Suzz

1 messages · Page 1 of 1 (latest)

iron ridgeBOT
strong granite
#

👋 happy to help

open robin
#

thankyou, please help me to setup autopayment with dotnet and js fe

#

asp.netcore

strong granite
open robin
#

would like to setup autopayment which follows this.

woven holly
#

Do you mean a recurring payment?

iron ridgeBOT
open robin
#

yes

woven holly
#

Just create a price on your product with as a recurring price

open robin
#

both recurring payment and the subscription are different?

open robin
timid summit
#

you need your customer payment method before charge them automatically.
second recuring payment on which you can create subscription. the user will be charged agaisnt that subscription which is automatically charged.

open robin
#

i've created a product with two prices
like this

timid summit
#

excellent now create subscription agaianst these prices. in checkout session

open robin
#

both the prices are recurring and bill will generate monthly

#

in backend i've done like this
[HttpPost("create-checkout")] [Consumes("application/json")] public async Task<IActionResult> CreateCheckoutSession(PaymentRequest paymentRequest) { var _baseUrl = $"{Request.Scheme}://{Request.Host}"; SessionCreateOptions options = new SessionCreateOptions { SuccessUrl = $"{_baseUrl}/success.html?session_id={{CHECKOUT_SESSION_ID}}", CancelUrl = $"{_baseUrl}/canceled.html", Mode = "subscription", LineItems = new List<SessionLineItemOptions> { new() { Price = paymentRequest.PriceId, Quantity = 1, }, }, }; }; var service = new SessionService(_client); try { var session = await service.CreateAsync(options); Response.Headers.Add("Location", session.Url); return new StatusCodeResult(303); } catch (StripeException e) { Console.WriteLine(e.StripeError.Message); return BadRequest(new ErrorResponse { ErrorMessage = new ErrorMessage { Message = e.StripeError.Message, } }); } }

woven holly
#

Please add those in ` x3

open robin
#

this is the subscription you meant right @timid summit

open robin
woven holly
#

That looks correct Suzz

#
code
#

instead of code

open robin
#

sorry, i didnt get that

woven holly
#

You can do
``

``

open robin
#

ok gotta

woven holly
#

Next is just to let the customer go through the checkout, and then you'll have a subscription connected to that price and customer

open robin
#

all these are enough, will the stripe debit the amount on the next month once i successfully finish the checkout?

woven holly
#

Go through you Stripe settings, but yes, I belive the default is automatically charge each month

open robin
#

ok

woven holly
#

and then you can define when you want to send out notifications etc

timid summit
#

lemme share code example with you. that might helo you

open robin
#

ok, thankyou @timid summit

woven holly
#

in Stripe dashboard

open robin
#

ok

#

ok, i will try

timid summit
#

try this

#

to create subscription on checkout

#

and sedn thsi checkout link to user . subscription will be start and user will be chagres automaticlalu

silent fern
#

Hey, taking over here. Let me know if there's any follow-up Qs I can answer!

open robin
woven holly
#

Not sure if you're able to send out notifications in test mode

open robin
#

ok

open robin
woven holly
#

That's for adding metadata. You can get the sub metadata when you retrieve a subscription

open robin
#

i should do that on the checkout api right? retrieve the subscription details and pass to the checkoutsession

open robin
#

@woven holly @timid summit

woven holly
#

I recommend using a webhook for listening to subscriptions being created/updated

#
if (event.type === 'customer.subscription.created') {
    const subscription = event.data.object as Stripe.Subscription;
    const productId = subscription.items.data[0].plan.product;

    const product = await stripe.products.retrieve(productId as string);
    await enableToken(subscription.metadata.userId, product.name as SubscriptionPlan);
  }

for example like this

open robin
#

yes i've setup the webhook, will update like this

timid summit
open robin
#

but if i update the webhook, how will i get the details in the checkout api @woven holly ?

open robin
timid summit
#

webhook is the next step. first you need to chagre rightly your customer

open robin
# timid summit

@timid summit the code you shared have the subscription data. how will i get it to pass to the checkout api

woven holly
#

Not sure what you mean, but the flow looks like this
Checkout with data > sub is created > listen to event > get subscription > do something

timid summit
#

you dont need to get the subscription data. you only need to pass the priceId( recurring price) and set the mode = subscription while createing checkout .
and metadata i just a dictionary which is just any thing youwant to stroe in the object

timid summit
# timid summit

in this attachment you just need to add priceID which is recurring price basically and create checkout just

#

you need to add "invoice.paid" event in the weebhook to lsitent

open robin
#

this is my webhook

open robin
#

SessionCreateOptions options = new SessionCreateOptions { SuccessUrl = $"{_baseUrl}/success.html?session_id={{CHECKOUT_SESSION_ID}}", CancelUrl = $"{_baseUrl}/canceled.html", Mode = "subscription", LineItems = new List<SessionLineItemOptions> { new() { Price = paymentRequest.PriceId, Quantity = 1, }, }, }; };

open robin
timid summit
#

we listen to webhook to make changes in our system after succesfull payment.
to acuatally make all changes in our system after succesful payment we listen to webhook.

#

this is all ok. now what you need next br0?

open robin
#

what kind of changes can you please expalin a bit?

timid summit
#

sure

#

for example offering a course A. you got the link of my course A. but you dont have any access to the course meterail it just a view for you.
now after you purchased my course and pay through stripe a webhook will triger and i will listen the webhook accoringly and check if the payment status is succeeded then i will add that user data in my database and in my system upon which he will be able to view the course and unlock all the meterial.

open robin
#

ok, got it

timid summit
#

assume there is a flag (isPurchased) for that particular course . if user dont pay for it so will not able t access the course. after payment and i will listen the event in webhook and then set the (isPurchased) flag to true for the user. now on front end on the base of flag value i will show the content of locked course to my user

#

now clear brother?

open robin
#

yes, clear

#

thankyou verymuch

timid summit
#

no issue

#

if you need to setup stripe in your system you can contact me

open robin
#

sure i will