Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
if (!subscriptionID) {
throw new Error(`Expected subscription id to be passed as first argument to revise subscription api`);
}
if (!subscriptionPayload) {
throw new Error(`Expected subscription payload to be passed`);
}
const headers : Object = {
'Authorization': `Bearer ${ accessToken }`,
'PayPal-Partner-Attribution-Id': partnerAttributionID
};
return request({
method: `post`,
url: `${ CREATE_SUBSCRIPTIONS_API_URL }/${ subscriptionID }/revise`,
headers,
json: subscriptionPayload
}).then(({ body, status }) : string => {
if (status !== 200) {
throw new Error(`Revise Subscription Api HTTP-${ status } response: error:\n\n${ JSON.stringify(body, null, 4) }`);
}
// for revision flow the same subscription id is returned
return subscriptionID;
});
}
export function callSmartAPI({ accessToken, url, method = 'get', json } : APIRequest) : ZalgoPromise {
const reqHeaders : { [string] : string } = {
[ HEADERS.REQUESTED_BY ]: SMART_PAYMENT_BUTTONS
};
if (accessToken) {
reqHeaders[HEADERS.ACCESS_TOKEN] = accessToken;
}
return request({ url, method, headers: reqHeaders, json })
.then(({ status, body, headers }) => {
if (body.ack === 'contingency') {
const err = new Error(body.contingency);
// $FlowFixMe
err.data = body.data;
throw err;
}
if (status > 400) {
throw new Error(`Api: ${ url } returned status code: ${ status } (Corr ID: ${ headers[HEADERS.PAYPAL_DEBUG_ID] })`);
}
if (body.ack !== 'success') {
throw new Error(`Api: ${ url } returned ack: ${ body.ack } (Corr ID: ${ headers[HEADERS.PAYPAL_DEBUG_ID] })`);
}
const paymentSource : PaymentSource = {
token: {
id: paymentMethodID,
type: 'NONCE'
}
};
if (enableThreeDomainSecure) {
paymentSource.contingencies = [ VALIDATE_CONTINGENCIES.THREE_DOMAIN_SECURE ];
}
const json = {
payment_source: paymentSource
};
return request({
method: `post`,
url: `${ ORDERS_API_URL }/${ orderID }/${ VALIDATE_PAYMENT_METHOD_API }`,
headers,
json
});
}
export const createAccessToken = memoize((clientID : string) : ZalgoPromise => {
getLogger().info(`rest_api_create_access_token`);
if (proxyRest.createAccessToken && !proxyRest.createAccessToken.source.closed) {
return proxyRest.createAccessToken(clientID);
}
const basicAuth : string = base64encode(`${ clientID }:`);
return request({
method: `post`,
url: getAuthAPIUrl(),
headers: {
Authorization: `Basic ${ basicAuth }`
},
data: {
grant_type: `client_credentials`
}
}).then(({ body }) => {
if (body && body.error === 'invalid_client') {
throw new Error(`Auth Api invalid client id: ${ clientID }:\n\n${ JSON.stringify(body, null, 4) }`);
}
function httpTransport(_ref) {
var url = _ref.url,
method = _ref.method,
headers = _ref.headers,
json = _ref.json;
return request({
url: url,
method: method,
headers: headers,
json: json
}).then(noop);
}
return inlineMemoize(createAccessToken, () => {
getLogger().info(`rest_api_create_access_token`);
const basicAuth = base64encode(`${ clientID }:`);
return request({
method: `post`,
url: AUTH_API_URL,
headers: {
Authorization: `Basic ${ basicAuth }`
},
data: {
grant_type: `client_credentials`
}
}).then(({ body }) => {
if (body && body.error === 'invalid_client') {
throw new Error(`Auth Api invalid client id: ${ clientID }:\n\n${ JSON.stringify(body, null, 4) }`);
}
return inlineMemoize(createAccessToken, () => {
getLogger().info(`rest_api_create_access_token`);
const basicAuth = base64encode(`${ clientID }:`);
return request({
method: `post`,
url: getAuthAPIUrl(),
headers: {
Authorization: `Basic ${ basicAuth }`
},
data: {
grant_type: `client_credentials`
}
}).then(({ body }) => {
if (body && body.error === 'invalid_client') {
throw new Error(`Auth Api invalid client id: ${ clientID }:\n\n${ JSON.stringify(body, null, 4) }`);
}
function httpTransport({ url, method, headers, json } : { url : string, method : string, headers : { [string] : string }, json : Object }) : ZalgoPromise {
return request({ url, method, headers, json }).then(noop);
}
getLogger().info(`rest_api_create_subscription_id`);
if (!accessToken) {
throw new Error(`Access token not passed`);
}
if (!subscriptionPayload) {
throw new Error(`Expected subscription payload to be passed`);
}
const headers : Object = {
'Authorization': `Bearer ${ accessToken }`,
'PayPal-Partner-Attribution-Id': partnerAttributionID
};
return request({
method: `post`,
url: CREATE_SUBSCRIPTIONS_API_URL,
headers,
json: subscriptionPayload
}).then(({ body }) : string => {
if (!body || !body.id) {
throw new Error(`Create Subscription Api response error:\n\n${ JSON.stringify(body, null, 4) }`);
}
return body.id;
});
}
export function callGraphQL({ query, variables = {}, headers = {} } : { query : string, variables? : { [string] : mixed }, headers? : { [string] : string } }) : ZalgoPromise {
return request({
url: GRAPHQL_URI,
method: 'POST',
json: {
query,
variables
},
headers: {
'x-app-name': SMART_PAYMENT_BUTTONS,
...headers
}
}).then(({ status, body }) => {
const errors = body.errors || [];
if (errors.length) {
const message = errors[0].message || JSON.stringify(errors[0]);
throw new Error(message);