Created
October 1, 2015 14:15
-
-
Save caiotarifa/b3c46fde7850e38de1cd 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
function sendPayment() { | |
var sheet, last, prices, person; | |
function getColumnByLastRow(column) { | |
return sheet.getRange(last.row, column).getValue().toString(); | |
} | |
sheet = SpreadsheetApp.getActiveSheet(); | |
last = { | |
row: sheet.getLastRow(), | |
col: sheet.getLastColumn() | |
}; | |
prices = { | |
trip: 160.00, | |
shirt: 25.00 | |
} | |
person = { | |
name: getColumnByLastRow(2), | |
bornDate: getColumnByLastRow(3), | |
document: getColumnByLastRow(5), | |
email: getColumnByLastRow(6), | |
phone: getColumnByLastRow(7), | |
addressPostalCode: getColumnByLastRow(11), | |
addressStreet: getColumnByLastRow(12), | |
addressNumber: getColumnByLastRow(13), | |
addressComplement: getColumnByLastRow(14), | |
addressCity: getColumnByLastRow(15), | |
addressState: getColumnByLastRow(16), | |
shirts: getColumnByLastRow(22), | |
shirtsSize: getColumnByLastRow(23), | |
shirtsModel: getColumnByLastRow(24) | |
} | |
pagSeguroCheckout(sheet, last, person); | |
} | |
function pagSeguroCheckout(sheet, last, person) { | |
var url, range, payload, options, result; | |
url = 'https://ws.sandbox.pagseguro.uol.com.br/v2/checkout'; | |
payload = { | |
email: '', | |
token: '', | |
currency: 'BRL', | |
itemId1: '201600001', | |
itemDescription1: 'Caravana Teleport: Traslado Campus Party 2016', | |
itemAmount1: '160.00', | |
itemQuantity1: '1', | |
reference: '2016' + zeroPad(last.row, 3), | |
senderName: person.name, | |
senderBornDate: person.bornDate, | |
senderCPF: person.document, | |
senderEmail: person.email, | |
senderAreaCode: person.phone.substr(0, 2), | |
senderPhone: person.phone.substr(2), | |
shippingType: '3', | |
shippingAddressCountry: 'BRA', | |
shippingAddressState: person.addressState, | |
shippingAddressCity: person.addressCity, | |
shippingAddressPostalCode: person.addressPostalCode, | |
shippingAddressDistrict: '', | |
shippingAddressStreet: person.addressStreet, | |
shippingAddressNumber: person.addressNumber, | |
shippingAddressComplement: person.addressComplement, | |
redirectURL: 'https://sites.google.com/site/caravanateleport/' | |
} | |
if (parseInt(person.shirts) > 0) { | |
payload.itemId2 = '201600002'; | |
payload.itemDescription2 = 'Caravana Teleport: Camiseta Campus Party 2016'; | |
payload.itemAmount2 = '25.00'; | |
payload.itemQuantity2 = person.shirts; | |
} | |
options = { | |
method: 'POST', | |
payload: payload, | |
followRedirects: true, | |
muteHttpExceptions: true | |
} | |
result = UrlFetchApp.fetch(url, options); | |
if (result.getResponseCode() === 200) { | |
result = XmlService.parse(result.getContentText()); | |
result = result.getRootElement(); | |
result = result.getChild('code').getText(); | |
range = sheet.getRange(last.row, last.col + 1).getCell(1, 1); | |
range.setValue(result); | |
payload.token = result; | |
MailApp.sendEmail( | |
payload.senderEmail, | |
'Caravana Teleport: Pedido #' + payload.reference, | |
getMailerFromFile('welcome-mailer.txt', payload), | |
{ | |
name: 'Caravana Teleport', | |
replyTo: payload.email, | |
htmlBody: getMailerFromFile('welcome-mailer', payload) | |
} | |
); | |
return result; | |
} | |
return false; | |
} | |
function getMailerFromFile(file, data) { | |
file = HtmlService.createTemplateFromFile(file); | |
file.data = data; | |
return HtmlService.createHtmlOutput(file.evaluate()).getContent(); | |
} | |
function zeroPad(number, count) { | |
var response = parseInt(number) + ''; | |
while(response.length < count) { | |
response = '0' + response; | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment