Created
January 16, 2017 16:31
-
-
Save EFF/e90c67bc0796d824da64c199ffb71d7d to your computer and use it in GitHub Desktop.
calls to dexero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import fetch from 'node-fetch'; | |
import DexeroHttpError from '../../error'; | |
export default class DonationService { | |
getBase64EncodedDexeroCredentials() { | |
this.clientId = process.env.DEXERO_CLIENT_ID; | |
this.clientSecret = process.env.DEXERO_CLIENT_SECRET; | |
return new Buffer(`${this.clientId}:${this.clientSecret}`).toString('base64'); | |
} | |
formatDonations(donations) { | |
let formattedDonations = []; | |
for (let donation of donations) { | |
if (donation.status != 'cancelled') { | |
let anonymous = !!donation.order_form[1].value; | |
formattedDonations.push({ | |
first_name: anonymous ? 'anonyme' : donation.customer.first_name, | |
last_name: anonymous ? '' : donation.customer.last_name, | |
email: anonymous ? '' : donation.customer.email, | |
amount: donation.total_order, | |
anonymous: anonymous | |
}); | |
} | |
} | |
return formattedDonations; | |
} | |
getDexeroAuthData() { | |
return fetch(`${process.env.DEXERO_OAUTH2_URL}?scope=read&grant_type=client_credentials`, { | |
headers: {'Authorization': `Basic ${this.getBase64EncodedDexeroCredentials()}`} | |
}) | |
.then((response) => { | |
if (response.ok) { | |
return response.json(); | |
} else { | |
throw new DexeroHttpError('there was an error while authentication tokens from dexero', response.status); | |
} | |
}) | |
.then((data) => { | |
this.dexeroAuthData = data; | |
this.dexeroAuthData.tokenExpiration = new Date(Date.now() + data.expires_in); | |
}); | |
} | |
fetchDonations() { | |
return fetch(`${process.env.DEXERO_ORDERS_ENDPOINT_URL}?status=treated&status=completed&include_forms=true&count=50000`, { | |
headers: {'Authorization': `${this.dexeroAuthData.token_type} ${this.dexeroAuthData.access_token}`} | |
}) | |
.then((response) => { | |
if (response.ok) { | |
return response.json(); | |
} else { | |
throw new DexeroHttpError('there was an error while getting orders from dexero', response.status); | |
} | |
}) | |
.then(this.handleFetchDonationResponseData) | |
} | |
fetchSearchDonation(email) { | |
return fetch(`${process.env.DEXERO_ORDERS_ENDPOINT_URL}?status=treated&status=completed&customer_email=${email}&include_forms=true`, { | |
headers: {'Authorization': `${this.dexeroAuthData.token_type} ${this.dexeroAuthData.access_token}`} | |
}) | |
.then((response) => { | |
if (response.ok) { | |
return response.json(); | |
} else { | |
throw new DexeroHttpError('there was an error while getting orders from dexero', response.status); | |
} | |
}) | |
.then(this.handleFetchDonationResponseData) | |
} | |
handleFetchDonationResponseData(data) { | |
if (data.orders.totalResults === 0) { | |
return []; | |
} else if (data.orders.totalResults === 1) { | |
// doesn't return an array when there is one order WHY ? | |
return this.formatDonations([data.orders.order]); | |
} | |
return this.formatDonations(data.orders.order); | |
} | |
getAll() { | |
if (!this.dexeroAuthData || this.dexeroAuthData.tokenExpiration >= Date.now()) { | |
return this.getDexeroAuthData() | |
.then(this.fetchDonations.bind(this)); | |
} else { | |
return this.fetchDonations(); | |
} | |
} | |
search(email) { | |
if (!this.dexeroAuthData || this.dexeroAuthData.tokenExpiration >= Date.now()) { | |
return this.getDexeroAuthData() | |
.then(this.fetchSearchDonation.bind(this, email)); | |
} else { | |
return this.fetchSearchDonation(email); | |
} | |
} | |
getTotalDonationAmount() { | |
return fetch('https://ecommerce.dexero.com/service/rest/2/ulaval/fulfsg/donations/product/43860/raised.json') | |
.then((response) => { | |
if (response.ok) { | |
return response.json(); | |
} else { | |
throw new DexeroHttpError('there was an error while getting total from dexero', response.status); | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment