mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Run eslint autofix
This commit is contained in:
parent
36a13f6500
commit
11105d913b
141 changed files with 14160 additions and 11825 deletions
|
@ -1,62 +1,71 @@
|
|||
import {loadScript} from "@paypal/paypal-js";
|
||||
import { loadScript } from '@paypal/paypal-js';
|
||||
|
||||
const storageKey = 'ppcp-data-client-id';
|
||||
|
||||
const validateToken = (token, user) => {
|
||||
if (! token) {
|
||||
return false;
|
||||
}
|
||||
if (token.user !== user) {
|
||||
return false;
|
||||
}
|
||||
const currentTime = new Date().getTime();
|
||||
const isExpired = currentTime >= token.expiration * 1000;
|
||||
return ! isExpired;
|
||||
}
|
||||
const validateToken = ( token, user ) => {
|
||||
if ( ! token ) {
|
||||
return false;
|
||||
}
|
||||
if ( token.user !== user ) {
|
||||
return false;
|
||||
}
|
||||
const currentTime = new Date().getTime();
|
||||
const isExpired = currentTime >= token.expiration * 1000;
|
||||
return ! isExpired;
|
||||
};
|
||||
|
||||
const storedTokenForUser = (user) => {
|
||||
const token = JSON.parse(sessionStorage.getItem(storageKey));
|
||||
if (validateToken(token, user)) {
|
||||
return token.token;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
const storedTokenForUser = ( user ) => {
|
||||
const token = JSON.parse( sessionStorage.getItem( storageKey ) );
|
||||
if ( validateToken( token, user ) ) {
|
||||
return token.token;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const storeToken = (token) => {
|
||||
sessionStorage.setItem(storageKey, JSON.stringify(token));
|
||||
}
|
||||
const storeToken = ( token ) => {
|
||||
sessionStorage.setItem( storageKey, JSON.stringify( token ) );
|
||||
};
|
||||
|
||||
const dataClientIdAttributeHandler = (scriptOptions, config, callback, errorCallback = null) => {
|
||||
fetch(config.endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({
|
||||
nonce: config.nonce
|
||||
})
|
||||
}).then((res)=>{
|
||||
return res.json();
|
||||
}).then((data)=>{
|
||||
const isValid = validateToken(data, config.user);
|
||||
if (!isValid) {
|
||||
return;
|
||||
}
|
||||
storeToken(data);
|
||||
const dataClientIdAttributeHandler = (
|
||||
scriptOptions,
|
||||
config,
|
||||
callback,
|
||||
errorCallback = null
|
||||
) => {
|
||||
fetch( config.endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify( {
|
||||
nonce: config.nonce,
|
||||
} ),
|
||||
} )
|
||||
.then( ( res ) => {
|
||||
return res.json();
|
||||
} )
|
||||
.then( ( data ) => {
|
||||
const isValid = validateToken( data, config.user );
|
||||
if ( ! isValid ) {
|
||||
return;
|
||||
}
|
||||
storeToken( data );
|
||||
|
||||
scriptOptions['data-client-token'] = data.token;
|
||||
scriptOptions[ 'data-client-token' ] = data.token;
|
||||
|
||||
loadScript(scriptOptions).then((paypal) => {
|
||||
if (typeof callback === 'function') {
|
||||
callback(paypal);
|
||||
}
|
||||
}).catch(err => {
|
||||
if (typeof errorCallback === 'function') {
|
||||
errorCallback(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
loadScript( scriptOptions )
|
||||
.then( ( paypal ) => {
|
||||
if ( typeof callback === 'function' ) {
|
||||
callback( paypal );
|
||||
}
|
||||
} )
|
||||
.catch( ( err ) => {
|
||||
if ( typeof errorCallback === 'function' ) {
|
||||
errorCallback( err );
|
||||
}
|
||||
} );
|
||||
} );
|
||||
};
|
||||
|
||||
export default dataClientIdAttributeHandler;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue