Skip to content

Commit

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

* docs: Add updatePaymentIntent example

* docs: Fix typo

* refactor: Use PUT method
  • Loading branch information
ynnoj authored Jan 27, 2021
1 parent d270753 commit fd9df55
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ const session = await createPaymentIntent({
})
```

#### Update

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

const paymentIntent = await updatePaymentIntent('pi_id', {
amount: 1000,
currency: 'usd'
})
```

## Acknowledgements

- A lot of the patterns in this library were inspred by [NextAuth](https://github.com/nextauthjs/next-auth).
Expand Down
27 changes: 26 additions & 1 deletion src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,29 @@ async function createPaymentIntent(body) {
return res.json()
}

export default { createCheckoutSession, createPaymentIntent }
async function updatePaymentIntent(id, body) {
const res = await fetch(`/api/stripe/update/payment-intent`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id, body })
})

if (!res.ok) {
const error = new Error('An error occurred while performing this request.')

error.info = await res.json()
error.status = res.status

throw error
}

return res.json()
}

export default {
createCheckoutSession,
createPaymentIntent,
updatePaymentIntent
}
5 changes: 5 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ async function NextStripeHandler(req, res, options) {
case 'payment-intent':
return routes.createPaymentIntent(req, res, options)
}
} else if (method === 'update') {
switch (type) {
case 'payment-intent':
return routes.updatePaymentIntent(req, res, options)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Create
export { default as createCheckoutSession } from './create/checkout-session'
export { default as createPaymentIntent } from './create/payment-intent'
// Update
export { default as updatePaymentIntent } from './update/payment-intent'
15 changes: 15 additions & 0 deletions src/server/routes/update/payment-intent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Stripe from 'stripe'

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

const { id, body } = req.body

const paymentIntent = await stripe.paymentIntents.update(id, body)

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

0 comments on commit fd9df55

Please sign in to comment.