-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DASH-150 add payments job to check payment statuses from stripe and u…
…pdate order status
- Loading branch information
1 parent
982b6b3
commit 1a522cc
Showing
9 changed files
with
89 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import CommonJob from './job.mjs'; | ||
|
||
import Stripe from '../external/payment-processor/stripe.mjs'; | ||
|
||
import logger from '../logger.mjs'; | ||
|
||
import { Order, Payment, OrderItem, OrderItemStatus } from '../models'; | ||
|
||
// https://stripe.com/docs/payments/intents#intent-statuses | ||
const STRIPE_INTENT_SUCCESS = 'succeeded'; | ||
|
||
class PaymentsJob extends CommonJob { | ||
async execute() { | ||
const orders = await Order.findAll({ | ||
where: { | ||
status: Order.STATUS.PAYMENT_PENDING, | ||
}, | ||
include: [OrderItem, Payment], | ||
}); | ||
|
||
const t = await Order.sequelize.transaction(); | ||
|
||
try { | ||
for (const order of orders) { | ||
const payments = await order.getPayments({ | ||
where: { processor: Payment.PROCESSOR.STRIPE }, | ||
}); | ||
const paymentStatuses = []; | ||
for (const payment of payments) { | ||
const orderItemStatus = await OrderItemStatus.findOne({ | ||
where: { | ||
paymentId: payment.id, | ||
}, | ||
transaction: t, | ||
}); | ||
|
||
const intentId = payment.externalId; | ||
const intent = await Stripe.retrieveIntent(intentId); | ||
paymentStatuses.push(intent.status); | ||
if (intent.status === STRIPE_INTENT_SUCCESS) { | ||
await payment.update( | ||
{ status: Payment.STATUS.COMPLETED }, | ||
{ transaction: t }, | ||
); | ||
await orderItemStatus.update( | ||
{ paymentStatus: Payment.STATUS.COMPLETED }, | ||
{ transaction: t }, | ||
); | ||
} else { | ||
await payment.update({ status: Payment.STATUS.FAILED }, { transaction: t }); | ||
await orderItemStatus.update( | ||
{ paymentStatus: Payment.STATUS.FAILED }, | ||
{ transaction: t }, | ||
); | ||
} | ||
} | ||
|
||
const allSucceeded = paymentStatuses.some( | ||
status => status === STRIPE_INTENT_SUCCESS, | ||
); | ||
if (allSucceeded) { | ||
await order.update({ status: Order.STATUS.SUCCESS }, { transaction: t }); | ||
} else { | ||
await order.update({ status: Order.STATUS.FAILED }, { transaction: t }); | ||
} | ||
} | ||
await t.commit(); | ||
} catch (error) { | ||
logger.error(`Update transaction failed error: ${error.message}`); | ||
await t.rollback(); | ||
} | ||
} | ||
} | ||
|
||
new PaymentsJob().execute(); |
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
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
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