Skip to content

Commit

Permalink
feat: Support refund and cancel for Stripe payment integration #676
Browse files Browse the repository at this point in the history
  • Loading branch information
treoden committed Dec 19, 2024
1 parent 1d1abc7 commit a67fc93
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 131 deletions.
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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const { cancelOrder } = require('../../services/cancelOrder');
// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, deledate, next) => {
try {
await cancelOrder(request.params.id);
const { reason } = request.body;
await cancelOrder(request.params.id, reason);
response.status(OK);
response.json({
data: {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "object",
"properties": {
"reason": {
"type": "string"
}
},
"required": ["reason"],
"additionalProperties": true,
"errorMessage": {
"properties": {
"reason": "Reason is mandatory"
}
}
}
10 changes: 7 additions & 3 deletions packages/evershop/src/modules/oms/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ module.exports = () => {
}
},
additionalProperties: false
},
reStockAfterCancellation: {
type: 'boolean'
}
},
required: ['shipmentStatus', 'paymentStatus'],
Expand Down Expand Up @@ -119,7 +122,7 @@ module.exports = () => {
},
canceled: {
name: 'Canceled',
badge: 'success',
badge: 'critical',
progress: 'complete',
isCancelable: false
}
Expand All @@ -139,11 +142,12 @@ module.exports = () => {
},
canceled: {
name: 'Canceled',
badge: 'success',
badge: 'critical',
progress: 'complete',
isCancelable: false
}
}
},
reStockAfterCancellation: true
},
carriers: {
default: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function Activities({ order: { activities = [] } }) {
});
} else {
dailyActivities.push({
date: element.createdAt.value,
date: element.createdAt.date,
activities: [
{
comment: element.comment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,95 @@ import PropTypes from 'prop-types';
import React from 'react';
import Button from '@components/common/form/Button';
import { useAlertContext } from '@components/common/modal/Alert';
import { Form } from '@components/common/form/Form';
import { Field } from '@components/common/form/Field';
import { toast } from 'react-toastify';
import RenderIfTrue from '@components/common/RenderIfTrue';

export default function CancelButton({ order: { cancelApi } }) {
export default function CancelButton({ order: { cancelApi, paymentStatus } }) {
const { openAlert, closeAlert, dispatchAlert } = useAlertContext();
return (
<Button
title="Cancel Order"
variant="danger"
onAction={() => {
openAlert({
heading: 'Cancel Order',
content: <div>Are you sure you want to cancel this order?</div>,
primaryAction: {
title: 'Cancel',
onAction: closeAlert,
variant: ''
},
secondaryAction: {
title: 'Cancel',
onAction: () => {
dispatchAlert({
type: 'update',
payload: { secondaryAction: { isLoading: true } }
});
window
.fetch(cancelApi, {
method: 'POST'
})
.then((response) => response.json())
.then((response) => {
if (response.error) {
toast.error(response.error.message);
<RenderIfTrue condition={paymentStatus.code !== 'canceled'}>
<Button
title="Cancel Order"
variant="critical"
onAction={() => {
openAlert({
heading: 'Cancel Order',
content: (
<div>
<Form
id="cancelReason"
method="POST"
action={cancelApi}
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 } }
});
} else {
// Reload the page
window.location.reload();
}
});
}}
>
<div>
<Field
formId="cancelReason"
type="textarea"
name="reason"
label="Reason for cancellation"
placeHolder="Reason for cancellation"
value=""
validationRules={['notEmpty']}
/>
</div>
</Form>
</div>
),
primaryAction: {
title: 'Cancel',
onAction: closeAlert,
variant: ''
},
variant: 'primary',
isLoading: false
}
});
}}
/>
secondaryAction: {
title: 'Cancel Order',
onAction: () => {
dispatchAlert({
type: 'update',
payload: { secondaryAction: { isLoading: true } }
});
document
.getElementById('cancelReason')
.dispatchEvent(
new Event('submit', { cancelable: true, bubbles: true })
);
},
variant: 'primary',
isLoading: false
}
});
}}
/>
</RenderIfTrue>
);
}

CancelButton.propTypes = {
order: PropTypes.shape({
paymentStatus: PropTypes.shape({
code: PropTypes.string
}).isRequired,
cancelApi: PropTypes.string.isRequired
}).isRequired
};
Expand All @@ -69,6 +105,9 @@ export const layout = {
export const query = `
query Query {
order(uuid: getContextValue("orderId")) {
paymentStatus {
code
}
cancelApi
}
}
Expand Down
Loading

0 comments on commit a67fc93

Please sign in to comment.