Preserve formatting of ColumnCalculations while downloading pdf from tabulator - grouping

Issue: While downloading through tabulator all the column formatting is removed. I am aware of using accessors to reformat the data before downloading. This way I can define formatting for individual columns. However, for column calculations, I am unable to define formatting for calculation results at the time of downloading in pdf format. In addition, I want row groups headers to have proper indentation as its available in tabulator.
Implemented code:
var tabledata = [
{
"Department": "Dept1",
"Promo Name": "$2 Off",
"Menu": "BURGER",
"Check #": "111",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "12.50",
"Discount": "2.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "$2 Off",
"Menu": "soda",
"Check #": "1112",
"Settled By": "emp2",
"Discount By": "emp2",
"Price": "11.95",
"Discount": "2.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "10% Off",
"Menu": "BURGER",
"Check #": "112",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "12.50",
"Discount": "0.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "10% Off",
"Menu": "Water",
"Check #": "1122",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "1.85",
"Discount": "0.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "222",
"Menu": "menu2",
"Check #": "1134",
"Settled By": "emp3",
"Discount By": "emp3",
"Price": "10.25",
"Discount": "2.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "pizza",
"Check #": "1156",
"Settled By": "emp3",
"Discount By": "emp3",
"Price": "12.95",
"Discount": "6.48",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "BURGER",
"Check #": "11562",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "12.50",
"Discount": "3.13",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "sandwich",
"Check #": "157",
"Settled By": "emp2",
"Discount By": "emp2",
"Price": "56.25",
"Discount": "28.13",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "coke",
"Check #": "27818",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "3.00",
"Discount": "1.50",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "juice",
"Check #": "13346",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "3.00",
"Discount": "1.50",
"Count of PromoID": "1"
}
];
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
})
var colMoneyFormatter = function (value, data, type, params, column) {
return formatter.format(value);
}
//create Tabulator on DOM element with id "example-table"
var table = new Tabulator("#example-table",
{
//height: 205, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
data: tabledata //assign data to table
,
layout: "fitData" //fit columns to width of table (optional)
,
groupClosedShowCalcs: [true, true],
columnCalcs: "both",
groupBy: ["Department", "Promo Name"],
groupStartOpen: [true, true],
groupHeader: function(value, count, data, group) {
return value + "<span style='color:#d00; margin-left:10px;'>(" + count + " item)</span>";
},
groupToggleElement: ["arrow", "arrow"],
columns: [
{ title: "Department", field: "Department", formatter: "plaintext" },
{ title: "Promo Name", field: "Promo Name", formatter: "plaintext" },
{ title: "Description", field: "Menu", formatter: "plaintext" }, {
title: "Price",
field: "Price",
accessorDownload: colMoneyFormatter,
formatter: "money",
bottomCalc: "sum",
bottomCalcParams: { precision: 2 },
bottomCalcFormatter: "money",
bottomCalcFormatterParams: {
decimal: ".",
thousand: ",",
symbol: "$"
},
formatterParams: {
decimal: ".",
thousand: ",",
symbol: "$"
}
}, {
title: "Discount",
field: "Discount",
accessorDownload: colMoneyFormatter,
formatter: "money",
bottomCalc: "sum",
bottomCalcParams: { precision: 2 },
bottomCalcFormatter: "money",
bottomCalcFormatterParams: {
decimal: ".",
thousand: ",",
symbol: "$"
},
formatterParams: {
decimal: ".",
thousand: ",",
symbol: "$"
}
}, { title: "Check #", field: "Check #", formatter: "plaintext" },
{ title: "Settled By", field: "Settled By", formatter: "plaintext" },
{ title: "Discount By", field: "Discount By", formatter: "plaintext" }
]
});
//trigger download of data.pdf file
$("#download-pdf").click(function () {
table.download("pdf",
"data.pdf",
{
orientation: "landscape" //set page orientation to portrait
,
title: "Detailed Promo Report" //add title to report
,
autoTable: {
margin: {
top: 60
}
}
});
});
I want my pdf to look exactly like tabulator table with some additional rows, headers, and footers.
Any help will be greatly appreciated.

<!DOCTYPE html>
<html lang="en">
<head>
<link href="https://unpkg.com/tabulator-tables#4.2.7/dist/css/tabulator.min.css" rel="stylesheet">
<script src="https://unpkg.com/tabulator-tables#4.2.7/dist/js/tabulator.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.0.5/jspdf.plugin.autotable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<button id="download-pdf">Download PDF</button>
<div id="example-table"></div>
</body>
</html>
<script>
var tabledata = [{
"Department": "Dept1",
"Promo Name": "$2 Off",
"Menu": "BURGER",
"Check #": "111",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "12.50",
"Discount": "2.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "$2 Off",
"Menu": "soda",
"Check #": "1112",
"Settled By": "emp2",
"Discount By": "emp2",
"Price": "11.95",
"Discount": "2.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "10% Off",
"Menu": "BURGER",
"Check #": "112",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "12.50",
"Discount": "0.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "10% Off",
"Menu": "Water",
"Check #": "1122",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "1.85",
"Discount": "0.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "222",
"Menu": "menu2",
"Check #": "1134",
"Settled By": "emp3",
"Discount By": "emp3",
"Price": "10.25",
"Discount": "2.00",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "pizza",
"Check #": "1156",
"Settled By": "emp3",
"Discount By": "emp3",
"Price": "12.95",
"Discount": "6.48",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "BURGER",
"Check #": "11562",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "12.50",
"Discount": "3.13",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "sandwich",
"Check #": "157",
"Settled By": "emp2",
"Discount By": "emp2",
"Price": "56.25",
"Discount": "28.13",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "coke",
"Check #": "27818",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "3.00",
"Discount": "1.50",
"Count of PromoID": "1"
},
{
"Department": "Dept1",
"Promo Name": "meal",
"Menu": "juice",
"Check #": "13346",
"Settled By": "aaaaa",
"Discount By": "aaaaa",
"Price": "3.00",
"Discount": "1.50",
"Count of PromoID": "1"
}
];
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
})
var colMoneyFormatter = function(value, data, type, params, column) {
return formatter.format(value);
}
const table = new Tabulator("#example-table", {
data: tabledata,
layout: "fitData",
groupClosedShowCalcs: [true, true],
columnCalcs: "both",
groupBy: ["Department", "Promo Name"],
groupStartOpen: [true, true],
groupHeader: function(value, count, data, group) {
return value + "<span style='color:#d00; margin-left:10px;'>(" + count + " item)</span>";
},
groupToggleElement: ["arrow", "arrow"],
columns: [{
title: "Department",
field: "Department",
formatter: "plaintext"
},
{
title: "Promo Name",
field: "Promo Name",
formatter: "plaintext"
},
{
title: "Description",
field: "Menu",
formatter: "plaintext"
}, {
title: "Price",
field: "Price",
accessorDownload: colMoneyFormatter,
formatter: "money",
bottomCalc: "sum",
bottomCalcParams: {
precision: 2
},
bottomCalcFormatter: "money",
bottomCalcFormatterParams: {
decimal: ".",
thousand: ",",
symbol: "$"
},
formatterParams: {
decimal: ".",
thousand: ",",
symbol: "$"
}
}, {
title: "Discount",
field: "Discount",
accessorDownload: colMoneyFormatter,
formatter: "money",
bottomCalc: "sum",
bottomCalcParams: {
precision: 2
},
bottomCalcFormatter: "money",
bottomCalcFormatterParams: {
decimal: ".",
thousand: ",",
symbol: "$"
},
formatterParams: {
decimal: ".",
thousand: ",",
symbol: "$"
}
}, {
title: "Check #",
field: "Check #",
formatter: "plaintext"
},
{
title: "Settled By",
field: "Settled By",
formatter: "plaintext"
},
{
title: "Discount By",
field: "Discount By",
formatter: "plaintext"
}
]
});
//trigger download of data.pdf file
$("#download-pdf").click(function(){
table.download("pdf", "data.pdf", {
orientation:"portrait", //set page orientation to portrait
title:"Example Report" //add title to report
});
});
</script>

Related

How to Implement Partial Auth on Authorize.net API [duplicate]

Okay I am setting up Partial Payments via the Authorize.net API in order to enable multiple cards to be used to cover a single balance/charge.
I'm assuming thier Partial Auth feature covers my use case, but in testing, there is an issue I can show you using the API live console here: https://developer.authorize.net/api/reference/index.html#payment-transactions-charge-a-credit-card
Go to the link above and authorize partial payments with the request I edited below and you will notice when you press submit you get a splitTenderId:
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "5KP3u95bQpv",
"transactionKey": "346HZ32z3fP4hTG2"
},
"refId": "123456",
"transactionRequest": {
"transactionType": "authCaptureTransaction",
"amount": "462.25",
"payment": {
"creditCard": {
"cardNumber": "5424000000000015",
"expirationDate": "2020-12",
"cardCode": "999"
}
},
"lineItems": {
"lineItem": {
"itemId": "1",
"name": "vase",
"description": "Cannes logo",
"quantity": "18",
"unitPrice": "45.00"
}
},
"tax": {
"amount": "4.26",
"name": "level2 tax name",
"description": "level2 tax"
},
"duty": {
"amount": "8.55",
"name": "duty name",
"description": "duty description"
},
"shipping": {
"amount": "4.26",
"name": "level2 tax name",
"description": "level2 tax"
},
"poNumber": "456654",
"customer": {
"id": "99999456654"
},
"billTo": {
"firstName": "Ellen",
"lastName": "Johnson",
"company": "Souveniropolis",
"address": "14 Main Street",
"city": "Pecan Springs",
"state": "TX",
"zip": "44628",
"country": "USA"
},
"shipTo": {
"firstName": "China",
"lastName": "Bayles",
"company": "Thyme for Tea",
"address": "12 Main Street",
"city": "Pecan Springs",
"state": "TX",
"zip": "44628",
"country": "USA"
},
"customerIP": "192.168.1.1",
"transactionSettings": {
"setting": [
{
"settingName": "emailCustomer",
"settingValue": "true"
}, {
"settingName": "allowPartialAuth",
"settingValue": "true"
},
]
},
"userFields": {
"userField": [
{
"name": "MerchantDefinedFieldName1",
"value": "MerchantDefinedFieldValue1"
},
{
"name": "favorite_color",
"value": "blue"
}
]
}
}
}
}
This is only successful because the amount is 462.25 as the docs say to use for testing, if I use any other (real) amount the splitTenderId is not there.
Here is a new example request, you can post this again on the link above:
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "5KP3u95bQpv",
"transactionKey": "346HZ32z3fP4hTG2"
},
"refId": "123456",
"transactionRequest": {
"transactionType": "authCaptureTransaction",
"amount": "462",
"payment": {
"creditCard": {
"cardNumber": "5424000000000015",
"expirationDate": "2020-12",
"cardCode": "999"
}
},
"lineItems": {
"lineItem": {
"itemId": "1",
"name": "vase",
"description": "Cannes logo",
"quantity": "18",
"unitPrice": "45.00"
}
},
"tax": {
"amount": "4.26",
"name": "level2 tax name",
"description": "level2 tax"
},
"duty": {
"amount": "8.55",
"name": "duty name",
"description": "duty description"
},
"shipping": {
"amount": "4.26",
"name": "level2 tax name",
"description": "level2 tax"
},
"poNumber": "456654",
"customer": {
"id": "99999456654"
},
"billTo": {
"firstName": "Ellen",
"lastName": "Johnson",
"company": "Souveniropolis",
"address": "14 Main Street",
"city": "Pecan Springs",
"state": "TX",
"zip": "44628",
"country": "USA"
},
"shipTo": {
"firstName": "China",
"lastName": "Bayles",
"company": "Thyme for Tea",
"address": "12 Main Street",
"city": "Pecan Springs",
"state": "TX",
"zip": "44628",
"country": "USA"
},
"customerIP": "192.168.1.1",
"transactionSettings": {
"setting": [
{
"settingName": "emailCustomer",
"settingValue": "true"
}, {
"settingName": "allowPartialAuth",
"settingValue": "true"
},
]
},
"userFields": {
"userField": [
{
"name": "MerchantDefinedFieldName1",
"value": "MerchantDefinedFieldValue1"
},
{
"name": "favorite_color",
"value": "blue"
}
]
}
}
}
}
And with the 462 amount entered this is no longer a partial auth payment and I no longer get a splitTenderId.
Can someone please help me figure out what is going on?

Pulling out specific values from postman response and aggregating them

I am getting below response in my Postman API request to pull a test execution report.
Notice that there are two set of values with "field_name": "Status". I want to aggregate these 2 values to get the following result:
Passed: 1
Unexecuted: 1
I read stuff on the internet, but could not find anything good. Appreciate the help!!
Response body JSON:
[
{
"parentId": 3171717,
"parentType": "test-cycle",
"automation": "No",
"links": [
{
"rel": "test-case",
"href": "xxxxxxx/api/v3/projects/20752/test-cases/27774385?versionId=37779716"
},
{
"rel": "test-cycle",
"href": "xxxxxxx/api/v3/projects/20752/test-cycles/3171717"
},
{
"rel": "self",
"href": "xxxxxxx/api/v3/projects/20752/test-runs/78572177"
}
],
"id": 78572177,
"name": "abcd test case",
"order": 2,
"pid": "TR-166875",
"created_date": "2018-12-05T06:05:32-05:00",
"last_modified_date": "2019-08-28T06:24:03-04:00",
"properties": [
{
"field_id": 2522370,
"field_name": "Run Order",
"field_value": ""
},
{
"field_id": 920264,
"field_name": "Execution Type",
"field_value": "501",
"field_value_name": "Functional"
},
{
"field_id": 920265,
"field_name": "Planned Start Date",
"field_value": "2016-12-23T05:00:00+00:00"
},
{
**"field_id": 920273,
"field_name": "Status",
"field_value": "605",
"field_value_name": "Unexecuted"**
},
{
"field_id": 920266,
"field_name": "Environment",
"field_value": ""
},
{
"field_id": 1055793,
"field_name": "Build Number",
"field_value": ""
},
{
"field_id": 920267,
"field_name": "Planned End Date",
"field_value": "2016-12-23T05:00:00+00:00"
},
{
"field_id": 1055794,
"field_name": "Build URL",
"field_value": ""
},
{
"field_id": 920269,
"field_name": "Target Release/Build",
"field_value": ""
},
{
"field_id": 1055795,
"field_name": "CI Tool",
"field_value": "",
"field_value_name": ""
},
{
"field_id": 920260,
"field_name": "Assigned To",
"field_value": "72843",
"field_value_name": "xxxxxxxx"
}
],
"test_case": {
"links": [],
"id": 27774385
},
"test_case_version_id": 37779716,
"test_case_version": "1.0",
"creator_id": 72843
},
{
"parentId": 3171717,
"parentType": "test-cycle",
"automation": "No",
"links": [
{
"rel": "test-case",
"href": "xxxxxxx/api/v3/projects/20752/test-cases/15957744?versionId=18722069"
},
{
"rel": "test-cycle",
"href": "xxxxxxx/api/v3/projects/20752/test-cycles/3171717"
},
{
"rel": "self",
"href": "xxxxxxx/api/v3/projects/20752/test-runs/26799694"
}
],
"id": 26799694,
"name": "xyzab",
"order": 1,
"pid": "TR-106714",
"created_date": "2017-11-26T23:48:28-05:00",
"last_modified_date": "2019-08-28T06:23:48-04:00",
"properties": [
{
"field_id": 2522370,
"field_name": "Run Order",
"field_value": ""
},
{
"field_id": 920264,
"field_name": "Execution Type",
"field_value": "501",
"field_value_name": "Functional"
},
{
"field_id": 920265,
"field_name": "Planned Start Date",
"field_value": "1989-12-31T05:00:00+00:00"
},
{
**"field_id": 920273,
"field_name": "Status",
"field_value": "601",
"field_value_name": "Passed"**
},
{
"field_id": 920266,
"field_name": "Environment",
"field_value": ""
},
{
"field_id": 1055793,
"field_name": "Build Number",
"field_value": ""
},
{
"field_id": 920267,
"field_name": "Planned End Date",
"field_value": "2016-12-23T05:00:00+00:00"
},
{
"field_id": 1055794,
"field_name": "Build URL",
"field_value": ""
},
{
"field_id": 920269,
"field_name": "Target Release/Build",
"field_value": ""
},
{
"field_id": 1055795,
"field_name": "CI Tool",
"field_value": "",
"field_value_name": ""
},
{
"field_id": 920260,
"field_name": "Assigned To",
"field_value": "22409",
"field_value_name": "xxxxxx"
}
],
"test_case": {
"links": [],
"id": 15957744
},
"test_case_version_id": 18722069,
"test_case_version": "1.0",
"creator_id": 31155
}
]
I was having a look at some local data and this might not work for you but you could give it a go:
let passed = 0
let unexecuted = 0
_.each(pm.response.json(), (data) => {
_.each(data.properties, (arrItem) => {
if(arrItem.field_name === "Status") {
if(arrItem.field_value_name === "Unexecuted") {
unexectued = unexectued + 1
}
else if (arrItem.field_value_name === "Passed"){
passed = passed + 1
}
}
})
})
console.log(`Passed: ${passed}`)
console.log(`Unexecuted: ${unexecuted}`)
I'm sure there are better ways to do this and I'm unsure about the performance of this code when using a larger data set but if you wanted something to work from and get a basic result in the console, this should work.

Is it possible to make a partial refund multiple times?

Is it possible to make partial refund using Authorize.Net? E.g. I have a transaction by 300$. First time I need to make a refund 100$ on this transaction. And second time to make a refund 50$.
What kind of transaction status will be after that?
first request:
...
"refId": "123456",
"transactionRequest": {
"transactionType": "refundTransaction",
"amount": "100.00",}
"refTransId": "1234567890"
}
...
second request:
...
"refId": "123456",
"transactionRequest": {
"transactionType": "refundTransaction",
"amount": "50.00",}
"refTransId": "1234567890"
}
...
Yes, you can make multiple refunds against one transaction as long as you do not go over the original transaction amount.
Below I made a $50 payment and then following up with a $20 and $10 refund. Both were successful.
Here is my original AUTH_CAPTURE request:
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "",
"transactionKey": ""
},
"refId": 38947161,
"transactionRequest": {
"transactionType": "authCaptureTransaction",
"amount": 100,
"payment": {
"creditCard": {
"cardNumber": "4111111111111111",
"expirationDate": "122020",
"cardCode": "999"
}
},
"order": {
"invoiceNumber": "1324567890",
"description": "this is a test transaction"
},
"lineItems": {
"lineItem": [
{
"itemId": "1",
"name": "vase",
"description": "Cannes logo",
"quantity": "18",
"unitPrice": "45.00"
},
{
"itemId": "2",
"name": "desk",
"description": "Big Desk",
"quantity": "10",
"unitPrice": "85.00"
}
]
},
"tax": {
"amount": "4.26",
"name": "level2 tax name",
"description": "level2 tax"
},
"duty": {
"amount": "8.55",
"name": "duty name",
"description": "duty description"
},
"shipping": {
"amount": "4.26",
"name": "level2 tax name",
"description": "level2 tax"
},
"poNumber": "456654",
"customer": {
"id": "18",
"email": "someone#blackhole.tv"
},
"billTo": {
"firstName": "Ellen",
"lastName": "Johnson",
"company": "Souveniropolis",
"address": "14 Main Street",
"city": "Pecan Springs",
"state": "TX",
"zip": "44628",
"country": "USA"
},
"shipTo": {
"firstName": "China",
"lastName": "Bayles",
"company": "Thyme for Tea",
"address": "12 Main Street",
"city": "Pecan Springs",
"state": "TX",
"zip": "44628",
"country": "USA"
},
"customerIP": "192.168.1.1",
"transactionSettings": {
"setting": [
{
"settingName": "allowPartialAuth",
"settingValue": "false"
},
{
"settingName": "duplicateWindow",
"settingValue": "0"
},
{
"settingName": "emailCustomer",
"settingValue": "false"
},
{
"settingName": "recurringBilling",
"settingValue": "false"
},
{
"settingName": "testRequest",
"settingValue": "false"
}
]
},
"userFields": {
"userField": {
"name": "favorite_color",
"value": "blue"
}
}
}
}
}
Here is my original AUTH_CAPTURE response:
{
"transactionResponse": {
"responseCode": "1",
"authCode": "TTJ26B",
"avsResultCode": "Y",
"cvvResultCode": "P",
"cavvResultCode": "2",
"transId": "40032153203",
"refTransID": "",
"transHash": "",
"testRequest": "0",
"accountNumber": "XXXX1111",
"accountType": "Visa",
"messages": [
{
"code": "1",
"description": "This transaction has been approved."
}
],
"userFields": [
{
"name": "favorite_color",
"value": "blue"
}
],
"transHashSha2": "0C9E5A47F93CD5B770A6B4AB24FE0F0D6F3B909066C72A10C4746F48C9886527A18B654736AE8D920D9048A14F2823A0DEC5B5F775A1C48DE9CBCF41C8D28CA3",
"SupplementalDataQualificationIndicator": 0
},
"refId": "38947161",
"messages": {
"resultCode": "Ok",
"message": [
{
"code": "I00001",
"text": "Successful."
}
]
}
}
Here is my first REFUND request (successful):
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "",
"transactionKey": ""
},
"refId": 30777668,
"transactionRequest": {
"transactionType": "refundTransaction",
"amount": 20,
"payment": {
"creditCard": {
"cardNumber": "4111111111111111",
"expirationDate": "122020"
}
},
"authCode": "40032153203"
}
}
}
Here is my first REFUND response (successful):
{
"transactionResponse": {
"responseCode": "1",
"authCode": "",
"avsResultCode": "P",
"cvvResultCode": "",
"cavvResultCode": "",
"transId": "40032153314",
"refTransID": "",
"transHash": "",
"testRequest": "0",
"accountNumber": "XXXX1111",
"accountType": "Visa",
"messages": [
{
"code": "1",
"description": "This transaction has been approved."
}
],
"transHashSha2": "4CE2D83B56C09494AD5609440E3337A6DEE0F4C74FBC99B218D65F6A121EFFC269F8954AC47C17B3B739E61A860C7B0818A8497D85D314D6E0CD89C9FE71A071",
"SupplementalDataQualificationIndicator": 0
},
"refId": "30777668",
"messages": {
"resultCode": "Ok",
"message": [
{
"code": "I00001",
"text": "Successful."
}
]
}
}
Here is my second REFUND request (successful):
{
"createTransactionRequest": {
"merchantAuthentication": {
"name": "",
"transactionKey": ""
},
"refId": 40597686,
"transactionRequest": {
"transactionType": "refundTransaction",
"amount": 10,
"payment": {
"creditCard": {
"cardNumber": "4111111111111111",
"expirationDate": "122020"
}
},
"authCode": "40032153203"
}
}
}
Here is my second REFUND response (successful):
{
"transactionResponse": {
"responseCode": "1",
"authCode": "",
"avsResultCode": "P",
"cvvResultCode": "",
"cavvResultCode": "",
"transId": "40032153319",
"refTransID": "",
"transHash": "",
"testRequest": "0",
"accountNumber": "XXXX1111",
"accountType": "Visa",
"messages": [
{
"code": "1",
"description": "This transaction has been approved."
}
],
"transHashSha2": "FD146FC0F8F3F31068905EC6C8D252662AA735ACDFAC53B4A8D808060C244C06288700AA8872466C3E240B62130D010CFBDE0473D09B631514A6E2E2CB77804F",
"SupplementalDataQualificationIndicator": 0
},
"refId": "40597686",
"messages": {
"resultCode": "Ok",
"message": [
{
"code": "I00001",
"text": "Successful."
}
]
}
}

How do I Compare two JSON files and form a new Custom JSON

My first JSON file(json1.json)
json1 = [{
"id": 547346726,
"appname": "flipkart",
"sum": 120,
"hours": "1 day"
},{
"id": 3094923,
"appname": "Amazon",
"sum": 40,
"hours": "2 days"
}]
My second JSON file(json2.json)
json2 = [{
"id": 547346726,
"Technology": ".Net",
"deployed": "china",
"Type": "Production"
}, {
"id": 3094923,
"Technology": "scala",
"deployed": "Japan",
"Type": "QA"
},
{
"id": 123434,
"Technology": "ruby",
"deployed": "EU",
"Type": "Business"
}
]
My JSON1 have two objects and my second JSON have three objects in that two objects ids are common. compare the JSON file with ids and form a new JSON. If there are no common objects id in two JSON files add the objects to Final JSON
Expecting Output Final.json:
finaljson = [{
"id": 547346726,
"appname": "flipkart",
"sum": 120,
"hours": "1 day",
"Technology": ".Net",
"deployed": "china",
"Type": "Production"
}, {
"id": 3094923,
"appname": "Amazon",
"sum": 40,
"hours": "2 days",
"Technology": "scala",
"deployed": "Japan",
"Type": "QA"
},
{
"id": 123434,
"Technology": "ruby",
"deployed": "EU",
"Type": "Business"
}
]
so far I tried this one
import json
a= json.load( open("newrelics.json"))
b = json.load( open("CareOrchestrator.json"))
# Creating dicts by Ids.
d_a = dict( [ ( x["id"], x ) for x in a ] )
d_b = dict( [ ( x["id"], x ) for x in b ] )
There's no need to create a dict.
You could try something like this:
import json
print("Hello World")
json1 = """[{
"id": 547346726,
"appname": "flipkart",
"sum": 120,
"hours": "1 day"
},{
"id": 3094923,
"appname": "Amazon",
"sum": 40,
"hours": "2 days"
}]"""
json2 = """[{
"id": 547346726,
"Technology": ".Net",
"deployed": "china",
"Type": "Production"
}, {
"id": 3094923,
"Technology": "scala",
"deployed": "Japan",
"Type": "QA"
},
{
"id": 123434,
"Technology": "ruby",
"deployed": "EU",
"Type": "Business"
}
]"""
a = json.loads(json1)
b = json.loads(json2)
final = []
for element in b:
isThere = False
for element2 in a:
if element['id'] == element2["id"]:
element2.update(element)
final.append(element2)
isThere = True
if not isThere:
final.append(element)
json = json.dumps(final)
Be aware that this solution just takes all the elements from the collection b. You won't have elements in the final result if they are just in the collection a. Therefore you'd need another loop or something like that.

list index out of range: How can I deal with list index error

products = [
{"id":1, "name": "Chocolate Sandwich Cookies", "department": "snacks", "aisle": "cookies cakes", "price": 3.50},
{"id":2, "name": "All-Seasons Salt", "department": "pantry", "aisle": "spices seasonings", "price": 4.99},
{"id":3, "name": "Robust Golden Unsweetened Oolong Tea", "department": "beverages", "aisle": "tea", "price": 2.49},
{"id":4, "name": "Smart Ones Classic Favorites Mini Rigatoni With Vodka Cream Sauce", "department": "frozen", "aisle": "frozen meals", "price": 6.99},
{"id":5, "name": "Green Chile Anytime Sauce", "department": "pantry", "aisle": "marinades meat preparation", "price": 7.99},
{"id":6, "name": "Dry Nose Oil", "department": "personal care", "aisle": "cold flu allergy", "price": 21.99},
{"id":7, "name": "Pure Coconut Water With Orange", "department": "beverages", "aisle": "juice nectars", "price": 3.50},
{"id":8, "name": "Cut Russet Potatoes Steam N' Mash", "department": "frozen", "aisle": "frozen produce", "price": 4.25},
{"id":9, "name": "Light Strawberry Blueberry Yogurt", "department": "dairy eggs", "aisle": "yogurt", "price": 6.50},
{"id":10, "name": "Sparkling Orange Juice & Prickly Pear Beverage", "department": "beverages", "aisle": "water seltzer sparkling water", "price": 2.99},
{"id":11, "name": "Peach Mango Juice", "department": "beverages", "aisle": "refrigerated", "price": 1.99},
{"id":12, "name": "Chocolate Fudge Layer Cake", "department": "frozen", "aisle": "frozen dessert", "price": 18.50},
{"id":13, "name": "Saline Nasal Mist", "department": "personal care", "aisle": "cold flu allergy", "price": 16.00},
{"id":14, "name": "Fresh Scent Dishwasher Cleaner", "department": "household", "aisle": "dish detergents", "price": 4.99},
{"id":15, "name": "Overnight Diapers Size 6", "department": "babies", "aisle": "diapers wipes", "price": 25.50},
{"id":16, "name": "Mint Chocolate Flavored Syrup", "department": "snacks", "aisle": "ice cream toppings", "price": 4.50},
{"id":17, "name": "Rendered Duck Fat", "department": "meat seafood", "aisle": "poultry counter", "price": 9.99},
{"id":18, "name": "Pizza for One Suprema Frozen Pizza", "department": "frozen", "aisle": "frozen pizza", "price": 12.50},
{"id":19, "name": "Gluten Free Quinoa Three Cheese & Mushroom Blend", "department": "dry goods pasta", "aisle": "grains rice dried goods", "price": 3.99},
{"id":20, "name": "Pomegranate Cranberry & Aloe Vera Enrich Drink", "department": "beverages", "aisle": "juice nectars", "price": 4.25}
]
product_ids = []
while True:
product_id = input ("Hey please input a product identifier: ")
if product_id == "done":
break
else:
product_ids.append(product_id)
def lookup_product_by_id(product_id):
matching_products = [product for product in products if product["id"] == product_id]
return matching_products[0]
raw_total = 0
for product_id in product_ids:
product = lookup_product_by_id(product_id)
raw_total += product["price"]
print(str(product["id"]) + " " + product["name"] + " " + str(product["price"]))
It says ' list index out of range ' I don't know what is wrong with me. How can I solve this problem? I don't know how 'return matching_products[0]' and 'product = lookup_product_by_id(product_id)' are interracted.