Make _library/*.jst routines use async/await more master
authorNick Downing <nick@ndcode.org>
Sun, 19 Dec 2021 04:06:47 +0000 (15:06 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 19 Dec 2021 04:06:47 +0000 (15:06 +1100)
_library/capture_order.jst
_library/create_order.jst
_library/get_access_token.jst
_library/get_oauth.jst
_library/get_order_details.jst
_library/patch_order_details.jst
api/patchOrder.json.jst

index 237e6b8..36ac9d0 100644 (file)
@@ -1,35 +1,32 @@
-let https = require('https');
+let https = require('https')
 
 return async (env, order_id) => {
-  let config = await env.site.get_json('/_config/config.json');
-  let get_access_token = await _require('get_access_token.jst');
+  let config = await env.site.get_json('/_config/config.json')
+  let get_access_token = await _require('get_access_token.jst')
 
-  return new Promise(resolve => {
-    get_access_token(env).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'
-      }
+  let access_token = await get_access_token(env)
+  return new Promise(
+    resolve => {
+      let request = https.request(
+        {
+          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'
+          }
+        },
+        response => {
+          let str = ''
+          response.on('data', chunk => {str += chunk})
+          response.on('end', () => {resolve(JSON.parse(str))})
+        }
+      )
+      request.end()
     }
-    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();
-    });
-  });
-};
+  )
+}
index 7d5a1e9..22bf0fe 100644 (file)
@@ -1,40 +1,31 @@
-let https = require('https');
+let https = require('https')
 
 return async (env, item_obj) => {
-  let config = await env.site.get_json('/_config/config.json');
-  let get_access_token = await _require('get_access_token.jst');
+  let config = await env.site.get_json('/_config/config.json')
+  let get_access_token = await _require('get_access_token.jst')
 
-  return new Promise(resolve => {
-    get_access_token(env).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
-      }
+  let access_token = await get_access_token(env)
+  return new Promise(
+    resolve => {
+      let request = https.request(
+        {
+          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
+          }
+        },
+        response => {
+          let str = ''
+          response.on('data', chunk => {str += chunk})
+          response.on('end', () => {resolve(JSON.parse(str))})
+        }
+      )
+      request.write(JSON.stringify(item_obj));
+      request.end()
     }
-    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();
-
-  });
-
-  });
-};
+  )
+}
index 7dedac0..844bff2 100644 (file)
@@ -1,12 +1,8 @@
-let https = require('https');
+let https = require('https')
 
 return async env => {
-  let get_oauth = await _require('get_oauth.jst');
+  let get_oauth = await _require('get_oauth.jst')
 
-  return new Promise(resolve => {
-    get_oauth(env).then((response) => {
-      resolve(response.access_token);
-    });
-  });
-};
+  return (await get_oauth(env)).access_token
+}
 
index b22ea02..0d54b10 100644 (file)
@@ -1,39 +1,31 @@
-let https = require('https');
+let https = require('https')
 
 return async env => {
-  let config = await env.site.get_json('/_config/config.json');
+  let config = await env.site.get_json('/_config/config.json')
 
-  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')
+  return new Promise(
+    resolve => {
+      let data = 'grant_type=client_credentials'
+      let request = https.request(
+        {
+          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')
+          }
+        },
+        response => {
+          let str = ''
+          response.on('data', chunk => {str += chunk})
+          response.on('end', () => {resolve(JSON.parse(str))})
         }
-      }
-      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();
-
-    });
-};
+      request.end()
+    }
+  )
+}
index 6b4c9f6..0d093be 100644 (file)
@@ -1,39 +1,30 @@
-let https = require('https');
+let https = require('https')
 
 return async (env, order_id) => {
-  let config = await env.site.get_json('/_config/config.json');
-  let get_access_token = await _require('get_access_token.jst');
+  let config = await env.site.get_json('/_config/config.json')
+  let get_access_token = await _require('get_access_token.jst')
 
-  return new Promise(resolve => {
-    get_access_token(env).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
-      }
+  let access_token = await get_access_token(env)
+  return new Promise(
+    resolve => {
+      let request = https.request(
+        {
+          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
+          }
+        },
+        response => {
+          let str = ''
+          response.on('data', chunk => {str += chunk})
+          response.on('end', () => {resolve(JSON.parse(str))})
+        }
+      )
+      request.end()
     }
-    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();
-
-  });
-
-  });
-};
+  )
+}
index d1933ec..ee68ff3 100644 (file)
@@ -1,29 +1,27 @@
-let https = require('https');
+let https = require('https')
 
-return async (env, new_order_details) => {
-  let config = await env.site.get_json('/_config/config.json');
-  let get_access_token = await _require('get_access_token.jst');
+return async (env, order_id, patch_details) => {
+  let config = await env.site.get_json('/_config/config.json')
+  let get_access_token = await _require('get_access_token.jst')
 
-  return new Promise((resolve, reject) => {
-    get_access_token(env).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);
-      });
+  let access_token = await get_access_token(env)
+  return new Promise(
+    resolve => {
+      let request = https.request(
+        {
+          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
+          }
+        },
+        response => {resolve(response.statusCode)}
+      )
       request.write(JSON.stringify(patch_details));
-      request.end();
-  });
-});
-
-};
+      request.end()
+    }
+  )
+}
index 1e327ca..869b364 100644 (file)
@@ -22,8 +22,8 @@ return async env => {
     console.log('received patchOrder form:', JSON.stringify(query))
 
     let cookies = cookie.parse(env.request.headers.cookie || '')
-    let new_order_details = {
-      "patch_details": [{
+    let patch_details = [
+      {
         "op" : "replace",
         "path" : "/purchase_units/@reference_id==\'PU1\'/amount",
         "value" : {
@@ -56,11 +56,11 @@ return async env => {
             }
           }
         }       
-      }],
-      "order_id": cookies.order_id
-    }
+      }
+    ]
     response = {
-      http_code: await patch_order_details(env, new_order_details)
+      http_code:
+        await patch_order_details(env, cookies.order_id, patch_details)
     }
     console.log('patch_order_details()', response)
   }