'Update Metadata Stripe
I'm doing a stripe implementation, and I'm using the metadata.
Everything is fine until I need to use a value in the metadata that I need to update in order to know, for example, if the uuid of that metadata has already been used.
payment_intent_data: {
metadata: {
uuid: usedUuid,
isUsed: "false",
},
This is the example of the function that I am using to know if the uuid is valid and I have doubts about how to update the value of the metadata for the invalidate function.
async function isValidUuid(uuid: string): Promise<boolean> {
// TODO: check if uuid has been used before and its valid
var isValid = uuid4.valid(uuid);
const charges = await stripe.charges.search({
query: `metadata[\'uuid\']:${uuid} AND metadata[\'isUsed\']:\'false\'`,
});
return isValid && charges.data.length > 0;
}
Could someone tell me how I could update the isUsed field?
Thanks in advice
Solution 1:[1]
You should be able to update this metadata with a call to the update charge function[1]. You only need to specify the metadata keys that you are changing, so if you only update isUsed
your uuid
value will not be affected.
const charge = await stripe.charges.update(
'ch_123',
{metadata: {isUsed: 'true'}}
);
One thing to possibly keep in mind is that the PaymentIntent and Charge have seperate copies of their metadata. The PaymentIntent metadata is copied over when the Charge is first created, but there is no automatic copying or syncing after that. If you want to update the metadata on both objects, you will need to update the metadata on both with two separate API calls[1][2]. If you only need this data to be updated on the charge, then you do not need to do this.
const charge = await stripe.charges.update(
'ch_123',
{metadata: {isUsed: 'true'}}
);
const paymentIntent = await stripe.paymentIntents.update(
'pi_456',
{metadata: {isUsed: 'true'}}
);
[1] https://stripe.com/docs/api/charges/update#update_charge-metadata [2] https://stripe.com/docs/api/payment_intents/update?lang=node#update_payment_intent-metadata
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pompey |