-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support refund and cancel for Stripe payment integration #676
- Loading branch information
Showing
11 changed files
with
316 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/evershop/src/modules/stripe/api/capturePaymentIntent/route.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"methods": ["POST"], | ||
"path": "/stripe/paymentIntents/capture", | ||
"access": "public" | ||
"access": "private" | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/evershop/src/modules/stripe/api/refundPaymentIntent/[context]bodyParser[auth].js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const bodyParser = require('body-parser'); | ||
|
||
module.exports = (request, response, delegate, next) => { | ||
bodyParser.json({ inflate: false })(request, response, next); | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/evershop/src/modules/stripe/api/refundPaymentIntent/payloadSchema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"order_id": { | ||
"type": "string" | ||
}, | ||
"amount": { | ||
"type": ["string", "number"], | ||
"pattern": "^\\d+(\\.\\d{1,2})?$", | ||
"errorMessage": { | ||
"pattern": "Amount should be a number with maximum 2 decimal places" | ||
} | ||
} | ||
}, | ||
"required": ["order_id"], | ||
"additionalProperties": true, | ||
"errorMessage": { | ||
"properties": { | ||
"order_id": "Order is invalid" | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
packages/evershop/src/modules/stripe/api/refundPaymentIntent/refundPaymentIntent.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const stripePayment = require('stripe'); | ||
const smallestUnit = require('zero-decimal-currencies'); | ||
const { getConfig } = require('@evershop/evershop/src/lib/util/getConfig'); | ||
const { | ||
OK, | ||
INVALID_PAYLOAD, | ||
INTERNAL_SERVER_ERROR | ||
} = require('@evershop/evershop/src/lib/util/httpStatus'); | ||
const { error } = require('@evershop/evershop/src/lib/log/logger'); | ||
const { pool } = require('@evershop/evershop/src/lib/postgres/connection'); | ||
const { | ||
select, | ||
getConnection, | ||
startTransaction, | ||
insert, | ||
commit, | ||
rollback | ||
} = require('@evershop/postgres-query-builder'); | ||
const { getSetting } = require('../../../setting/services/setting'); | ||
const { | ||
updatePaymentStatus | ||
} = require('../../../oms/services/updatePaymentStatus'); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
module.exports = async (request, response, delegate, next) => { | ||
const connection = await getConnection(pool); | ||
try { | ||
await startTransaction(connection); | ||
// eslint-disable-next-line camelcase | ||
const { order_id, amount } = request.body; | ||
// Load the order | ||
const order = await select() | ||
.from('order') | ||
.where('order_id', '=', order_id) | ||
.load(connection); | ||
if (!order || order.payment_method !== 'stripe') { | ||
response.status(INVALID_PAYLOAD); | ||
response.json({ | ||
error: { | ||
status: INVALID_PAYLOAD, | ||
message: 'Invalid order' | ||
} | ||
}); | ||
return; | ||
} | ||
|
||
// Get the payment transaction | ||
const paymentTransaction = await select() | ||
.from('payment_transaction') | ||
.where('payment_transaction_order_id', '=', order.order_id) | ||
.load(connection); | ||
if (!paymentTransaction) { | ||
response.status(INVALID_PAYLOAD); | ||
response.json({ | ||
error: { | ||
status: INVALID_PAYLOAD, | ||
message: 'Can not find payment transaction' | ||
} | ||
}); | ||
return; | ||
} | ||
|
||
const stripeConfig = getConfig('system.stripe', {}); | ||
let stripeSecretKey; | ||
|
||
if (stripeConfig.secretKey) { | ||
stripeSecretKey = stripeConfig.secretKey; | ||
} else { | ||
stripeSecretKey = await getSetting('stripeSecretKey', ''); | ||
} | ||
const stripe = stripePayment(stripeSecretKey); | ||
// Refund | ||
const refund = await stripe.refunds.create({ | ||
payment_intent: paymentTransaction.transaction_id, | ||
amount: smallestUnit.default(amount, order.currency) | ||
}); | ||
const charge = await stripe.charges.retrieve(refund.charge); | ||
// Update the order status | ||
const status = charge.refunded === true ? 'refunded' : 'partial_refunded'; | ||
await updatePaymentStatus(order.order_id, status, connection); | ||
await insert('order_activity') | ||
.given({ | ||
order_activity_order_id: order.order_id, | ||
comment: `Refunded ${amount} ${charge.currency}` | ||
}) | ||
.execute(connection); | ||
await commit(connection); | ||
response.status(OK); | ||
response.json({ | ||
data: { | ||
amount: refund.amount | ||
} | ||
}); | ||
} catch (err) { | ||
error(err); | ||
await rollback(connection); | ||
response.status(INTERNAL_SERVER_ERROR); | ||
response.json({ | ||
error: { | ||
status: INTERNAL_SERVER_ERROR, | ||
message: err.message | ||
} | ||
}); | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/evershop/src/modules/stripe/api/refundPaymentIntent/route.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"methods": ["POST"], | ||
"path": "/stripe/paymentIntents/refund", | ||
"access": "private" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/evershop/src/modules/stripe/pages/admin/orderEdit/StripeRefundButton.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { toast } from 'react-toastify'; | ||
import Button from '@components/common/form/Button'; | ||
import RenderIfTrue from '@components/common/RenderIfTrue'; | ||
import { useAlertContext } from '@components/common/modal/Alert'; | ||
import { Card } from '@components/admin/cms/Card'; | ||
import { Form } from '@components/common/form/Form'; | ||
import { Field } from '@components/common/form/Field'; | ||
|
||
export default function StripeRefundButton({ | ||
refundAPI, | ||
order: { paymentStatus, orderId, paymentMethod, grandTotal } | ||
}) { | ||
const { openAlert, closeAlert, dispatchAlert } = useAlertContext(); | ||
return ( | ||
<RenderIfTrue | ||
condition={ | ||
paymentMethod === 'stripe' && | ||
['paid', 'partial_refunded'].includes(paymentStatus.code) | ||
} | ||
> | ||
<Card.Session> | ||
<div className="flex justify-end"> | ||
<Button | ||
title="Refund" | ||
variant="secondary" | ||
onAction={() => { | ||
openAlert({ | ||
heading: 'Refund', | ||
content: ( | ||
<div> | ||
<Form | ||
id="stripeRefund" | ||
method="POST" | ||
action={refundAPI} | ||
submitBtn={false} | ||
isJSON | ||
onSuccess={(response) => { | ||
if (response.error) { | ||
toast.error(response.error.message); | ||
dispatchAlert({ | ||
type: 'update', | ||
payload: { secondaryAction: { isLoading: false } } | ||
}); | ||
} else { | ||
// Reload the page | ||
window.location.reload(); | ||
} | ||
}} | ||
onValidationError={() => { | ||
dispatchAlert({ | ||
type: 'update', | ||
payload: { secondaryAction: { isLoading: false } } | ||
}); | ||
}} | ||
> | ||
<div> | ||
<Field | ||
formId="stripeRefund" | ||
type="text" | ||
name="amount" | ||
label="Refund amount" | ||
placeHolder="Refund amount" | ||
value={grandTotal.value} | ||
validationRules={['notEmpty']} | ||
suffix={grandTotal.currency} | ||
/> | ||
</div> | ||
<input type="hidden" name="order_id" value={orderId} /> | ||
</Form> | ||
</div> | ||
), | ||
primaryAction: { | ||
title: 'Cancel', | ||
onAction: closeAlert, | ||
variant: '' | ||
}, | ||
secondaryAction: { | ||
title: 'Refund', | ||
onAction: () => { | ||
dispatchAlert({ | ||
type: 'update', | ||
payload: { secondaryAction: { isLoading: true } } | ||
}); | ||
document | ||
.getElementById('stripeRefund') | ||
.dispatchEvent( | ||
new Event('submit', { cancelable: true, bubbles: true }) | ||
); | ||
}, | ||
variant: 'primary', | ||
isLoading: false | ||
} | ||
}); | ||
}} | ||
/> | ||
</div> | ||
</Card.Session> | ||
</RenderIfTrue> | ||
); | ||
} | ||
|
||
StripeRefundButton.propTypes = { | ||
refundAPI: PropTypes.string.isRequired, | ||
order: PropTypes.shape({ | ||
paymentStatus: PropTypes.shape({ | ||
code: PropTypes.string.isRequired | ||
}).isRequired, | ||
orderId: PropTypes.string.isRequired, | ||
paymentMethod: PropTypes.string.isRequired, | ||
grandTotal: PropTypes.shape({ | ||
value: PropTypes.number.isRequired, | ||
currency: PropTypes.string.isRequired | ||
}).isRequired | ||
}).isRequired | ||
}; | ||
|
||
export const layout = { | ||
areaId: 'orderPaymentActions', | ||
sortOrder: 10 | ||
}; | ||
|
||
export const query = ` | ||
query Query { | ||
refundAPI: url(routeId: "refundPaymentIntent") | ||
order(uuid: getContextValue("orderId")) { | ||
orderId | ||
grandTotal { | ||
value | ||
currency | ||
} | ||
paymentStatus { | ||
code | ||
} | ||
paymentMethod | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters