Skip to content
Permalink
Browse files

fix: remove all year-end gift implementation (#38030)

  • Loading branch information
raisedadead committed Jan 8, 2020
1 parent 86127a2 commit 059e826465b789d4c76bbd13525fd6b9a1859f03
@@ -14,7 +14,6 @@ const log = debug('fcc:boot:donate');

export default function donateBoot(app, done) {
let stripe = false;
const { User } = app.models;
const api = app.loopback.Router();
const donateRouter = app.loopback.Router();

@@ -170,6 +169,16 @@ export default function donateBoot(app, done) {
};

return Promise.resolve(user)
.then(nonDonatingUser => {
const { isDonating } = nonDonatingUser;
if (isDonating) {
throw {
message: `User already has active donation(s).`,
type: 'AlreadyDonatingError'
};
}
return nonDonatingUser;
})
.then(createCustomer)
.then(customer => {
return duration === 'onetime'
@@ -184,92 +193,10 @@ export default function donateBoot(app, done) {
})
.then(createAsyncUserDonation)
.catch(err => {
if (err.type === 'StripeCardError') {
return res.status(402).send({ error: err.message });
}
return res
.status(500)
.send({ error: 'Donation failed due to a server error.' });
});
}

function createStripeDonationYearEnd(req, res) {
const { user, body } = req;

const {
amount,
duration,
token: { email, id }
} = body;

if (amount < 1 || duration !== 'onetime' || !isEmail(email)) {
return res.status(500).send({
error: 'The donation form had invalid values for this submission.'
});
}

const fccUser = user
? Promise.resolve(user)
: new Promise((resolve, reject) =>
User.findOrCreate(
{ where: { email } },
{ email },
(err, instance, isNew) => {
log('is new user instance: ', isNew);
if (err) {
return reject(err);
}
return resolve(instance);
}
)
);

let donatingUser = {};
let donation = {
email,
amount,
duration,
provider: 'stripe',
startDate: new Date(Date.now()).toISOString()
};

const createCustomer = user => {
donatingUser = user;
return stripe.customers.create({
email,
card: id
});
};

const createOneTimeCharge = customer => {
donation.customerId = customer.id;
return stripe.charges.create({
amount: amount,
currency: 'usd',
customer: customer.id
});
};

const createAsyncUserDonation = () => {
donatingUser
.createDonation(donation)
.toPromise()
.catch(err => {
throw new Error(err);
});
};

return Promise.resolve(fccUser)
.then(createCustomer)
.then(customer => {
return createOneTimeCharge(customer).then(charge => {
donation.subscriptionId = 'one-time-charge-prefix-' + charge.id;
return res.send(charge);
});
})
.then(createAsyncUserDonation)
.catch(err => {
if (err.type === 'StripeCardError') {
if (
err.type === 'StripeCardError' ||
err.type === 'AlreadyDonatingError'
) {
return res.status(402).send({ error: err.message });
}
return res
@@ -333,12 +260,10 @@ export default function donateBoot(app, done) {
done();
} else {
api.post('/charge-stripe', createStripeDonation);
api.post('/charge-stripe-year-end', createStripeDonationYearEnd);
api.post('/create-hmac-hash', createHmacHash);
donateRouter.use('/donate', api);
app.use(donateRouter);
app.use('/internal', donateRouter);
app.use('/unauthenticated', donateRouter);
connectToStripe().then(done);
}
}
@@ -9,25 +9,16 @@ const propTypes = {
error: PropTypes.string,
processing: PropTypes.bool,
reset: PropTypes.func.isRequired,
success: PropTypes.bool,
yearEndGift: PropTypes.bool
success: PropTypes.bool
};

function DonateCompletion({
processing,
reset,
success,
error = null,
yearEndGift = false
}) {
function DonateCompletion({ processing, reset, success, error = null }) {
/* eslint-disable no-nested-ternary */
const style = processing ? 'info' : success ? 'success' : 'danger';
const heading = processing
? 'We are processing your donation.'
: success
? yearEndGift
? 'Thank you for your donation.'
: 'Thank you for being a supporter.'
? 'Thank you for being a supporter.'
: 'Something went wrong with your donation.';
return (
<Alert bsStyle={style} className='donation-completion'>
@@ -43,7 +34,7 @@ function DonateCompletion({
name='line-scale'
/>
)}
{success && !yearEndGift && (
{success && (
<div>
<p>
Your donations will support free technology education for people
@@ -55,11 +46,6 @@ function DonateCompletion({
</p>
</div>
)}
{success && yearEndGift && (
<div>
<p>You should receive a receipt in your email.</p>
</div>
)}
{error && <p>{error}</p>}
</div>
<div className='donation-completion-buttons'>
@@ -29,8 +29,7 @@ const propTypes = {
stripe: PropTypes.shape({
createToken: PropTypes.func.isRequired
}),
theme: PropTypes.string,
yearEndGift: PropTypes.bool
theme: PropTypes.string
};
const initialState = {
donationState: {
@@ -134,7 +133,6 @@ class DonateFormChildViewForHOC extends Component {

postDonation(token) {
const { donationAmount: amount, donationDuration: duration } = this.state;
const { yearEndGift } = this.props;
this.setState(state => ({
...state,
donationState: {
@@ -152,7 +150,7 @@ class DonateFormChildViewForHOC extends Component {
this.props.showCloseBtn();
}

return postChargeStripe(yearEndGift, {
return postChargeStripe({
token,
amount,
duration
@@ -275,14 +273,12 @@ class DonateFormChildViewForHOC extends Component {
const {
donationState: { processing, success, error }
} = this.state;
const { yearEndGift } = this.props;
if (processing || success || error) {
return this.renderCompletion({
processing,
success,
error,
reset: this.resetDonation,
yearEndGift
reset: this.resetDonation
});
}
return this.renderDonateForm();

0 comments on commit 059e826

Please sign in to comment.
You can’t perform that action at this time.