Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Last active June 28, 2020 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shanwixcode/712bb1ece2515884a2e6c5d30991fe22 to your computer and use it in GitHub Desktop.
Save shanwixcode/712bb1ece2515884a2e6c5d30991fe22 to your computer and use it in GitHub Desktop.
import {fetch} from 'wix-fetch';
export async function getAccess() {
const response = await fetch("https://api.sandbox.paypal.com/v1/oauth2/token", {
method: 'post',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + "" //ENTER ENCODED STRING
},
body: "grant_type=client_credentials"
});
const ret = await response.json();
return ret;
}
export async function order(token, item, amount) {
var orderObj = {
"intent": "CAPTURE",
"application_context": {
"return_url": "URL_HERE",
"cancel_url": "URL_HERE",
"landing_page": "BILLING",
"payment_method": {
"payee_preferred": "IMMEDIATE_PAYMENT_REQUIRED"
}
},
"purchase_units": [
{
"description": item,
"amount": {
"currency_code": "USD",
"value": Number(amount),
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": Number(amount)
}
}
},
"items": [{
"name": "Item Name - " + item,
"unit_amount": {
"currency_code": "USD",
"value": Number(amount)
},
"quantity": "1"
}]
}
]
};
const response = await fetch("https://api.sandbox.paypal.com/v2/checkout/orders", {
method: 'post',
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token
},
body: JSON.stringify(orderObj)
});
const ret = await response.json();
return ret;
}
export async function capturePayment(url) {
const response = await fetch(`${url}`, {
method: 'post',
headers: {
"Content-Type": "application/json",
"Authorization": "Basic " + "" //ENTER ENCODED STRING
}
});
const ret = await response.json();
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment