{
	"info": {
		"_postman_id": "5abddcba-0d37-4fa7-baa9-13fe6de4026a",
		"name": "Batch Payment Notification - Viva",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "Batch Payment Notification",
			"event": [
				{
					"listen": "prerequest",
					"script": {
						"exec": [
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"protocolProfileBehavior": {
				"disabledSystemHeaders": {
					"content-type": true
				}
			},
			"request": {
				"auth": {
					"type": "bearer",
					"bearer": [
						{
							"key": "token",
							"value": "{{access_token}}",
							"type": "string"
						}
					]
				},
				"method": "POST",
				"header": [
					{
						"key": "Content-Type",
						"value": "application/json",
						"type": "text"
					}
				],
				"body": {
					"mode": "raw",
					"raw": "{\n\t\"amount\": {{amount_in_cents}},\n\t\"customerTrns\": \"{{short_description}}\",\n\t\"customer\": {\n\t\t\"email\": \"{{customer_email}}\",\n        \"fullName\": \"{{customer_fullname}}\"\n\t},\n    \"paymentNotification\": true,\n    \"preauth\": false,\n    \"allowRecurring\": false,\n    \"paymentTimeOut\": 65535\n}"
				},
				"url": {
					"raw": "https://demo-api.vivapayments.com/checkout/v2/orders",
					"protocol": "https",
					"host": [
						"demo-api",
						"vivapayments",
						"com"
					],
					"path": [
						"checkout",
						"v2",
						"orders"
					]
				}
			},
			"response": []
		}
	],
	"event": [
		{
			"listen": "prerequest",
			"script": {
				"type": "text/javascript",
				"exec": [
					"const id = pm.variables.get('smart_checkout_client_id');",
					"const secret = pm.variables.get('smart_checkout_client_secret');",
					"const token_endpoint = pm.variables.get('token_endpoint');",
					"const user = btoa(id + ':' + secret);",
					"const access_token = pm.variables.get('access_token');",
					"var date = new Date();",
					"var timestamp1 = date.getTime();",
					"var timestamp2 = parseInt(pm.variables.get('unix_timestamp'), 10);    ",
					"if(timestamp1 >= timestamp2  || isNaN(timestamp2) || access_token == \"\"){   ",
					"    const getTokenRequest = {",
					"        method: 'POST',",
					"        url: token_endpoint,",
					"        header: {",
					"            \"Content-Type\": \"application/x-www-form-urlencoded\",",
					"            \"Authorization\": \"Basic \" + user",
					"        }, ",
					"        body: {",
					"            mode: 'urlencoded',",
					"            urlencoded: [",
					"                {key: \"grant_type\", value: \"client_credentials\"}",
					"        ]",
					"        }",
					"    };",
					"    pm.sendRequest(getTokenRequest, (err, response) => {",
					"        if (err) throw new Error(err);",
					"        var jsonData = response.json();",
					"        var date = new Date();",
					"        var set_timestamp = date.getTime()+((jsonData.expires_in - 3560) * 1000);  ",
					"        pm.variables.set('access_token', jsonData.access_token);",
					"        pm.variables.set('unix_timestamp', set_timestamp);              ",
					"    });",
					"}else{",
					"    pm.variables.set('access_token', pm.variables.get('access_token'));",
					"}"
				]
			}
		},
		{
			"listen": "test",
			"script": {
				"type": "text/javascript",
				"exec": [
					""
				]
			}
		}
	],
	"variable": [
		{
			"key": "smart_checkout_client_id",
			"value": ""
		},
		{
			"key": "smart_checkout_client_secret",
			"value": ""
		},
		{
			"key": "token_endpoint",
			"value": "https://demo-accounts.vivapayments.com/connect/token"
		},
		{
			"key": "access_token",
			"value": ""
		},
		{
			"key": "unix_timestamp",
			"value": ""
		}
	]
}
