Created
April 25, 2018 12:33
-
-
Save xslim/76a760969d25583e6fb689004f2a2e90 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/doqedi
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
Object.prototype.valueForKeyPath = function(keyPath) { | |
if (keyPath === null) return; | |
if (!keyPath.includes('.')) { return this[keyPath]; } | |
var chain = keyPath.split('.'); | |
var firstKey = chain.shift(); | |
var shiftedKeyPath = chain.join('.'); | |
return this[firstKey].valueForKeyPath(shiftedKeyPath); | |
} | |
function querystring_parse(query) { | |
return JSON.parse('{"' + query.replace(/&/g, '","') | |
.replace(/=/g,'":"') + '"}', function(key, value) { | |
return key==="" ? value : decodeURIComponent(value); | |
}) | |
} | |
function getCustomerReceiptText(txData) { | |
var receipts = txData.valueForKeyPath('SaleToPOIResponse.PaymentResponse.PaymentReceipt') | |
var type = 'CustomerReceipt'; | |
var consolidate = true; | |
var receipt = getReceipt(receipts, type); | |
var receiptText = formatReceipt(receipt, consolidate); | |
return receiptText; | |
} | |
function getReceipt(receipts, type) { | |
if (!receipts) return undefined; | |
for (var i = 0; i < receipts.length; i++) { | |
if (receipts[i].DocumentQualifier == type) { | |
return receipts[i]; | |
} | |
} | |
return undefined; | |
} | |
function formatReceiptLine(ls, rs, max = 40) { | |
if (!ls) ls = '' | |
if (!rs) rs = '' | |
var ls_l = ls.length; | |
var rs_l = rs.length; | |
var total = ls_l + rs_l + 1; | |
var pad = max - total; | |
return ls.padEnd(ls_l + pad) + rs + "\n" | |
} | |
function formatReceipt(receipt, consolidate = true) { | |
if (!receipt || !receipt.OutputContent || !receipt.OutputContent.OutputText) { | |
return undefined; | |
} | |
var data = receipt.OutputContent.OutputText; | |
var text = ''; | |
for (var i = 0; i < data.length; i++) { | |
var fields = querystring_parse(data[i].Text) | |
if (fields.key == 'filler' && !consolidate) { | |
text += "\n"; | |
} else if (fields.name && ! fields.value && !consolidate) { | |
text += fields.name + "\n"; | |
} else if (!fields.name && fields.value && !consolidate) { | |
text += " : " + fields.value + "\n"; | |
} else if (fields.name && fields.value) { | |
text += formatReceiptLine(fields.name, fields.value); | |
} | |
} | |
return text; | |
} | |
function formatReceiptText(data, type = 'CustomerReceipt', consolidate = true) { | |
var paymentResponse = data.valueForKeyPath('SaleToPOIResponse.PaymentResponse'); | |
if (paymentResponse) { | |
var receipts = paymentResponse.valueForKeyPath('PaymentReceipt') | |
var receipt = getReceipt(receipts, type); | |
return formatReceipt(receipt, consolidate); | |
} | |
return undefined; | |
} | |
function getApprovedTxData() { | |
return { SaleToPOIResponse: | |
{ PaymentResponse: | |
{ POIData: | |
{ POITransactionID: | |
{ | |
TimeStamp: '2018-04-15T14:57:01.000Z', | |
TransactionID: 'oLkO001523804221000.8815238042359567' | |
} | |
}, | |
SaleData: | |
{ SaleTransactionID: | |
{ TimeStamp: '2018-04-15T14:57:01.649Z', | |
TransactionID: 'TransactionID 27908' }, | |
SaleReferenceID: '39dadf9fc9eb5fc7462acc4e8611b9a3051ed77b' }, | |
PaymentReceipt: | |
[ { RequiredSignatureFlag: false, | |
DocumentQualifier: 'CashierReceipt', | |
OutputContent: | |
{ OutputFormat: 'Text', | |
OutputText: | |
[ { CharacterStyle: 'Bold', | |
Text: 'key=header1', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'key=header2', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=MERCHANT%20COPY&key=merchantTitle', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Date&value=15%2f04%2f18&key=txdate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Time&value=16%3a57%3a01&key=txtime', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Card&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a0037&key=pan', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Pref.%20name&key=preferredName', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Card%20type&value=mc&key=cardType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20method&value=mc&key=paymentMethod', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20variant&value=mc&key=paymentMethodVariant', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Entry%20mode&value=MSR&key=posEntryMode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=CVM%20res.&value=SIGNATURE%20VERIFIED&key=cvmRes', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=MID&value=1105&key=mid', EndOfLineFlag: true }, | |
{ Text: 'name=TID&value=V400m-324688163&key=tid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=PTID&value=12000000&key=ptid', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Auth.%20code&value=123456&key=authCode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=ATC&key=atc', EndOfLineFlag: true }, | |
{ Text: 'name=Buyer%20ID&key=buyerId', EndOfLineFlag: true }, | |
{ Text: 'name=Operation%20type&key=walletOperationType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Transaction%20ref.&key=walletTransactionReference', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20amount&key=walletDccAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20rate&key=walletDccRate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Tender&value=oLkO001523804221000&key=txRef', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Reference&value=TransactionID%2027908&key=mref', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Type&value=GOODS_SERVICES&key=txtype', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=TOTAL&value=%e2%82%ac%201.00%20%20&key=totalAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=APPROVED&key=approved', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true } ] } }, | |
{ RequiredSignatureFlag: false, | |
DocumentQualifier: 'CustomerReceipt', | |
OutputContent: | |
{ OutputFormat: 'Text', | |
OutputText: | |
[ { CharacterStyle: 'Bold', | |
Text: 'key=header1', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'key=header2', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=CARDHOLDER%20COPY&key=cardholderHeader', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Date&value=15%2f04%2f18&key=txdate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Time&value=16%3a57%3a01&key=txtime', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Card&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a0037&key=pan', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Pref.%20name&key=preferredName', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Card%20type&value=mc&key=cardType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20method&value=mc&key=paymentMethod', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20variant&value=mc&key=paymentMethodVariant', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Entry%20mode&value=MSR&key=posEntryMode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=CVM%20res.&value=SIGNATURE%20VERIFIED&key=cvmRes', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=MID&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a1105&key=mid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=TID&value=V400m-324688163&key=tid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=PTID&value=12000000&key=ptid', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Auth.%20code&value=123456&key=authCode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=ATC&key=atc', EndOfLineFlag: true }, | |
{ Text: 'name=Buyer%20ID&key=buyerId', EndOfLineFlag: true }, | |
{ Text: 'name=Operation%20type&key=walletOperationType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Transaction%20ref.&key=walletTransactionReference', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20amount&key=walletDccAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20rate&key=walletDccRate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Tender&value=oLkO001523804221000&key=txRef', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Reference&value=TransactionID%2027908&key=mref', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Type&value=GOODS_SERVICES&key=txtype', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=TOTAL&value=%e2%82%ac%201.00%20%20&key=totalAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=APPROVED&key=approved', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Please%20retain%20for%20your%20records&key=retain', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Thank%20you&key=thanks', EndOfLineFlag: true } ] } } ], | |
PaymentResult: | |
{ AuthenticationMethod: [ 'SignatureCapture' ], | |
PaymentAcquirerData: | |
{ AcquirerPOIID: 'V400m-324688163', | |
AcquirerTransactionID: | |
{ TimeStamp: '2018-04-15T14:57:01.000Z', | |
TransactionID: '8815238042359567' }, | |
MerchantID: 'PME_POS' }, | |
PaymentInstrumentData: | |
{ CardData: | |
{ EntryMode: [ 'MagStripe' ], | |
PaymentBrand: 'mc', | |
MaskedPan: '541333 **** 0037', | |
CardCountryCode: '528', | |
SensitiveCardData: { ExpiryDate: '1225' } }, | |
PaymentInstrumentType: 'Card' }, | |
AmountsResp: { AuthorizedAmount: 1, Currency: 'EUR' } }, | |
Response: | |
{ Result: 'Success', | |
AdditionalResponse: 'tid=12000000&transactionType=GOODS_SERVICES&pspReference=8815238042359567&cardHolderName=ADYENMSR&offline=false&shopperReference=joopsloop&mid=1105&cardHolderVerificationMethodResults=1E0300' } }, | |
MessageHeader: | |
{ ProtocolVersion: '3.0', | |
SaleID: 'AdyenSeller', | |
MessageClass: 'Service', | |
MessageCategory: 'Payment', | |
ServiceID: '036fc1b21b', | |
POIID: 'V400m-324688163', | |
MessageType: 'Response' } } }; | |
} | |
var txData = getApprovedTxData(); | |
var receiptText = getCustomerReceiptText(txData); | |
console.log(receiptText); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
Object.prototype.valueForKeyPath = function(keyPath) { | |
if (keyPath === null) return; | |
if (!keyPath.includes('.')) { return this[keyPath]; } | |
var chain = keyPath.split('.'); | |
var firstKey = chain.shift(); | |
var shiftedKeyPath = chain.join('.'); | |
return this[firstKey].valueForKeyPath(shiftedKeyPath); | |
} | |
function querystring_parse(query) { | |
return JSON.parse('{"' + query.replace(/&/g, '","') | |
.replace(/=/g,'":"') + '"}', function(key, value) { | |
return key==="" ? value : decodeURIComponent(value); | |
}) | |
} | |
function getCustomerReceiptText(txData) { | |
var receipts = txData.valueForKeyPath('SaleToPOIResponse.PaymentResponse.PaymentReceipt') | |
var type = 'CustomerReceipt'; | |
var consolidate = true; | |
var receipt = getReceipt(receipts, type); | |
var receiptText = formatReceipt(receipt, consolidate); | |
return receiptText; | |
} | |
function getReceipt(receipts, type) { | |
if (!receipts) return undefined; | |
for (var i = 0; i < receipts.length; i++) { | |
if (receipts[i].DocumentQualifier == type) { | |
return receipts[i]; | |
} | |
} | |
return undefined; | |
} | |
function formatReceiptLine(ls, rs, max = 40) { | |
if (!ls) ls = '' | |
if (!rs) rs = '' | |
var ls_l = ls.length; | |
var rs_l = rs.length; | |
var total = ls_l + rs_l + 1; | |
var pad = max - total; | |
return ls.padEnd(ls_l + pad) + rs + "\n" | |
} | |
function formatReceipt(receipt, consolidate = true) { | |
if (!receipt || !receipt.OutputContent || !receipt.OutputContent.OutputText) { | |
return undefined; | |
} | |
var data = receipt.OutputContent.OutputText; | |
var text = ''; | |
for (var i = 0; i < data.length; i++) { | |
var fields = querystring_parse(data[i].Text) | |
if (fields.key == 'filler' && !consolidate) { | |
text += "\n"; | |
} else if (fields.name && ! fields.value && !consolidate) { | |
text += fields.name + "\n"; | |
} else if (!fields.name && fields.value && !consolidate) { | |
text += " : " + fields.value + "\n"; | |
} else if (fields.name && fields.value) { | |
text += formatReceiptLine(fields.name, fields.value); | |
} | |
} | |
return text; | |
} | |
function formatReceiptText(data, type = 'CustomerReceipt', consolidate = true) { | |
var paymentResponse = data.valueForKeyPath('SaleToPOIResponse.PaymentResponse'); | |
if (paymentResponse) { | |
var receipts = paymentResponse.valueForKeyPath('PaymentReceipt') | |
var receipt = getReceipt(receipts, type); | |
return formatReceipt(receipt, consolidate); | |
} | |
return undefined; | |
} | |
function getApprovedTxData() { | |
return { SaleToPOIResponse: | |
{ PaymentResponse: | |
{ POIData: | |
{ POITransactionID: | |
{ | |
TimeStamp: '2018-04-15T14:57:01.000Z', | |
TransactionID: 'oLkO001523804221000.8815238042359567' | |
} | |
}, | |
SaleData: | |
{ SaleTransactionID: | |
{ TimeStamp: '2018-04-15T14:57:01.649Z', | |
TransactionID: 'TransactionID 27908' }, | |
SaleReferenceID: '39dadf9fc9eb5fc7462acc4e8611b9a3051ed77b' }, | |
PaymentReceipt: | |
[ { RequiredSignatureFlag: false, | |
DocumentQualifier: 'CashierReceipt', | |
OutputContent: | |
{ OutputFormat: 'Text', | |
OutputText: | |
[ { CharacterStyle: 'Bold', | |
Text: 'key=header1', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'key=header2', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=MERCHANT%20COPY&key=merchantTitle', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Date&value=15%2f04%2f18&key=txdate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Time&value=16%3a57%3a01&key=txtime', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Card&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a0037&key=pan', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Pref.%20name&key=preferredName', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Card%20type&value=mc&key=cardType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20method&value=mc&key=paymentMethod', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20variant&value=mc&key=paymentMethodVariant', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Entry%20mode&value=MSR&key=posEntryMode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=CVM%20res.&value=SIGNATURE%20VERIFIED&key=cvmRes', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=MID&value=1105&key=mid', EndOfLineFlag: true }, | |
{ Text: 'name=TID&value=V400m-324688163&key=tid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=PTID&value=12000000&key=ptid', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Auth.%20code&value=123456&key=authCode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=ATC&key=atc', EndOfLineFlag: true }, | |
{ Text: 'name=Buyer%20ID&key=buyerId', EndOfLineFlag: true }, | |
{ Text: 'name=Operation%20type&key=walletOperationType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Transaction%20ref.&key=walletTransactionReference', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20amount&key=walletDccAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20rate&key=walletDccRate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Tender&value=oLkO001523804221000&key=txRef', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Reference&value=TransactionID%2027908&key=mref', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Type&value=GOODS_SERVICES&key=txtype', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=TOTAL&value=%e2%82%ac%201.00%20%20&key=totalAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=APPROVED&key=approved', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true } ] } }, | |
{ RequiredSignatureFlag: false, | |
DocumentQualifier: 'CustomerReceipt', | |
OutputContent: | |
{ OutputFormat: 'Text', | |
OutputText: | |
[ { CharacterStyle: 'Bold', | |
Text: 'key=header1', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'key=header2', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=CARDHOLDER%20COPY&key=cardholderHeader', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Date&value=15%2f04%2f18&key=txdate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Time&value=16%3a57%3a01&key=txtime', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Card&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a0037&key=pan', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Pref.%20name&key=preferredName', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Card%20type&value=mc&key=cardType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20method&value=mc&key=paymentMethod', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20variant&value=mc&key=paymentMethodVariant', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Entry%20mode&value=MSR&key=posEntryMode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=CVM%20res.&value=SIGNATURE%20VERIFIED&key=cvmRes', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=MID&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a1105&key=mid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=TID&value=V400m-324688163&key=tid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=PTID&value=12000000&key=ptid', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Auth.%20code&value=123456&key=authCode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=ATC&key=atc', EndOfLineFlag: true }, | |
{ Text: 'name=Buyer%20ID&key=buyerId', EndOfLineFlag: true }, | |
{ Text: 'name=Operation%20type&key=walletOperationType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Transaction%20ref.&key=walletTransactionReference', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20amount&key=walletDccAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20rate&key=walletDccRate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Tender&value=oLkO001523804221000&key=txRef', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Reference&value=TransactionID%2027908&key=mref', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Type&value=GOODS_SERVICES&key=txtype', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=TOTAL&value=%e2%82%ac%201.00%20%20&key=totalAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=APPROVED&key=approved', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Please%20retain%20for%20your%20records&key=retain', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Thank%20you&key=thanks', EndOfLineFlag: true } ] } } ], | |
PaymentResult: | |
{ AuthenticationMethod: [ 'SignatureCapture' ], | |
PaymentAcquirerData: | |
{ AcquirerPOIID: 'V400m-324688163', | |
AcquirerTransactionID: | |
{ TimeStamp: '2018-04-15T14:57:01.000Z', | |
TransactionID: '8815238042359567' }, | |
MerchantID: 'PME_POS' }, | |
PaymentInstrumentData: | |
{ CardData: | |
{ EntryMode: [ 'MagStripe' ], | |
PaymentBrand: 'mc', | |
MaskedPan: '541333 **** 0037', | |
CardCountryCode: '528', | |
SensitiveCardData: { ExpiryDate: '1225' } }, | |
PaymentInstrumentType: 'Card' }, | |
AmountsResp: { AuthorizedAmount: 1, Currency: 'EUR' } }, | |
Response: | |
{ Result: 'Success', | |
AdditionalResponse: 'tid=12000000&transactionType=GOODS_SERVICES&pspReference=8815238042359567&cardHolderName=ADYENMSR&offline=false&shopperReference=joopsloop&mid=1105&cardHolderVerificationMethodResults=1E0300' } }, | |
MessageHeader: | |
{ ProtocolVersion: '3.0', | |
SaleID: 'AdyenSeller', | |
MessageClass: 'Service', | |
MessageCategory: 'Payment', | |
ServiceID: '036fc1b21b', | |
POIID: 'V400m-324688163', | |
MessageType: 'Response' } } }; | |
} | |
var txData = getApprovedTxData(); | |
var receiptText = getCustomerReceiptText(txData); | |
console.log(receiptText); | |
</script></body> | |
</html> |
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
Object.prototype.valueForKeyPath = function(keyPath) { | |
if (keyPath === null) return; | |
if (!keyPath.includes('.')) { return this[keyPath]; } | |
var chain = keyPath.split('.'); | |
var firstKey = chain.shift(); | |
var shiftedKeyPath = chain.join('.'); | |
return this[firstKey].valueForKeyPath(shiftedKeyPath); | |
} | |
function querystring_parse(query) { | |
return JSON.parse('{"' + query.replace(/&/g, '","') | |
.replace(/=/g,'":"') + '"}', function(key, value) { | |
return key==="" ? value : decodeURIComponent(value); | |
}) | |
} | |
function getCustomerReceiptText(txData) { | |
var receipts = txData.valueForKeyPath('SaleToPOIResponse.PaymentResponse.PaymentReceipt') | |
var type = 'CustomerReceipt'; | |
var consolidate = true; | |
var receipt = getReceipt(receipts, type); | |
var receiptText = formatReceipt(receipt, consolidate); | |
return receiptText; | |
} | |
function getReceipt(receipts, type) { | |
if (!receipts) return undefined; | |
for (var i = 0; i < receipts.length; i++) { | |
if (receipts[i].DocumentQualifier == type) { | |
return receipts[i]; | |
} | |
} | |
return undefined; | |
} | |
function formatReceiptLine(ls, rs, max = 40) { | |
if (!ls) ls = '' | |
if (!rs) rs = '' | |
var ls_l = ls.length; | |
var rs_l = rs.length; | |
var total = ls_l + rs_l + 1; | |
var pad = max - total; | |
return ls.padEnd(ls_l + pad) + rs + "\n" | |
} | |
function formatReceipt(receipt, consolidate = true) { | |
if (!receipt || !receipt.OutputContent || !receipt.OutputContent.OutputText) { | |
return undefined; | |
} | |
var data = receipt.OutputContent.OutputText; | |
var text = ''; | |
for (var i = 0; i < data.length; i++) { | |
var fields = querystring_parse(data[i].Text) | |
if (fields.key == 'filler' && !consolidate) { | |
text += "\n"; | |
} else if (fields.name && ! fields.value && !consolidate) { | |
text += fields.name + "\n"; | |
} else if (!fields.name && fields.value && !consolidate) { | |
text += " : " + fields.value + "\n"; | |
} else if (fields.name && fields.value) { | |
text += formatReceiptLine(fields.name, fields.value); | |
} | |
} | |
return text; | |
} | |
function formatReceiptText(data, type = 'CustomerReceipt', consolidate = true) { | |
var paymentResponse = data.valueForKeyPath('SaleToPOIResponse.PaymentResponse'); | |
if (paymentResponse) { | |
var receipts = paymentResponse.valueForKeyPath('PaymentReceipt') | |
var receipt = getReceipt(receipts, type); | |
return formatReceipt(receipt, consolidate); | |
} | |
return undefined; | |
} | |
function getApprovedTxData() { | |
return { SaleToPOIResponse: | |
{ PaymentResponse: | |
{ POIData: | |
{ POITransactionID: | |
{ | |
TimeStamp: '2018-04-15T14:57:01.000Z', | |
TransactionID: 'oLkO001523804221000.8815238042359567' | |
} | |
}, | |
SaleData: | |
{ SaleTransactionID: | |
{ TimeStamp: '2018-04-15T14:57:01.649Z', | |
TransactionID: 'TransactionID 27908' }, | |
SaleReferenceID: '39dadf9fc9eb5fc7462acc4e8611b9a3051ed77b' }, | |
PaymentReceipt: | |
[ { RequiredSignatureFlag: false, | |
DocumentQualifier: 'CashierReceipt', | |
OutputContent: | |
{ OutputFormat: 'Text', | |
OutputText: | |
[ { CharacterStyle: 'Bold', | |
Text: 'key=header1', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'key=header2', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=MERCHANT%20COPY&key=merchantTitle', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Date&value=15%2f04%2f18&key=txdate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Time&value=16%3a57%3a01&key=txtime', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Card&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a0037&key=pan', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Pref.%20name&key=preferredName', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Card%20type&value=mc&key=cardType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20method&value=mc&key=paymentMethod', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20variant&value=mc&key=paymentMethodVariant', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Entry%20mode&value=MSR&key=posEntryMode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=CVM%20res.&value=SIGNATURE%20VERIFIED&key=cvmRes', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=MID&value=1105&key=mid', EndOfLineFlag: true }, | |
{ Text: 'name=TID&value=V400m-324688163&key=tid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=PTID&value=12000000&key=ptid', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Auth.%20code&value=123456&key=authCode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=ATC&key=atc', EndOfLineFlag: true }, | |
{ Text: 'name=Buyer%20ID&key=buyerId', EndOfLineFlag: true }, | |
{ Text: 'name=Operation%20type&key=walletOperationType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Transaction%20ref.&key=walletTransactionReference', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20amount&key=walletDccAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20rate&key=walletDccRate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Tender&value=oLkO001523804221000&key=txRef', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Reference&value=TransactionID%2027908&key=mref', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Type&value=GOODS_SERVICES&key=txtype', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=TOTAL&value=%e2%82%ac%201.00%20%20&key=totalAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=APPROVED&key=approved', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true } ] } }, | |
{ RequiredSignatureFlag: false, | |
DocumentQualifier: 'CustomerReceipt', | |
OutputContent: | |
{ OutputFormat: 'Text', | |
OutputText: | |
[ { CharacterStyle: 'Bold', | |
Text: 'key=header1', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'key=header2', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=CARDHOLDER%20COPY&key=cardholderHeader', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Date&value=15%2f04%2f18&key=txdate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Time&value=16%3a57%3a01&key=txtime', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Card&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a%2a0037&key=pan', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Pref.%20name&key=preferredName', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Card%20type&value=mc&key=cardType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20method&value=mc&key=paymentMethod', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Payment%20variant&value=mc&key=paymentMethodVariant', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Entry%20mode&value=MSR&key=posEntryMode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=CVM%20res.&value=SIGNATURE%20VERIFIED&key=cvmRes', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=MID&value=%2a%2a%2a%2a%2a%2a%2a%2a%2a1105&key=mid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=TID&value=V400m-324688163&key=tid', | |
EndOfLineFlag: true }, | |
{ Text: 'name=PTID&value=12000000&key=ptid', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Auth.%20code&value=123456&key=authCode', | |
EndOfLineFlag: true }, | |
{ Text: 'name=ATC&key=atc', EndOfLineFlag: true }, | |
{ Text: 'name=Buyer%20ID&key=buyerId', EndOfLineFlag: true }, | |
{ Text: 'name=Operation%20type&key=walletOperationType', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Transaction%20ref.&key=walletTransactionReference', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20amount&key=walletDccAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'name=DCC%20rate&key=walletDccRate', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Tender&value=oLkO001523804221000&key=txRef', | |
EndOfLineFlag: true }, | |
{ Text: 'name=Reference&value=TransactionID%2027908&key=mref', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Type&value=GOODS_SERVICES&key=txtype', | |
EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=TOTAL&value=%e2%82%ac%201.00%20%20&key=totalAmount', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ CharacterStyle: 'Bold', | |
Text: 'name=APPROVED&key=approved', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Please%20retain%20for%20your%20records&key=retain', | |
EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'key=filler', EndOfLineFlag: true }, | |
{ Text: 'name=Thank%20you&key=thanks', EndOfLineFlag: true } ] } } ], | |
PaymentResult: | |
{ AuthenticationMethod: [ 'SignatureCapture' ], | |
PaymentAcquirerData: | |
{ AcquirerPOIID: 'V400m-324688163', | |
AcquirerTransactionID: | |
{ TimeStamp: '2018-04-15T14:57:01.000Z', | |
TransactionID: '8815238042359567' }, | |
MerchantID: 'PME_POS' }, | |
PaymentInstrumentData: | |
{ CardData: | |
{ EntryMode: [ 'MagStripe' ], | |
PaymentBrand: 'mc', | |
MaskedPan: '541333 **** 0037', | |
CardCountryCode: '528', | |
SensitiveCardData: { ExpiryDate: '1225' } }, | |
PaymentInstrumentType: 'Card' }, | |
AmountsResp: { AuthorizedAmount: 1, Currency: 'EUR' } }, | |
Response: | |
{ Result: 'Success', | |
AdditionalResponse: 'tid=12000000&transactionType=GOODS_SERVICES&pspReference=8815238042359567&cardHolderName=ADYENMSR&offline=false&shopperReference=joopsloop&mid=1105&cardHolderVerificationMethodResults=1E0300' } }, | |
MessageHeader: | |
{ ProtocolVersion: '3.0', | |
SaleID: 'AdyenSeller', | |
MessageClass: 'Service', | |
MessageCategory: 'Payment', | |
ServiceID: '036fc1b21b', | |
POIID: 'V400m-324688163', | |
MessageType: 'Response' } } }; | |
} | |
var txData = getApprovedTxData(); | |
var receiptText = getCustomerReceiptText(txData); | |
console.log(receiptText); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment