Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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