Created
April 13, 2020 00:54
-
-
Save shanwixcode/09c3596293e80d98bafeb6189230a3f9 to your computer and use it in GitHub Desktop.
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
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