From 8fdd3ff9406840bf78b9b05cc2d71f785a71f7a1 Mon Sep 17 00:00:00 2001 From: Ethan Setnik Date: Tue, 19 Nov 2019 17:13:24 -0500 Subject: [PATCH] [@types/stripe] - add Stripe.oauth resources (#40443) * add oauth token endpoint * fix version error * fix linter errors * add HeaderOptions as per https://github.com/stripe/stripe-node/blob/master/lib/StripeMethod.js * add deauthorize and full jsdoc comments * fix redundant * fix package.json * add test cases, add variant without HeaderOptions * fix test linter errors --- types/stripe/index.d.ts | 476 +++++++++++++++++++++++------------ types/stripe/stripe-tests.ts | 54 +++- 2 files changed, 365 insertions(+), 165 deletions(-) diff --git a/types/stripe/index.d.ts b/types/stripe/index.d.ts index 1cba40be43..a4719e3472 100644 --- a/types/stripe/index.d.ts +++ b/types/stripe/index.d.ts @@ -100,6 +100,8 @@ declare class Stripe { paymentMethods: Stripe.resources.PaymentMethods; payouts: Stripe.resources.Payouts; plans: Stripe.resources.Plans; + oauth: Stripe.resources.OAuth; + /** * @deprecated */ @@ -3679,155 +3681,155 @@ declare namespace Stripe { namespace events { type EventType = - | "*" - | "account.updated" - | "account.application.authorized" - | "account.application.deauthorized" - | "account.external_account.created" - | "account.external_account.deleted" - | "account.external_account.updated" - | "application_fee.created" - | "application_fee.refunded" - | "application_fee.refund.updated" - | "balance.available" - | "capability.updated" - | "charge.captured" - | "charge.expired" - | "charge.failed" - | "charge.pending" - | "charge.refunded" - | "charge.succeeded" - | "charge.updated" - | "charge.dispute.closed" - | "charge.dispute.created" - | "charge.dispute.funds_reinstated" - | "charge.dispute.funds_withdrawn" - | "charge.dispute.updated" - | "charge.refund.updated" - | "checkout.session.completed" - | "coupon.created" - | "coupon.deleted" - | "coupon.updated" - | "credit_note.created" - | "credit_note.updated" - | "credit_note.voided" - | "customer.created" - | "customer.deleted" - | "customer.updated" - | "customer.discount.created" - | "customer.discount.deleted" - | "customer.discount.updated" - | "customer.source.created" - | "customer.source.deleted" - | "customer.source.expiring" - | "customer.source.updated" - | "customer.subscription.created" - | "customer.subscription.deleted" - | "customer.subscription.trial_will_end" - | "customer.subscription.updated" - | "customer.tax_id.created" - | "customer.tax_id.deleted" - | "customer.tax_id.updated" - | "file.created" - | "invoice.created" - | "invoice.deleted" - | "invoice.finalized" - | "invoice.marked_uncollectible" - | "invoice.payment_action_required" - | "invoice.payment_failed" - | "invoice.payment_succeeded" - | "invoice.sent" - | "invoice.upcoming" - | "invoice.updated" - | "invoice.voided" - | "invoiceitem.created" - | "invoiceitem.deleted" - | "invoiceitem.updated" - | "issuing_authorization.created" - | "issuing_authorization.request" - | "issuing_authorization.updated" - | "issuing_card.created" - | "issuing_card.updated" - | "issuing_cardholder.created" - | "issuing_cardholder.updated" - | "issuing_dispute.created" - | "issuing_dispute.updated" - | "issuing_settlement.created" - | "issuing_settlement.updated" - | "issuing_transaction.created" - | "issuing_transaction.updated" - | "mandate.updated" - | "order.created" - | "order.payment_failed" - | "order.payment_succeeded" - | "order.updated" - | "order_return.created" - | "payment_intent.amount_capturable_updated" - | "payment_intent.canceled" - | "payment_intent.created" - | "payment_intent.payment_failed" - | "payment_intent.succeeded" - | "payment_method.attached" - | "payment_method.card_automatically_updated" - | "payment_method.detached" - | "payment_method.updated" - | "payout.canceled" - | "payout.created" - | "payout.failed" - | "payout.paid" - | "payout.updated" - | "person.created" - | "person.deleted" - | "person.updated" - | "plan.created" - | "plan.deleted" - | "plan.updated" - | "product.created" - | "product.deleted" - | "product.updated" - | "radar.early_fraud_warning.created" - | "radar.early_fraud_warning.updated" - | "recipient.created" - | "recipient.deleted" - | "recipient.updated" - | "reporting.report_run.failed" - | "reporting.report_run.succeeded" - | "reporting.report_type.updated" - | "review.closed" - | "review.opened" - | "setup_intent.canceled" - | "setup_intent.created" - | "setup_intent.setup_failed" - | "setup_intent.succeeded" - | "sigma.scheduled_query_run.created" - | "sku.created" - | "sku.deleted" - | "sku.updated" - | "source.canceled" - | "source.chargeable" - | "source.failed" - | "source.mandate_notification" - | "source.refund_attributes_required" - | "source.transaction.created" - | "source.transaction.updated" - | "subscription_schedule.aborted" - | "subscription_schedule.canceled" - | "subscription_schedule.completed" - | "subscription_schedule.created" - | "subscription_schedule.expiring" - | "subscription_schedule.released" - | "subscription_schedule.updated" - | "tax_rate.created" - | "tax_rate.updated" - | "topup.created" - | "topup.failed" - | "topup.reversed" - | "topup.succeeded" - | "transfer.created" - | "transfer.failed" - | "transfer.paid" - | "transfer.reversed" - | "transfer.updated"; + | '*' + | 'account.updated' + | 'account.application.authorized' + | 'account.application.deauthorized' + | 'account.external_account.created' + | 'account.external_account.deleted' + | 'account.external_account.updated' + | 'application_fee.created' + | 'application_fee.refunded' + | 'application_fee.refund.updated' + | 'balance.available' + | 'capability.updated' + | 'charge.captured' + | 'charge.expired' + | 'charge.failed' + | 'charge.pending' + | 'charge.refunded' + | 'charge.succeeded' + | 'charge.updated' + | 'charge.dispute.closed' + | 'charge.dispute.created' + | 'charge.dispute.funds_reinstated' + | 'charge.dispute.funds_withdrawn' + | 'charge.dispute.updated' + | 'charge.refund.updated' + | 'checkout.session.completed' + | 'coupon.created' + | 'coupon.deleted' + | 'coupon.updated' + | 'credit_note.created' + | 'credit_note.updated' + | 'credit_note.voided' + | 'customer.created' + | 'customer.deleted' + | 'customer.updated' + | 'customer.discount.created' + | 'customer.discount.deleted' + | 'customer.discount.updated' + | 'customer.source.created' + | 'customer.source.deleted' + | 'customer.source.expiring' + | 'customer.source.updated' + | 'customer.subscription.created' + | 'customer.subscription.deleted' + | 'customer.subscription.trial_will_end' + | 'customer.subscription.updated' + | 'customer.tax_id.created' + | 'customer.tax_id.deleted' + | 'customer.tax_id.updated' + | 'file.created' + | 'invoice.created' + | 'invoice.deleted' + | 'invoice.finalized' + | 'invoice.marked_uncollectible' + | 'invoice.payment_action_required' + | 'invoice.payment_failed' + | 'invoice.payment_succeeded' + | 'invoice.sent' + | 'invoice.upcoming' + | 'invoice.updated' + | 'invoice.voided' + | 'invoiceitem.created' + | 'invoiceitem.deleted' + | 'invoiceitem.updated' + | 'issuing_authorization.created' + | 'issuing_authorization.request' + | 'issuing_authorization.updated' + | 'issuing_card.created' + | 'issuing_card.updated' + | 'issuing_cardholder.created' + | 'issuing_cardholder.updated' + | 'issuing_dispute.created' + | 'issuing_dispute.updated' + | 'issuing_settlement.created' + | 'issuing_settlement.updated' + | 'issuing_transaction.created' + | 'issuing_transaction.updated' + | 'mandate.updated' + | 'order.created' + | 'order.payment_failed' + | 'order.payment_succeeded' + | 'order.updated' + | 'order_return.created' + | 'payment_intent.amount_capturable_updated' + | 'payment_intent.canceled' + | 'payment_intent.created' + | 'payment_intent.payment_failed' + | 'payment_intent.succeeded' + | 'payment_method.attached' + | 'payment_method.card_automatically_updated' + | 'payment_method.detached' + | 'payment_method.updated' + | 'payout.canceled' + | 'payout.created' + | 'payout.failed' + | 'payout.paid' + | 'payout.updated' + | 'person.created' + | 'person.deleted' + | 'person.updated' + | 'plan.created' + | 'plan.deleted' + | 'plan.updated' + | 'product.created' + | 'product.deleted' + | 'product.updated' + | 'radar.early_fraud_warning.created' + | 'radar.early_fraud_warning.updated' + | 'recipient.created' + | 'recipient.deleted' + | 'recipient.updated' + | 'reporting.report_run.failed' + | 'reporting.report_run.succeeded' + | 'reporting.report_type.updated' + | 'review.closed' + | 'review.opened' + | 'setup_intent.canceled' + | 'setup_intent.created' + | 'setup_intent.setup_failed' + | 'setup_intent.succeeded' + | 'sigma.scheduled_query_run.created' + | 'sku.created' + | 'sku.deleted' + | 'sku.updated' + | 'source.canceled' + | 'source.chargeable' + | 'source.failed' + | 'source.mandate_notification' + | 'source.refund_attributes_required' + | 'source.transaction.created' + | 'source.transaction.updated' + | 'subscription_schedule.aborted' + | 'subscription_schedule.canceled' + | 'subscription_schedule.completed' + | 'subscription_schedule.created' + | 'subscription_schedule.expiring' + | 'subscription_schedule.released' + | 'subscription_schedule.updated' + | 'tax_rate.created' + | 'tax_rate.updated' + | 'topup.created' + | 'topup.failed' + | 'topup.reversed' + | 'topup.succeeded' + | 'transfer.created' + | 'transfer.failed' + | 'transfer.paid' + | 'transfer.reversed' + | 'transfer.updated'; interface IEvent extends IResourceObject { /** @@ -7746,6 +7748,102 @@ declare namespace Stripe { } } + namespace oauth { + interface IOAuthAuthorizationCodeTokenRequest { + /** + * authorization_code when turning an authorization code into an access token + */ + grant_type: 'authorization_code'; + + /** + * The value of the code + */ + code: string; + + /** + * Has no effect when requesting an access token from an authorization code. + */ + scope?: string; + + /** + * Check whether the suggested_capabilities were applied to the connected account. + */ + assert_capabilities?: string; + } + + interface IOAuthRefreshTokenRequest { + /** + * use a refresh token to get a new access token. + */ + grant_type: 'refresh_token'; + + /** + * The value of the refresh_token + */ + refresh_token: string; + + /** + * When requesting a new access token from a refresh token, any scope that has an equal or lesser scope as the refresh token. + * + * Defaults to the scope of the refresh token. + */ + scope?: string; + + /** + * Check whether the suggested_capabilities were applied to the connected account. + */ + assert_capabilities?: string; + } + + interface IOAuthToken { + /** + * The unique id of the account you have been granted access to, as a string. + */ + stripe_user_id: string; + + /** + * The access token you can use to make requests on behalf of this Stripe account. Use it as you would any Stripe secret API key. + * + * This key does not expire, but may be revoked by the user at any time (you'll get a account.application.deauthorized webhook event when this happens). + */ + access_token: string; + + /** + * The scope granted to the access token, depending on the scope of the authorization code and scope parameter. + */ + scope: string; + + /** + * The live mode indicator for the token. If true, the access_token can be used as a live secret key. If false, the access_token can be used as a test secret key. + * + * Depends on the mode of the secret API key used to make the request. + */ + livemode: boolean; + + /** + * Will always have a value of bearer. + */ + token_type: 'bearer'; + + /** + * Can be used to get a new access token of an equal or lesser scope, or of a different live mode (where applicable). + */ + refresh_token: string; + + /** + * A publishable key that can be used with this account. Matches the mode—live or test—of the token. + */ + stripe_publishable_key: string; + } + + interface IOAuthDeauthorizationResponse { + /** + * The unique id of the account you have revoked access to, as a string. This is the same as the stripe_user_id you passed in. If this is returned, the revocation was successful. + */ + stripe_user_id: string; + } + } + namespace tokens { interface IToken extends ICardToken, IBankAccountToken {} @@ -9779,7 +9877,7 @@ declare namespace Stripe { /** * Value is 'webhook_endpoint' */ - object: "webhook_endpoint"; + object: 'webhook_endpoint'; id: string; @@ -9812,7 +9910,7 @@ declare namespace Stripe { /** * The status of the webhook. */ - status: "enabled" | "disabled"; + status: 'enabled' | 'disabled'; /** * The URL of the webhook endpoint. @@ -13755,6 +13853,40 @@ declare namespace Stripe { retrieve(tokenId: string, response?: IResponseFn): Promise; } + class OAuth extends StripeResource { + /** + * Used both for turning an authorization_code into an access_token, and for getting a new access token using a refresh_token. + * + */ + token( + data: oauth.IOAuthAuthorizationCodeTokenRequest | oauth.IOAuthRefreshTokenRequest, + options: HeaderOptions, + response?: IResponseFn, + ): Promise; + token( + data: oauth.IOAuthAuthorizationCodeTokenRequest | oauth.IOAuthRefreshTokenRequest, + response?: IResponseFn, + ): Promise; + + /** + * When revoking access to an account, you must use an API key that matches the mode—live or test—of the authorization code (which depends on whether the client_id used was production or development). + * + * @param client_id The client_id of the application that you'd like to disconnect the account from. The account must be connected to this application. + * @param stripe_user_id The account you'd like to disconnect from. + */ + deauthorize( + client_id: string, + stripe_user_id: string, + options: HeaderOptions, + response?: IResponseFn, + ): Promise; + deauthorize( + client_id: string, + stripe_user_id: string, + response?: IResponseFn, + ): Promise; + } + class Transfers extends StripeResource { /** * To send funds from your Stripe account to a third-party recipient or to your own bank account, you create a new transfer @@ -14347,7 +14479,10 @@ declare namespace Stripe { options: HeaderOptions, response?: IResponseFn, ): Promise; - create(data: webhookEndpoints.IWebhookCreateOptions, response?: IResponseFn): Promise; + create( + data: webhookEndpoints.IWebhookCreateOptions, + response?: IResponseFn, + ): Promise; /** * Retrieves the details of an existing webhook @@ -14358,9 +14493,20 @@ declare namespace Stripe { options: HeaderOptions, response?: IResponseFn, ): Promise; - retrieve(webhookId: string, data: IDataOptions, response?: IResponseFn): Promise; - retrieve(webhookId: string, options: HeaderOptions, response?: IResponseFn): Promise; - retrieve(webhookId: string, response?: IResponseFn): Promise; + retrieve( + webhookId: string, + data: IDataOptions, + response?: IResponseFn, + ): Promise; + retrieve( + webhookId: string, + options: HeaderOptions, + response?: IResponseFn, + ): Promise; + retrieve( + webhookId: string, + response?: IResponseFn, + ): Promise; /** * Updates the specific webhook endpoint by setting the values of the parameters passed. Any parameters not provided will be left unchanged. @@ -14373,7 +14519,11 @@ declare namespace Stripe { options: HeaderOptions, response?: IResponseFn, ): Promise; - update(webhookId: string, data: webhookEndpoints.IWebhookUpdateOptions, response?: IResponseFn): Promise; + update( + webhookId: string, + data: webhookEndpoints.IWebhookUpdateOptions, + response?: IResponseFn, + ): Promise; /** * Returns a list of your webhook endpoints @@ -14383,9 +14533,17 @@ declare namespace Stripe { options: HeaderOptions, response?: IResponseFn>, ): IListPromise; - list(data: IListOptions, response?: IResponseFn>): IListPromise; - list(options: HeaderOptions, response?: IResponseFn>): IListPromise; - list(response?: IResponseFn>): IListPromise; + list( + data: IListOptions, + response?: IResponseFn>, + ): IListPromise; + list( + options: HeaderOptions, + response?: IResponseFn>, + ): IListPromise; + list( + response?: IResponseFn>, + ): IListPromise; /** * Deletes a webhook endpoint. diff --git a/types/stripe/stripe-tests.ts b/types/stripe/stripe-tests.ts index 56420a97d9..19c3c72415 100644 --- a/types/stripe/stripe-tests.ts +++ b/types/stripe/stripe-tests.ts @@ -1050,14 +1050,26 @@ stripe.customers.listTaxIds('cus_FhdWgak8aeNfht', (err, taxIds) => { // ################################################################################## stripe.transfers.create( - { amount: 100, currency: 'USD', destination: 'acct_17wV8KBoqMA9o2xk', source_type: 'bank_account', transfer_group: 'Order_X' }, + { + amount: 100, + currency: 'USD', + destination: 'acct_17wV8KBoqMA9o2xk', + source_type: 'bank_account', + transfer_group: 'Order_X', + }, (err, reversal) => { // asynchronously called }, ); stripe.transfers - .create({ amount: 100, currency: 'USD', destination: 'acct_17wV8KBoqMA9o2xk', source_type: 'bank_account', transfer_group: 'Order_X' }) + .create({ + amount: 100, + currency: 'USD', + destination: 'acct_17wV8KBoqMA9o2xk', + source_type: 'bank_account', + transfer_group: 'Order_X', + }) .then(reversal => { // asynchronously called }); @@ -1468,6 +1480,36 @@ stripe.products //#endregion +//#region Oauth tests +// ################################################################################## + +stripe.oauth + .token({ + grant_type: 'authorization_code', + code: 'ac_123456789', + }) + .then(response => { + // asynchronously called + const connected_account_id = response.stripe_user_id; + }); + +stripe.oauth + .token({ + grant_type: 'refresh_token', + refresh_token: 'random_refresh_token', + }) + .then(response => { + // asynchronously called + const access_token = response.access_token; + }); + +stripe.oauth.deauthorize('ac_123456789', 'userid_123456789').then(response => { + // asynchronously called + const stripe_user_id = response.stripe_user_id; +}); + +//#endregion + //#region SKUs tests // ################################################################################## @@ -1500,8 +1542,8 @@ const header: string = stripe.webhooks.generateTestHeaderString({ stripe.webhookEndpoints.create( { - url: "https://example.com/success", - enabled_events: ["plan.updated"], + url: 'https://example.com/success', + enabled_events: ['plan.updated'], }, (err, webhookEndpoint) => { // asynchronously called @@ -1509,8 +1551,8 @@ stripe.webhookEndpoints.create( ); stripe.webhookEndpoints .create({ - url: "https://example.com/success", - enabled_events: ["plan.updated"], + url: 'https://example.com/success', + enabled_events: ['plan.updated'], }) .then(webhookEndpoint => { // asynchronously called