Break _library/basic_functions.jst into individual jst files per function
authorNick Downing <nick@ndcode.org>
Sun, 19 Dec 2021 03:07:10 +0000 (14:07 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 19 Dec 2021 03:07:20 +0000 (14:07 +1100)
_library/basic_functions.jst [deleted file]
_library/capture_order.jst [new file with mode: 0644]
_library/create_order.jst [new file with mode: 0644]
_library/get_access_token.jst [new file with mode: 0644]
_library/get_oauth.jst [new file with mode: 0644]
_library/get_order_details.jst [new file with mode: 0644]
_library/patch_order_details.jst [new file with mode: 0644]
api/captureOrder.json.jst
api/createOrder.json.jst
api/getOrderDetails.json.jst
api/patchOrder.json.jst

diff --git a/_library/basic_functions.jst b/_library/basic_functions.jst
deleted file mode 100644 (file)
index fdb4ec8..0000000
+++ /dev/null
@@ -1,175 +0,0 @@
-const https = require('https');
-const get_oauth = config => {
-  return new Promise(resolve => {
-      
-      const data = 'grant_type=client_credentials';
-
-      const options = {
-        hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
-        port: 443,
-        path: '/v1/oauth2/token',
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/x-www-form-urlencoded',
-          'Content-Length': data.length,
-          'Authorization': 'Basic ' + Buffer.from((config.environment === 'sandbox' ? config.sandbox_client_id : config.production_client_id) + ':' + (config.environment === 'sandbox' ? config.sandbox_secret : config.production_secret)).toString('base64')
-        }
-      }
-      const my_callback = function(res){
-
-        let str='';
-
-        res.on('data',function(chunk){
-            str+=chunk;
-        });
-
-        res.on('end',function(){
-            obj=JSON.parse(str);
-            resolve(obj);
-        });
-      }
-      let request = https.request(options, my_callback);
-      request.write(data);
-      request.end();
-
-    });
-};
-const get_access_token = config => {
-  return new Promise(resolve => {
-    get_oauth(config).then((response) => {
-      resolve(response.access_token);
-    });
-  });
-};
-const create_order = (config, item_obj) => {
-  return new Promise(resolve => {
-    get_access_token(config).then((access_token) => {
-  
-    const options = {
-      hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
-      port: 443,
-      path: '/v2/checkout/orders',
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-        'Authorization': 'Bearer ' + access_token
-      }
-    }
-    const my_callback = function(res){
-
-      let str='';
-
-      res.on('data',function(chunk){
-          str+=chunk;
-      });
-
-      res.on('end',function(){
-          obj=JSON.parse(str);
-          resolve(obj);
-      });
-    }
-    let request = https.request(options,my_callback);
-    request.write(JSON.stringify(item_obj));
-    request.end();
-
-  });
-
-  });
-};
-const get_order_details = (config, order_id) => {
-  return new Promise(resolve => {
-    get_access_token(config).then((access_token) => {
-  
-    const options = {
-      hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
-      port: 443,
-      path: '/v2/checkout/orders/' + order_id,
-      method: 'GET',
-      headers: {
-        'Content-Type': 'application/json',
-        'Authorization': 'Bearer ' + access_token
-      }
-    }
-    const my_callback = function(res){
-
-      let str='';
-
-      res.on('data',function(chunk){
-          str+=chunk;
-      });
-
-      res.on('end',function(){
-          obj=JSON.parse(str);
-          resolve(obj);
-      });
-    }
-    let request = https.request(options,my_callback);
-    request.end();
-
-  });
-
-  });
-};
-const patch_order_details = (config, new_order_details) => {
-  return new Promise((resolve, reject) => {
-    get_access_token(config).then((access_token) => {
-      patch_details = new_order_details.patch_details;
-      order_id = new_order_details.order_id;
-      const options = {
-        hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
-        port: 443,
-        path: '/v2/checkout/orders/' + order_id,
-        method: 'PATCH',
-        headers: {
-          'Content-Type': 'application/json',
-          'Authorization': 'Bearer ' + access_token
-        }
-      }
-      let request = https.request(options, function (response) {
-        resolve(response.statusCode);
-      });
-      request.write(JSON.stringify(patch_details));
-      request.end();
-  });
-});
-
-};
-const capture_order = (config, order_id) => {
-  return new Promise(resolve => {
-    get_access_token(config).then((access_token) => {
-    const options = {
-      hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
-      port: 443,
-      path: '/v2/checkout/orders/' + order_id + '/capture',
-      method: 'POST',
-      headers: {
-        'Content-Type': 'application/json',
-        'Authorization': 'Bearer ' + access_token,
-        'return': 'representation',
-        'PayPal-Partner-Attribution-Id': 'PP-DemoPortal-Checkout-NodeJS-SDK'
-      }
-    }
-    const my_callback = function(res){
-      let str='';
-      res.on('data',function(chunk){
-          str+=chunk;
-      });
-      res.on('end',function(){
-          obj=JSON.parse(str);
-          resolve(obj);
-      });
-    }
-    let request = https.request(options,my_callback);
-    request.end();
-    });
-  });
-};
-
-return {
-  get_access_token: get_access_token,
-  get_oauth: get_oauth,
-  create_order: create_order,
-  get_order_details: get_order_details,
-  patch_order_details: patch_order_details,
-  capture_order: capture_order
-};
diff --git a/_library/capture_order.jst b/_library/capture_order.jst
new file mode 100644 (file)
index 0000000..8af6381
--- /dev/null
@@ -0,0 +1,34 @@
+let https = require('https');
+
+return async (config, order_id) => {
+  let get_access_token = await _require('get_access_token.jst');
+
+  return new Promise(resolve => {
+    get_access_token(config).then((access_token) => {
+    const options = {
+      hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
+      port: 443,
+      path: '/v2/checkout/orders/' + order_id + '/capture',
+      method: 'POST',
+      headers: {
+        'Content-Type': 'application/json',
+        'Authorization': 'Bearer ' + access_token,
+        'return': 'representation',
+        'PayPal-Partner-Attribution-Id': 'PP-DemoPortal-Checkout-NodeJS-SDK'
+      }
+    }
+    const my_callback = function(res){
+      let str='';
+      res.on('data',function(chunk){
+          str+=chunk;
+      });
+      res.on('end',function(){
+          obj=JSON.parse(str);
+          resolve(obj);
+      });
+    }
+    let request = https.request(options,my_callback);
+    request.end();
+    });
+  });
+};
diff --git a/_library/create_order.jst b/_library/create_order.jst
new file mode 100644 (file)
index 0000000..baa7ba4
--- /dev/null
@@ -0,0 +1,39 @@
+let https = require('https');
+
+return async (config, item_obj) => {
+  let get_access_token = await _require('get_access_token.jst');
+
+  return new Promise(resolve => {
+    get_access_token(config).then((access_token) => {
+  
+    const options = {
+      hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
+      port: 443,
+      path: '/v2/checkout/orders',
+      method: 'POST',
+      headers: {
+        'Content-Type': 'application/json',
+        'Authorization': 'Bearer ' + access_token
+      }
+    }
+    const my_callback = function(res){
+
+      let str='';
+
+      res.on('data',function(chunk){
+          str+=chunk;
+      });
+
+      res.on('end',function(){
+          obj=JSON.parse(str);
+          resolve(obj);
+      });
+    }
+    let request = https.request(options,my_callback);
+    request.write(JSON.stringify(item_obj));
+    request.end();
+
+  });
+
+  });
+};
diff --git a/_library/get_access_token.jst b/_library/get_access_token.jst
new file mode 100644 (file)
index 0000000..52df668
--- /dev/null
@@ -0,0 +1,12 @@
+let https = require('https');
+
+return async config => {
+  let get_oauth = await _require('get_oauth.jst');
+
+  return new Promise(resolve => {
+    get_oauth(config).then((response) => {
+      resolve(response.access_token);
+    });
+  });
+};
+
diff --git a/_library/get_oauth.jst b/_library/get_oauth.jst
new file mode 100644 (file)
index 0000000..ef7f732
--- /dev/null
@@ -0,0 +1,37 @@
+let https = require('https');
+
+return async config => {
+  return new Promise(resolve => {
+      
+      const data = 'grant_type=client_credentials';
+
+      const options = {
+        hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
+        port: 443,
+        path: '/v1/oauth2/token',
+        method: 'POST',
+        headers: {
+          'Content-Type': 'application/x-www-form-urlencoded',
+          'Content-Length': data.length,
+          'Authorization': 'Basic ' + Buffer.from((config.environment === 'sandbox' ? config.sandbox_client_id : config.production_client_id) + ':' + (config.environment === 'sandbox' ? config.sandbox_secret : config.production_secret)).toString('base64')
+        }
+      }
+      const my_callback = function(res){
+
+        let str='';
+
+        res.on('data',function(chunk){
+            str+=chunk;
+        });
+
+        res.on('end',function(){
+            obj=JSON.parse(str);
+            resolve(obj);
+        });
+      }
+      let request = https.request(options, my_callback);
+      request.write(data);
+      request.end();
+
+    });
+};
diff --git a/_library/get_order_details.jst b/_library/get_order_details.jst
new file mode 100644 (file)
index 0000000..3025472
--- /dev/null
@@ -0,0 +1,38 @@
+let https = require('https');
+
+return async (config, order_id) => {
+  let get_access_token = await _require('get_access_token.jst');
+
+  return new Promise(resolve => {
+    get_access_token(config).then((access_token) => {
+  
+    const options = {
+      hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
+      port: 443,
+      path: '/v2/checkout/orders/' + order_id,
+      method: 'GET',
+      headers: {
+        'Content-Type': 'application/json',
+        'Authorization': 'Bearer ' + access_token
+      }
+    }
+    const my_callback = function(res){
+
+      let str='';
+
+      res.on('data',function(chunk){
+          str+=chunk;
+      });
+
+      res.on('end',function(){
+          obj=JSON.parse(str);
+          resolve(obj);
+      });
+    }
+    let request = https.request(options,my_callback);
+    request.end();
+
+  });
+
+  });
+};
diff --git a/_library/patch_order_details.jst b/_library/patch_order_details.jst
new file mode 100644 (file)
index 0000000..c603cfb
--- /dev/null
@@ -0,0 +1,28 @@
+let https = require('https');
+
+return async (config, new_order_details) => {
+  let get_access_token = await _require('get_access_token.jst');
+
+  return new Promise((resolve, reject) => {
+    get_access_token(config).then((access_token) => {
+      patch_details = new_order_details.patch_details;
+      order_id = new_order_details.order_id;
+      const options = {
+        hostname: 'api.' + (config.environment === 'sandbox' ? 'sandbox.' : '') + 'paypal.com',
+        port: 443,
+        path: '/v2/checkout/orders/' + order_id,
+        method: 'PATCH',
+        headers: {
+          'Content-Type': 'application/json',
+          'Authorization': 'Bearer ' + access_token
+        }
+      }
+      let request = https.request(options, function (response) {
+        resolve(response.statusCode);
+      });
+      request.write(JSON.stringify(patch_details));
+      request.end();
+  });
+});
+
+};
index 4dd59eb..fa9c869 100644 (file)
@@ -1,11 +1,11 @@
 let cookie = require('cookie')
 
 return async env => {
-  let basic_functions = await _require('/_library/basic_functions.jst')
+  let capture_order = await _require('/_library/capture_order.jst')
   let config = await env.site.get_json('/_config/config.json')
 
   let cookies = cookie.parse(env.request.headers.cookie || '')
-  let response = await basic_functions.capture_order(config, cookies.order_id)
+  let response = await capture_order(config, cookies.order_id)
   console.log('capture_order()', response)
 
   env.site.serve(
index 8c851c4..44209bd 100644 (file)
@@ -3,8 +3,8 @@ let stream_buffers = require('stream-buffers')
 let XDate = require('xdate')
 
 return async env => {
-  let basic_functions = await _require('/_library/basic_functions.jst')
   let config = await env.site.get_json('/_config/config.json')
+  let create_order = await _require('/_library/create_order.jst')
   let random_number = await _require('/_library/random_number.jst')
 
   let response = null
@@ -94,7 +94,7 @@ return async env => {
       }
     }
     console.log('item_obj', item_obj)
-    response = await basic_functions.create_order(config, item_obj)
+    response = await create_order(config, item_obj)
     console.log('create_order()', response)
 
     //sess.order_id = response.id
index 4d8026d..af08353 100644 (file)
@@ -1,12 +1,11 @@
 let cookie = require('cookie')
 
 return async env => {
-  let basic_functions = await _require('/_library/basic_functions.jst')
   let config = await env.site.get_json('/_config/config.json')
+  let get_order_details = await _require('/_library/get_order_details.jst')
 
   let cookies = cookie.parse(env.request.headers.cookie || '')
-  let response =
-    await basic_functions.get_order_details(config, cookies.order_id)
+  let response = await get_order_details(config, cookies.order_id)
   console.log('get_order_details()', response)
 
   env.site.serve(
index 41ae5df..c7ff181 100644 (file)
@@ -4,8 +4,8 @@ let stream_buffers = require('stream-buffers')
 let XDate = require('xdate')
 
 return async env => {
-  let basic_functions = await _require('/_library/basic_functions.jst')
   let config = await env.site.get_json('/_config/config.json')
+  let patch_order_details = await _require('/_library/patch_order_details.jst')
   let random_number = await _require('/_library/random_number.jst')
 
   let response = null
@@ -61,8 +61,7 @@ return async env => {
       "order_id": cookies.order_id
     }
     response = {
-      http_code:
-        await basic_functions.patch_order_details(config, new_order_details)
+      http_code: await patch_order_details(config, new_order_details)
     }
     console.log('patch_order_details()', response)
   }