#TuanPham-metadata

1 messages ยท Page 1 of 1 (latest)

haughty pulsar
#

hi there! how are you trying to update your charge object? can you share the code snippet?

robust ice
#

sure

#

const subscription = await this.stripe.subscriptions.create(
{
customer: findCustomer.stripe_customer_id,
items: [
{
price: service.stripe_price_id,
},
],
expand: ['latest_invoice.payment_intent'],
default_payment_method: stripePmId,
metadata: {
serviceId,
fromDetailService,
fromUserId: userId,
},
},
{
stripeAccount: stripeAccountId,
},
);
console.log('created subscription', JSON.stringify(subscription));

const thisSubChargeId = subscription.latest_invoice['charge'];
console.log('start to update charge of this subscription');
await this.stripe.charges.update(thisSubChargeId, {
  metadata: {
    serviceId,
    fromDetailService,
    fromUserId: userId,
    stripeAccountId,
  },
});
#

Is there any way to update my charge object metadata right after the subscription is created and before the hook service can catch ?

#

๐Ÿ˜…

haughty pulsar
#

unfortunately, no. Can you elaborate more on why you want to update the metadata on the Charge object? If you want to know that the subscription is paid, you can always use the invoice.paid webhook instead which contains the subscription id

robust ice
#

hmm, I just need to do that because I create subscription with a direct charge to stripe connect account

#

so I need the stripe account id to be able to retrieve something to put the data into db

haughty pulsar
#

what are you retrieving?

robust ice
#

I want to retrieve the subscription data

#

but I still need the stripe Account id to be able to get it

#

else It will give me the error "There is no subscription with this id sub_xxx "

#

I know it is because I use the direct charge but I did not find anything to help with this

haughty pulsar
#

i'm a bit confused here, since you're listening to the connected account webhooks, couldn't you wait for the customer.subscription.created webhook to be sent to your webhook endpoint also?

robust ice
#

hmm it seems to be a good choice to me

#

but I also create one time payment

#

so I tried to append this two payments by using the event charge succeeded

#

Am i supposed to store the stripe account id along with the subscription id into the db ?

haughty pulsar
#

i'd probably be able to provide better advice if you described your use case in detail

robust ice
#

yeah sure

#

In my case, I have to create a service for one-time payment and subscription payment
So I find the hook event type charge.succeeded , I use it fine for the one-time payment but in subscription payment, I can not find any way to retrieve the subscription data . I still got the subscription id in the charge object I catch, but I can not get the metadata in this subscription ( this includes the stripeAccountId ) . Thus, I tried to update the charge object right after I created a subscription to be able to get the metadata from the charge object which I will catch via charge.succeeded event

#

Is this clear enough ? Please tell me if something makes you confused

haughty pulsar
#

how is the customer paying for the one-time payment? Is it together with the subscription payment or a separate payment?

robust ice
#

it will be a separate payment. I create a paymentIntent and a payment method also. Then, I use the function confirmPayment from stripe-react-native

haughty pulsar
#

iirc, you should be able to add a one-time Price to a subscription as well. Unless you're intentionally doing it separately?

robust ice
#

It will be different. The stripe price will be created in my API create sub service

haughty pulsar
#

which payment is done first? the subscription or the one-time payment?

robust ice
#

umm. I think we got something wrong here

#

In my case, I already separate one-time payment and the subscription into 2 different API

#

the one-time payment I use with confirmPayment function of stripe-react-native in app

#

and the subscription I use via my API , and use stripe instance in the back-end

#

So, in my case, the one-time payment still works fine and the subscription gets some struggles

#

to be more detailed, it is I can not retrieve it because missing the stripe account id which I pass into the subscription metadata

haughty pulsar
#

ah, then like what i said before, listen for the customer.subscription.created webhook event

robust ice
#

so you mean that I should not append my two types of payments into 1 event : charge.succeeded , right?

#

and I'm a little confused . the customer.subscription.created also handles when user renew the subscription right ?

haughty pulsar
#

no

robust ice
#

wow . it might be cool for my solution

#

when user renews the subscription , I can handle it via this event customer.subscription.updated , is it correct ?

haughty pulsar
#

sorry, should have written in full. When the user renews the subscription, what information does your application need first?

robust ice
#

i will need the serviceId and userId

#

to reupdate the data in my db

haughty pulsar
#

we're talking about renewal though

#

not creation

#

why would the serviceId and userId change?

robust ice
#

I'm sorry . I just check and find out that my renewal code does not run

#

so, the renewal stills keep the subscription metadata right?

haughty pulsar
#

customer.subscription.updated would continue to have the subscription metadata

#

in essence, customer.subscription.* will have the subscription metadata

robust ice
#

cool, mate

#

I think this is the solution I need

#

Just separate my two payments and use your recommended event type

haughty pulsar
robust ice
#

I will take a look at it

#

Thank you for spending your time for helping me

#

๐Ÿฅฐ

robust ice
#

are you still here , alex ?

#

๐Ÿ˜…

haughty pulsar
#

yep

robust ice
#

I have just tried the above solution

#

and I can not catch the event customer.subscription.created

#

I still see the subscription is created in both dashboard and my app

#

but in my BE, I can not catch this event @@

haughty pulsar
robust ice
#

sure

#

acct_1KrCTV2ELzgUanaa

#

I just created a subscription with account male2

haughty pulsar
#

what's the subscription id?

robust ice
#

sub_1LFW5u2ELzgUanaaTg1BMtMO

haughty pulsar
#

so the webhook event is being sent to your platform webhook endpoint, and your server responded with 201

robust ice
#

it seems like that

haughty pulsar
#

what did you mean when you can not catch the event customer.subscription.created?

robust ice
#

I have a switch case for this event type

#

case 'customer.subscription.created':
// this event is for subscription payment only
console.log('catch create subscription');
// console.log('created subscription :', JSON.stringify(event['data']));
this.handleBuyService(
event['data']['object'],
event['data']['object']['metadata'],
true,
);
break;

#

and I can not get the log catch create subscription

haughty pulsar
#

then you need to add additional logging prior to this to see where the problem is

robust ice
#

like when I catch charge.succeeded, I still get the log and start doing my function

#

oh

#

my logger debug is empty too

#

it did not log anything

haughty pulsar
#

since your application is responding with 201, it's definitely receiving the event, the question now is how is your server handling this event such that it's not entering that switch case

robust ice
#

is this related to the config stripe ?

haughty pulsar
#

what config?

robust ice
#

I tried to pass the stripe account when define the stripe instance but the result is still the same

#

this.stripeConfig =
this.configService.get<Configuration['stripe']>('stripe');
this.stripe = new Stripe(this.stripeConfig.stripeSecretKey, {
apiVersion: '2020-08-27',
});