1
1
const express = require ( 'express' ) ;
2
- const { indexOrders } = require ( '../indexing' ) ;
2
+ const { indexOrders, indexTransactions } = require ( '../indexing' ) ;
3
3
const { getId, sendEmail, getEmailTemplate } = require ( '../common' ) ;
4
4
const { getPaymentConfig } = require ( '../config' ) ;
5
5
const { emptyCart } = require ( '../cart' ) ;
@@ -14,11 +14,12 @@ router.get('/checkout_cancel', (req, res, next) => {
14
14
router . get ( '/checkout_return' , ( req , res , next ) => {
15
15
const db = req . app . db ;
16
16
const config = req . app . config ;
17
+ const paypalConfig = getPaymentConfig ( 'paypal' ) ;
17
18
const paymentId = req . session . paymentId ;
18
19
const payerId = req . query . PayerID ;
19
20
20
21
const details = { payer_id : payerId } ;
21
- paypal . payment . execute ( paymentId , details , ( error , payment ) => {
22
+ paypal . payment . execute ( paymentId , details , async ( error , payment ) => {
22
23
let paymentApproved = false ;
23
24
let paymentMessage = '' ;
24
25
let paymentDetails = '' ;
@@ -50,7 +51,7 @@ router.get('/checkout_return', (req, res, next) => {
50
51
if ( payment . state === 'approved' ) {
51
52
paymentApproved = true ;
52
53
paymentStatus = 'Paid' ;
53
- paymentMessage = 'Your payment was successfully completed ' ;
54
+ paymentMessage = 'Succeeded ' ;
54
55
paymentDetails = `<p><strong>Order ID: </strong>${ paymentOrderId } </p><p><strong>Transaction ID: </strong>${ payment . id } </p>` ;
55
56
56
57
// clear the cart
@@ -62,16 +63,34 @@ router.get('/checkout_return', (req, res, next) => {
62
63
// failed
63
64
if ( payment . failureReason ) {
64
65
paymentApproved = false ;
65
- paymentMessage = `Your payment failed - ${ payment . failureReason } ` ;
66
+ paymentMessage = `Declined: ${ payment . failureReason } ` ;
66
67
paymentStatus = 'Declined' ;
67
68
}
68
69
70
+ // Create our transaction
71
+ const transaction = await db . transactions . insertOne ( {
72
+ gateway : 'paypal' ,
73
+ gatewayReference : payment . id ,
74
+ gatewayMessage : paymentMessage ,
75
+ approved : paymentApproved ,
76
+ amount : req . session . totalCartAmount ,
77
+ currency : paypalConfig . paypalCurrency ,
78
+ customer : getId ( req . session . customerId ) ,
79
+ created : new Date ( ) ,
80
+ order : getId ( paymentOrderId )
81
+ } ) ;
82
+
83
+ const transactionId = transaction . insertedId ;
84
+
85
+ // Index transactios
86
+ await indexTransactions ( req . app ) ;
87
+
69
88
// update the order status
70
- db . orders . updateOne ( { _id : getId ( paymentOrderId ) } , { $set : { orderStatus : paymentStatus } } , { multi : false } , ( err , numReplaced ) => {
89
+ db . orders . updateOne ( { _id : getId ( paymentOrderId ) } , { $set : { orderStatus : paymentStatus , transaction : transactionId } } , { multi : false } , ( err , numReplaced ) => {
71
90
if ( err ) {
72
91
console . info ( err . stack ) ;
73
92
}
74
- db . orders . findOne ( { _id : getId ( paymentOrderId ) } , ( err , order ) => {
93
+ db . orders . findOne ( { _id : getId ( paymentOrderId ) } , async ( err , order ) => {
75
94
if ( err ) {
76
95
console . info ( err . stack ) ;
77
96
}
@@ -134,7 +153,7 @@ router.post('/checkout_action', (req, res, next) => {
134
153
paypal . configure ( paypalConfig ) ;
135
154
136
155
// create payment
137
- paypal . payment . create ( payment , ( error , payment ) => {
156
+ paypal . payment . create ( payment , async ( error , payment ) => {
138
157
if ( error ) {
139
158
req . session . message = 'There was an error processing your payment. You have not been charged and can try again.' ;
140
159
req . session . messageType = 'danger' ;
0 commit comments