Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate basket after payments fail #166

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/adyen-salesforce-pwa/lib/api/controllers/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ async function sendPayments(req, res, next) {
}

let order
let initialBasket;

try {
const {data} = req.body
const {siteId} = req.query
Expand All @@ -238,25 +240,25 @@ async function sendPayments(req, res, next) {
headers: {authorization: req.headers.authorization}
})

const basket = await shopperBaskets.getBasket({
initialBasket = await shopperBaskets.getBasket({
parameters: {
basketId: req.headers.basketid
}
})

if (!basket) {
if (!initialBasket) {
throw new AdyenError(errorMessages.INVALID_BASKET, 404)
}

if (!basket?.paymentInstruments || !basket?.paymentInstruments?.length) {
if (!initialBasket?.paymentInstruments || !initialBasket?.paymentInstruments?.length) {
Logger.info('sendPayments', 'addPaymentInstrumentToBasket')
const isCardPayment = data?.paymentMethod?.type === 'scheme';
const paymentMethodId = isCardPayment
? PAYMENT_METHODS.CREDIT_CARD
: PAYMENT_METHODS.ADYEN_COMPONENT;
const paymentInstrumentReq = {
body: {
amount: basket.orderTotal,
amount: initialBasket.orderTotal,
paymentMethodId,
paymentCard: {
cardType: isCardPayment
Expand Down Expand Up @@ -362,11 +364,16 @@ async function sendPayments(req, res, next) {
}
})
if (basket?.paymentInstruments?.length) {
Logger.info('removeAllPaymentInstrumentsFromBasket');
await removeAllPaymentInstrumentsFromBasket(basket, shopperBaskets)
}
if (order?.orderNo) {
Logger.info('updateOrderStatus and recreate basket');
const orderApi = new OrderApiClient()
await orderApi.updateOrderStatus(order.orderNo, ORDER.ORDER_STATUS_FAILED)
await shopperBaskets.createBasket({
body: initialBasket,
})
}
next(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {AdyenError} from '../../models/AdyenError'

let mockPayments = jest.fn()
let mockGetBasket = jest.fn()
let mockCreateBasket = jest.fn()
let mockAddPaymentInstrumentToBasket = jest.fn()
let mockRemovePaymentInstrumentFromBasket = jest.fn()
let mockCreateOrder = jest.fn()
Expand Down Expand Up @@ -35,6 +36,7 @@ jest.mock('commerce-sdk-isomorphic', () => {
return {
ShopperBaskets: jest.fn().mockImplementation(() => {
return {
createBasket: mockCreateBasket,
getBasket: mockGetBasket,
addPaymentInstrumentToBasket: mockAddPaymentInstrumentToBasket,
removePaymentInstrumentFromBasket: mockRemovePaymentInstrumentFromBasket
Expand Down Expand Up @@ -372,7 +374,7 @@ describe('payments controller', () => {
await PaymentsController(req, res, next)
expect(res.locals.response).toBeNil()
expect(mockUpdateOrderStatus).toHaveBeenCalled()
expect(consoleInfoSpy).toHaveBeenCalledTimes(2)
expect(consoleInfoSpy).toHaveBeenCalledTimes(3)
expect(consoleInfoSpy.mock.calls[0][0]).toContain('sendPayments start')
expect(consoleInfoSpy.mock.calls[1][0]).toContain('sendPayments orderCreated 123')
expect(consoleErrorSpy).toHaveBeenCalled()
Expand Down Expand Up @@ -999,7 +1001,7 @@ describe('payments controller', () => {

await PaymentsController(req, res, next)
expect(res.locals.response).toBeNil()
expect(consoleInfoSpy).toHaveBeenCalledTimes(3)
expect(consoleInfoSpy).toHaveBeenCalledTimes(4)
expect(consoleInfoSpy.mock.calls[0][0]).toContain('sendPayments start')
expect(consoleInfoSpy.mock.calls[1][0]).toContain('sendPayments orderCreated 123')
expect(consoleInfoSpy.mock.calls[2][0]).toContain('sendPayments resultCode Error')
Expand Down Expand Up @@ -1090,7 +1092,7 @@ describe('payments controller', () => {
await PaymentsController(req, res, next)
expect(res.locals.response).toBeNil()
expect(mockRemovePaymentInstrumentFromBasket).toHaveBeenCalled()
expect(consoleInfoSpy).toHaveBeenCalledTimes(3)
expect(consoleInfoSpy).toHaveBeenCalledTimes(5)
expect(consoleInfoSpy.mock.calls[0][0]).toContain('sendPayments start')
expect(consoleInfoSpy.mock.calls[1][0]).toContain('sendPayments orderCreated 123')
expect(consoleInfoSpy.mock.calls[2][0]).toContain('sendPayments resultCode Error')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const baseConfig = ({
beforeAdditionalDetails = [],
afterAdditionalDetails = [],
onError = () => {
props.onNavigate('/checkout/error')
window.location.reload()
},
...props
}) => {
Expand Down
Loading