update-billing-card.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. module.exports = {
  2. friendlyName: 'Update billing card',
  3. description: 'Update the credit card for the logged-in user.',
  4. inputs: {
  5. stripeToken: {
  6. type: 'string',
  7. example: 'tok_199k3qEXw14QdSnRwmsK99MH',
  8. description: 'The single-use Stripe Checkout token identifier representing the user\'s payment source (i.e. credit card.)',
  9. extendedDescription: 'Omit this (or use "") to remove this user\'s payment source.',
  10. whereToGet: {
  11. description: 'This Stripe.js token is provided to the front-end (client-side) code after completing a Stripe Checkout or Stripe Elements flow.'
  12. }
  13. },
  14. billingCardLast4: {
  15. type: 'string',
  16. example: '4242',
  17. description: 'Omit if removing card info.',
  18. whereToGet: { description: 'Credit card info is provided by Stripe after completing the checkout flow.' }
  19. },
  20. billingCardBrand: {
  21. type: 'string',
  22. example: 'visa',
  23. description: 'Omit if removing card info.',
  24. whereToGet: { description: 'Credit card info is provided by Stripe after completing the checkout flow.' }
  25. },
  26. billingCardExpMonth: {
  27. type: 'string',
  28. example: '08',
  29. description: 'Omit if removing card info.',
  30. whereToGet: { description: 'Credit card info is provided by Stripe after completing the checkout flow.' }
  31. },
  32. billingCardExpYear: {
  33. type: 'string',
  34. example: '2023',
  35. description: 'Omit if removing card info.',
  36. whereToGet: { description: 'Credit card info is provided by Stripe after completing the checkout flow.' }
  37. },
  38. },
  39. fn: async function (inputs, exits) {
  40. // Add, update, or remove the default payment source for the logged-in user's
  41. // customer entry in Stripe.
  42. var stripeCustomerId = await sails.helpers.stripe.saveBillingInfo.with({
  43. stripeCustomerId: this.req.me.stripeCustomerId,
  44. token: inputs.stripeToken || '',
  45. });
  46. // Update (or clear) the card info we have stored for this user in our database.
  47. // > Remember, never store complete card numbers-- only the last 4 digits + expiration!
  48. // > Storing (or even receiving) complete, unencrypted card numbers would require PCI
  49. // > compliance in the U.S.
  50. await User.update({ id: this.req.me.id })
  51. .set({
  52. stripeCustomerId,
  53. hasBillingCard: inputs.stripeToken ? true : false,
  54. billingCardBrand: inputs.stripeToken ? inputs.billingCardBrand : '',
  55. billingCardLast4: inputs.stripeToken ? inputs.billingCardLast4 : '',
  56. billingCardExpMonth: inputs.stripeToken ? inputs.billingCardExpMonth : '',
  57. billingCardExpYear: inputs.stripeToken ? inputs.billingCardExpYear : ''
  58. });
  59. return exits.success();
  60. }
  61. };