Add gravity flow demo
This commit is contained in:
47
woocomerce-order/src/helpers/GravityAPI.js
Normal file
47
woocomerce-order/src/helpers/GravityAPI.js
Normal file
@@ -0,0 +1,47 @@
|
||||
//eslint disable
|
||||
import CryptoJS from 'crypto-js';
|
||||
import fetch from "cross-fetch";
|
||||
|
||||
const apiKey = '0102c9cf3b';
|
||||
const privateKey = '1524cdc26dceaee';
|
||||
|
||||
function convertToFutureUnixTime(expirationInSeconds) {
|
||||
const currentDate = new Date();
|
||||
const unixTimeInSeconds = parseInt(currentDate.getTime() / 1000, 10);
|
||||
|
||||
return unixTimeInSeconds + expirationInSeconds;
|
||||
}
|
||||
|
||||
export function getFullPath(method, route) {
|
||||
const futureUnixTime = convertToFutureUnixTime(60);
|
||||
|
||||
const stringToSign = `${apiKey}:${method}:${route}:${futureUnixTime}`;
|
||||
|
||||
const hash = CryptoJS.HmacSHA1(stringToSign, privateKey);
|
||||
const base64 = hash.toString(CryptoJS.enc.Base64);
|
||||
|
||||
const signature = encodeURIComponent(base64);
|
||||
const fullPath = `http://localhost/gravityformsapi/${route}?api_key=${apiKey}&signature=${signature}&expires=${futureUnixTime}`;
|
||||
console.log(fullPath);
|
||||
return fullPath;
|
||||
|
||||
}
|
||||
|
||||
export function execute(method, path, callback) {
|
||||
return fetch(getFullPath(method, path), {
|
||||
method: method,
|
||||
}).then(response => {
|
||||
switch (response.status) {
|
||||
case 401:
|
||||
console.log(response.code);
|
||||
break;
|
||||
case 200:
|
||||
response.json().then(r => {
|
||||
//callback()
|
||||
callback(r.response);
|
||||
});
|
||||
default:
|
||||
console.log(response.code)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user