@@ -4,92 +4,89 @@ const fetch = require("node-fetch");
4
4
const FoxyWebhook = require ( "../../foxy/FoxyWebhook.js" ) ;
5
5
6
6
const { URLSearchParams } = require ( "url" ) ;
7
- const { FoxyApi } = require ( "@foxy.io/node-api" ) ;
8
-
9
7
10
8
const idevApiUrl = config . idevAffiliate . apiUrl || "" ;
11
9
const idevSecretKey = config . idevAffiliate . secretKey || "" ;
12
10
const foxyWebhookEncryptionKey = config . foxy . webhook . encryptionKey || "" ;
13
11
14
- const getFoxyApi = ( ) => new FoxyApi ( ) ;
15
-
16
12
const getAffiliateIdFromProduct = ( productCode ) => {
17
- if ( productCode . match ( / \- a ( \d + ) $ / i) ) {
18
- return productCode . match ( / \- a ( \d + ) $ / i) [ 1 ] ;
19
- }
20
- return null ;
13
+ const m = productCode . match ( / - a ( \d + ) $ / i)
14
+ return m ? m [ 1 ] : null ;
21
15
} ;
22
16
23
17
// TODO: Use this method instead of just referencing the `price`,
24
18
// as `price` doesn't include discounts, modifiers, coupons, etc.
25
19
const getProductNetPrice = ( productCode , webhook ) => { } ;
26
20
27
- const pushToIdev = async ( item , webhook ) => {
21
+ /**
22
+ * Push a single item to Idev affiliate
23
+ *
24
+ * @param {Object } item to be pushed
25
+ * @param {string|number } webhookId the id of the webhook.
26
+ * @returns {Promise } that resolves to the response of the posting to Idev.
27
+ */
28
+ function pushToIdev ( item , webhookId ) {
28
29
if ( ! item . name || ! item . code || ! item . price ) {
29
30
return false ;
30
31
}
31
32
const params = new URLSearchParams ( ) ;
32
33
params . append ( "affiliate_id" , getAffiliateIdFromProduct ( item . code ) ) ;
33
34
params . append ( "idev_saleamt" , item . price ) ;
34
- params . append ( "idev_ordernum" , webhook . id ) ;
35
+ params . append ( "idev_ordernum" , webhookId ) ;
35
36
// TODO: Check an existing attribute to see if this has already been done.
36
37
// Upsert a Foxy API attribute on the product after pushing so it's not duplicated
37
38
// with a re-run of the webhook.
38
39
return fetch ( idevApiUrl , {
39
- method : "POST" ,
40
40
body : params ,
41
+ method : "POST" ,
41
42
} ) ;
42
- } ;
43
+ }
43
44
44
- const processTransaction = async ( message ) => {
45
- const promises = [ ] ;
46
- try {
47
- for ( let i = 0 ; i < message . _embedded [ "fx:items" ] . length ; i ++ ) {
48
- const item = message . _embedded [ "fx:items" ] [ i ] ;
49
- promises . push ( pushToIdev ( item , message ) ) ;
50
- }
51
- } catch ( error ) {
52
- console . log ( "ERROR in processTransaction:" , error ) ;
53
- console . log ( message ) ;
54
- }
55
- return Promise . all ( promises ) ;
56
- } ;
45
+ /**
46
+ * Processes all transactions within a message.
47
+ *
48
+ * @param {Object } message to be processed
49
+ * @returns {Promise } that resolves to the completion of the processes of all
50
+ * transactions.
51
+ */
52
+ async function processTransaction ( message ) {
53
+ return Promise . all ( message . _embedded [ "fx:items" ]
54
+ . map ( i => pushToIdev ( i , message . id ) )
55
+ ) ;
56
+ }
57
57
58
58
/**
59
59
* Verifies the request as a valid Foxy webhook.
60
60
* @param (string) - The message payload, described here: https://wiki.foxycart.com/v/2.0/webhooks#example_payload
61
61
*/
62
-
63
- exports . handler = async ( event , context ) => {
64
- const foxy = getFoxyApi ( ) ;
65
- const err = FoxyWebhook . validFoxyRequest ( event ) ;
62
+ exports . handler = async ( requestEvent ) => {
63
+ const err = FoxyWebhook . validFoxyRequest ( requestEvent ) ;
66
64
if ( err ) {
67
- return FoxyWebhook . response ( err ) ;
65
+ return FoxyWebhook . response ( err , 400 ) ;
68
66
}
69
- const payload = JSON . parse ( event . body ) ;
67
+ const payload = JSON . parse ( requestEvent . body ) ;
70
68
// Make sure everything looks ok
69
+ if ( requestEvent . headers [ "foxy-webhook-event" ] !== "transaction/created" ) {
70
+ return FoxyWebhook . response ( 'Unsupported event.' , 501 ) ;
71
+ }
71
72
if (
72
- event . headers [ "foxy-webhook-event" ] !== "transaction/created" ||
73
73
! payload . _embedded ||
74
74
! payload . _embedded [ "fx:items" ] ||
75
75
! payload . _embedded [ "fx:items" ] . length > 0
76
76
) {
77
- return FoxyWebhook . response ( "Invalid payload." ) ;
77
+ return FoxyWebhook . response ( "Invalid payload." , 400 ) ;
78
78
}
79
- return processTransaction ( payload )
80
- . then ( ( data ) => {
81
- return {
82
- statusCode : 200 ,
83
- body : JSON . stringify ( { message : "success." } ) ,
84
- } ;
85
- } )
86
- . catch ( ( err ) => {
87
- console . log ( "ERROR:" ) ;
88
- console . log ( err ) ;
89
- console . log ( payload ) ;
90
- return {
91
- statusCode : 400 ,
92
- body : JSON . stringify ( { message : "error. check logs." } ) ,
93
- } ;
94
- } ) ;
95
- } ;
79
+ try {
80
+ await processTransaction ( payload ) ;
81
+ return {
82
+ body : JSON . stringify ( { message : "success." } ) ,
83
+ statusCode : 200 ,
84
+ } ;
85
+ } catch ( e ) {
86
+ console . error ( "ERROR:" , err ) ;
87
+ return {
88
+ body : JSON . stringify ( { message : "error. check logs." } ) ,
89
+ statusCode : 400 ,
90
+ } ;
91
+ }
92
+ }
0 commit comments