-
Notifications
You must be signed in to change notification settings - Fork 1
/
paypal.js
57 lines (52 loc) 路 1.72 KB
/
paypal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { BUSINESS_NAME, BUTTON_NAME, PAYPAL_DONATION_HEADING, BASE_URL } from 'constant';
import { setCookie, getCookie, eraseCookie } from 'shared/cookies';
import { navigateToUrl } from '../shared/utils/url';
let cookieId;
const COOKIE_NAME = 'donate-by-paypal';
const COOKIE_HOURS = 24;
const proceedToPaypal = (bag) => {
const params = new URLSearchParams({
amount: bag.amount,
currency_code: bag.currency.toString().toUpperCase(),
email: bag.email,
first_name: bag.firstName,
last_name: bag.lastName,
item_number: bag.giftItemLabel,
bn: BUTTON_NAME,
business: BUSINESS_NAME,
charset: 'utf-8',
cmd: '_donations',
item_name: PAYPAL_DONATION_HEADING,
no_note: 0,
no_shipping: 1,
// notify_url: "", // Paypal will notify this POST URL once the payment is completed
return: `${BASE_URL}/donate.html?${COOKIE_NAME}=${cookieId}`,
rm: 1, // return POST "2", GET "1"
});
navigateToUrl(`https://www.paypal.com/donate?${params.toString()}`, { inNewTab: false });
};
const saveDataToCookie = (bag) => {
const payload = {
donation: bag.donation,
donor: bag.donor,
giftItemLabel: bag.giftItemLabel,
email: bag.email,
wantsToComment: bag.wantsToComment,
comment: bag.comment,
};
cookieId = new Date().valueOf();
setCookie(`${COOKIE_NAME}-${cookieId}`, JSON.stringify(payload), COOKIE_HOURS);
};
const getCookieToData = (id) => {
const stringified = getCookie(`${COOKIE_NAME}-${id}`);
const data = JSON.parse(stringified);
return data;
};
export default {
proceedToPaypal,
saveDataToCookie,
getCookieToData,
doesCookieExists: (id) => !!getCookie(`${COOKIE_NAME}-${id}`),
eraseCookie: (id) => eraseCookie(`${COOKIE_NAME}-${id}`),
COOKIE_NAME,
};