Last active
August 29, 2015 14:22
-
-
Save chernjie/94620b3cd715c100cef1 to your computer and use it in GitHub Desktop.
HackerspaceSG membership payment via PayPal Rest API
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
#!/usr/bin/env bash | |
## | |
# Update your configuration here | |
# You may get your `client_id` and `client_secret` from developer.paypal.com | |
# For membership level, please refer to http://hackerspace.sg/membership | |
## | |
endpoint=api.paypal.com | |
client_id=$client_id | |
client_secret=$client_secret | |
membership_level="128.00" | |
receiver=luther@hackerspace.sg | |
## | |
# Configurations ends here | |
# Do not edit beyond this line | |
################################ | |
require () { | |
for i | |
do | |
if ! command -v $i > /dev/null | |
then | |
echo command $i not found >&2 | |
exit 1 | |
fi | |
done | |
} | |
getAccessToken() { | |
test "token.json" == "$(find token.json -mtime -14400s)" || | |
curl -s -XPOST "https://$endpoint/v1/oauth2/token" \ | |
-u "$client_id:$client_secret" \ | |
-H "Accept: application/json" \ | |
-d "grant_type=client_credentials" \ | |
| json | tee token.json | |
} | |
makePayout() { | |
local timeid=$(date +%Y%m%d%H%M%S) | |
local month=${month:-${timeid:0:6}} | |
local email_subject="HSG Membership $month" | |
local access_token="$(json -f token.json access_token)" | |
curl -s -XPOST "https://$endpoint/v1/payments/payouts?sync_mode=true" \ | |
-H "Content-Type:application/json" \ | |
-H "Authorization: Bearer $access_token" \ | |
-d '{ | |
"sender_batch_header": { | |
"sender_batch_id": "'$timeid'", | |
"email_subject": "'"$email_subject"'", | |
"recipient_type": "EMAIL" | |
}, | |
"items": [ | |
{ | |
"recipient_type": "EMAIL", | |
"amount": { | |
"value": "'$membership_level'", | |
"currency": "SGD" | |
}, | |
"note": "PayPal payment via Rest API: '$month' '$membership_level' SGD", | |
"sender_item_id": "'$timeid'", | |
"receiver": "'$receiver'" | |
} | |
] | |
}' \ | |
| json | tee payout-$month.json | |
} | |
require json curl tee date | |
getAccessToken | |
makePayout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment