/api/user/subscriptions/set-payment-method-detached (PATCH)
await global.api.user.subscriptions.SetPaymentMethodDetached.patch(req) Located in Stripe Subscriptions module API
Returns object
Exceptions
These exceptions are thrown (NodeJS) or returned as JSON (HTTP) if you provide incorrect data or do not meet the requirements:
Exception | Circumstances |
---|---|
invalid querystring paymentmethodid | |
invalid-paymentmethodid | missing querystring paymentmethodid |
invalid-account | ineligible accessing account |
invalid-paymentmethod | invalid querystring payment method is default |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const subscriptions = require('../../../../../index.js')
const stripeCache = require('../../../../stripe-cache.js')
module.exports = {
patch: async (req) => {
if (!req.query || !req.query.paymentmethodid) {
throw new Error('invalid-paymentmethodid')
}
const paymentMethod = await global.api.user.subscriptions.PaymentMethod.get(req)
if (!paymentMethod) {
throw new Error('invalid-paymentmethodid')
}
if (!paymentMethod.customer) {
throw new Error('invalid-paymentmethod')
}
req.query.customerid = paymentMethod.customer
const customer = await global.api.user.subscriptions.Customer.get(req)
if (customer.invoice_settings.default_payment_method === req.query.paymentmethodid) {
throw new Error('invalid-paymentmethod')
}
const paymentMethodNow = await stripeCache.execute('paymentMethods', 'detach', req.query.paymentmethodid, req.stripeKey)
await stripeCache.delete(req.query.paymentmethodid, req.stripeKey)
try {
await subscriptions.StorageList.remove(`${req.appid}/paymentMethods`, req.query.paymentmethodid)
} catch (error) {
}
try {
await subscriptions.StorageList.remove(`${req.appid}/account/paymentMethods/${req.account.accountid}`, req.query.paymentmethodid)
} catch (error) {
}
try {
await subscriptions.Storage.delete(`${req.appid}/map/accountid/paymentmethodid/${req.query.paymentmethodid}`)
} catch (error) {
}
return paymentMethodNow
}
}
Test source (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
const TestStripeAccounts = require('../../../../../test-stripe-accounts.js')
describe('/api/user/subscriptions/set-payment-method-detached', () => {
describe('exceptions', () => {
describe('invalid-paymentmethodid', () => {
it('missing querystring paymentmethodid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/set-payment-method-detached')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
})
it('invalid querystring paymentmethodid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/subscriptions/set-payment-method-detached?paymentmethodid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethodid')
})
})
describe('invalid-account', () => {
it('ineligible accessing account', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/subscriptions/set-payment-method-detached?paymentmethodid=${user.paymentMethod.id}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
describe('invalid-paymentmethod', () => {
it('invalid querystring payment method is default', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const req = TestHelper.createRequest(`/api/user/subscriptions/set-payment-method-detached?paymentmethodid=${user.paymentMethod.id}`)
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.patch()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-paymentmethod')
})
})
})
describe('returns', () => {
it('object', async () => {
const user = await TestStripeAccounts.createUserWithPaymentMethod()
const paymentMethod1 = user.paymentMethod
await TestHelper.createPaymentMethod(user, {
cvc: '111',
number: '4111111111111111',
exp_month: '1',
exp_year: (new Date().getFullYear() + 1).toString().substring(2),
name: user.profile.firstName + ' ' + user.profile.lastName,
address_line1: '285 Fulton St',
address_line2: 'Apt 893',
address_city: 'New York',
address_state: 'NY',
address_zip: '10007',
address_country: 'US',
default: 'true'
})
await TestHelper.waitForWebhook('customer.updated', (stripeEvent) => {
return stripeEvent.data.object.invoice_settings.default_payment_method === user.paymentMethod.id
})
await TestHelper.waitForWebhook('payment_method.attached', (stripeEvent) => {
return stripeEvent.data.object.id === user.paymentMethod.id
})
const req = TestHelper.createRequest(`/api/user/subscriptions/set-payment-method-detached?paymentmethodid=${paymentMethod1.id}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const paymentMethodNow = await req.patch()
assert.strictEqual(paymentMethodNow.customer, null)
})
})
})