medusa-payment-solana
Version:
Solana crypto currency payment provider for MedusaJS 2.0
54 lines • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = checkPaymentsJob;
const utils_1 = require("@medusajs/framework/utils");
const core_flows_1 = require("@medusajs/medusa/core-flows");
async function checkPaymentsJob(container) {
const logger = container.resolve('logger');
logger.info('Running check payments job');
const paymentModuleService = container.resolve(utils_1.Modules.PAYMENT);
const SolanaPaymentSessions = await paymentModuleService.listPaymentSessions({
provider_id: 'pp_solana_solana',
});
const pendingPaymentSessions = SolanaPaymentSessions.filter(paymentSession => paymentSession.status === 'pending');
for (const session of pendingPaymentSessions) {
try {
// Check for authorization status of the payment session. This will trigger authorizePayment in service.ts that checks against the blockchain. Error is thrown if not authorized yet.
const payment = await paymentModuleService.authorizePaymentSession(session.id, {});
const query = container.resolve('query');
const { data: paymentCollections } = await query.graph({
entity: 'payment_collection',
fields: ['*', 'cart.*'],
filters: {
id: payment.payment_collection_id
}
});
const cartId = paymentCollections[0]?.cart?.id;
// Complete the cart to place the order
const { result } = await (0, core_flows_1.completeCartWorkflow)(container).run({
input: {
id: cartId
}
});
logger.info(`Successfully completed cart ${cartId} for session ${session.id}, ${JSON.stringify(result)}`);
// If we get here, authorization succeeded, so capture the payment
await (0, core_flows_1.capturePaymentWorkflow)(container).run({
input: {
payment_id: payment.id
}
});
logger.info(`Successfully captured payment for session ${session.id}`);
}
catch (error) {
const medusaError = error;
if (medusaError.type === 'not_allowed' &&
medusaError.message.includes('was not authorized with the provider')) {
logger.info(`Payment session ${session.id} not yet ready for authorization`);
}
else {
logger.error(`Error processing payment session ${session.id}: ${medusaError.message}`);
}
}
}
}
//# sourceMappingURL=check-payment.js.map