Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Created April 13, 2020 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanwixcode/09c3596293e80d98bafeb6189230a3f9 to your computer and use it in GitHub Desktop.
Save shanwixcode/09c3596293e80d98bafeb6189230a3f9 to your computer and use it in GitHub Desktop.
import {ok, created, serverError, response} from 'wix-http-functions';
import {capturePayment} from 'backend/payPal.jsw';
import wixData from 'wix-data';
export function post_payPal(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
return request.body.json()
.then( (body) => {
if(body.event_type === "CHECKOUT.ORDER.APPROVED") {
payUser(body);
} else {
console.log(body);
}
})
.then( () => {
options.body = {
"status": 200
};
return created(options);
})
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
});
}
function payUser(body) {
var array = body.resource.links;
var newArray = array.filter(function (el) {
return el.rel === "capture";
});
let url = newArray[0].href;
capturePayment(url)
.then( (res) => {
if(res.status) {
if(res.status === 'COMPLETED') {
let amount = body.resource.purchase_units[0].amount.value;
let name = body.resource.purchase_units[0].description;
let rid = body.resource.id;
let data = {
title: 'DONE',
name: name,
amount: Number(amount),
resourceId: rid
};
wixData.insert('payments', data);
} else {
//handle failure
}
} else {
//handle failure
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment