Created
June 19, 2020 14:21
-
-
Save SimonHoiberg/e0a9982af9a525f99d624f9e4c9bf42f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const handler = async ( | |
event: APIGatewayProxyEvent, | |
context: any, | |
callback: (err: Error | null, data: any) => void, | |
) => { | |
if (!event.body) { | |
callback(Error('Invalid body')); | |
return; | |
} | |
const body = JSON.parse(event.body) as IBody; | |
const { paymentMethodID, customerID } = body; | |
if (!paymentMethodID || !customerID) { | |
callback(null, { | |
statusCode: 400, | |
body: 'Invalid payment method or customer ID', | |
}); | |
return; | |
} | |
try { | |
// Attach the new payment method | |
await stripe.paymentMethods.attach(paymentMethodID, { | |
customer: customerID, | |
}); | |
// Set the new payment method as default | |
await stripe.customers.update(customerID, { | |
invoice_settings: { | |
default_payment_method: paymentMethodID, | |
}, | |
}); | |
callback(null, { | |
statusCode: 200, | |
}); | |
} catch (error) { | |
callback(error); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment