Skip to content

Commit

Permalink
feat: Add retrievePaymentIntent (#11)
Browse files Browse the repository at this point in the history
* feat: Add route, function to retieve a Stripe PaymentIntent

* docs: Add retrievePaymentIntent example
  • Loading branch information
ynnoj authored Jan 27, 2021
1 parent 2509fa0 commit db2a8f7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ const paymentIntent = await confirmPaymentIntent('pi_id', {
})
```

#### Retrieve

[Stripe API Docs](https://stripe.com/docs/api/payment_intents/retrieve)

```js
import { retrievePaymentIntent } from 'next-stripe/client'

const paymentIntent = await retrievePaymentIntent('pi_id')
```

#### Update

[Stripe API Docs](https://stripe.com/docs/api/payment_intents/update)
Expand Down
9 changes: 9 additions & 0 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ async function createPaymentIntent(body) {
})
}

async function retrievePaymentIntent(id) {
return await fetcher({
body: { id },
method: 'GET',
url: `/api/stripe/retrieve/payment-intent`
})
}

async function updatePaymentIntent(id, body) {
return await fetcher({
body: { id, body },
Expand All @@ -44,5 +52,6 @@ export default {
createBillingPortalSession,
createCheckoutSession,
createPaymentIntent,
retrievePaymentIntent,
updatePaymentIntent
}
5 changes: 5 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ async function NextStripeHandler(req, res, options) {
case 'payment-intent':
return routes.createPaymentIntent(req, res, options)
}
} else if (method === 'retrieve') {
switch (type) {
case 'payment-intent':
return routes.retrievePaymentIntent(req, res, options)
}
} else if (method === 'update') {
switch (type) {
case 'payment-intent':
Expand Down
2 changes: 2 additions & 0 deletions src/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export { default as confirmPaymentIntent } from './confirm/payment-intent'
export { default as createBillingPortalSession } from './create/billing-portal-session'
export { default as createCheckoutSession } from './create/checkout-session'
export { default as createPaymentIntent } from './create/payment-intent'
// Retrieve
export { default as retrievePaymentIntent } from './retrieve/payment-intent'
// Update
export { default as updatePaymentIntent } from './update/payment-intent'
13 changes: 13 additions & 0 deletions src/server/routes/retrieve/payment-intent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Stripe from 'stripe'

export default async function retrievePaymentIntent(req, res, options) {
try {
const stripe = new Stripe(options.secret_key)

const paymentIntent = await stripe.paymentIntents.retrieve(req.body.id)

res.status(200).json(paymentIntent)
} catch ({ statusCode, raw: { message } }) {
res.status(statusCode).json({ message, status: statusCode })
}
}

0 comments on commit db2a8f7

Please sign in to comment.