{"_id":"555236587988e10d003452e9","project":"5436a1e1d0ffee0e00f18f8c","user":"5436a1afb7cf0e1c0020d9ca","version":{"_id":"54cc2148380ecd0d00ca18aa","__v":2,"forked_from":"5436a1e1d0ffee0e00f18f8f","project":"5436a1e1d0ffee0e00f18f8c","createdAt":"2015-01-31T00:26:48.753Z","releaseDate":"2015-01-31T00:26:48.753Z","categories":["54cc2149380ecd0d00ca18ab","54cc2149380ecd0d00ca18ac","54cc2149380ecd0d00ca18ad","54cc2149380ecd0d00ca18ae","54cc2149380ecd0d00ca18af","54cc2149380ecd0d00ca18b0","54cc2149380ecd0d00ca18b1","54cc2149380ecd0d00ca18b2","54cc2149380ecd0d00ca18b3","552f29ca633a5b0d00e99d09"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.1.0","version":"1.1"},"category":{"_id":"54cc2149380ecd0d00ca18ae","__v":2,"pages":["54cc2149380ecd0d00ca18d2","54cc2149380ecd0d00ca18d3","54cc2149380ecd0d00ca18d4","54cc2149380ecd0d00ca18d5","555236587988e10d003452e9"],"project":"5436a1e1d0ffee0e00f18f8c","version":"54cc2148380ecd0d00ca18aa","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-09T15:31:35.377Z","from_sync":false,"order":3,"slug":"easyship-widget","title":"EasyShip Widget"},"parentDoc":null,"__v":4,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-05-12T17:20:24.386Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":5,"body":"You may configure EasyShip to execute callback functions on the success or failure of a shipment label's generation. As each shipment label is submitted for purchase on your page, a poller will monitor the statuses for all pending labels until generation has completed.\n\nA successful label generation will trigger the `.success()` callback function and, likewise, a failure to generate a label will trigger the `.failure()` function.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Example configuration\"\n}\n[/block]\nTo enable this feature, you must set a polling interval in milliseconds, with a minimum of 500ms.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<script\\n  src=\\\"https://app.shippingeasy.com/easyship.js\\\"\\n  data-partner-key=\\\"XXX\\\"\\n  data-polling-interval=1000 \\n  data-token=\\\"dc02eec82448924a6a8d0ad4a1174af41e342fc2d45962e702ac137bd\\\">\\n</script>\",\n      \"language\": \"html\"\n    }\n  ]\n}\n[/block]\nBy default the success and failure functions do nothing, so you will need to customize them. A JSON object representing the shipment will be passed into the callback functions and is identical to the payload sent via the EasyShip webhooks.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<script>\\n  EasyShip.success = function (shipment) {\\n\\t  console.log(\\\"Success: \\\" + shipment.tracking_number);\\n  }\\n\\n  EasyShip.failure = function (shipment) {\\n\\t  console.log(\\\"Failure: \\\" + shipment.order_number);\\n  }\\n</script>\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Example JSON payload\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{  \\n   \\\"order_number\\\":\\\"w123456xxZs77\\\",\\n   \\\"refund_url\\\":\\\"http://127.0.0.1:5000/easyship/shipments/22d1483733a4ec3211028fe3307e2a7c4c0a3466a6adf1032095d717b558906cfebc5b3ca3853f6406e96bda5a89b290d46d2b7aaf0a3fa77f996e2acbed6cff/cancellations\\\",\\n   \\\"tracking_url\\\":\\\"https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1=0405510200830101052348\\\",\\n   \\\"tracking_number\\\":\\\"0405510200830101052348\\\",\\n   \\\"ship_date\\\":\\\"2015-05-12\\\",\\n   \\\"carrier\\\":\\\"USPS\\\",\\n   \\\"status\\\":\\\"purchased\\\",\\n   \\\"carrier_service\\\":\\\"Priority Mail (2-3 days)\\\",\\n   \\\"shipper\\\":{  \\n      \\\"external_identifier\\\":\\\"9283788\\\",\\n      \\\"name\\\":\\\"EasyShip Test Customer\\\",\\n      \\\"email\\\":\\\"product+easyship+consumer:::at:::shippingeasy.com\\\"\\n   }\\n}\",\n      \"language\": \"json\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"client-side-callbacks","type":"basic","title":"Client-side callbacks"}

Client-side callbacks


You may configure EasyShip to execute callback functions on the success or failure of a shipment label's generation. As each shipment label is submitted for purchase on your page, a poller will monitor the statuses for all pending labels until generation has completed. A successful label generation will trigger the `.success()` callback function and, likewise, a failure to generate a label will trigger the `.failure()` function. [block:api-header] { "type": "basic", "title": "Example configuration" } [/block] To enable this feature, you must set a polling interval in milliseconds, with a minimum of 500ms. [block:code] { "codes": [ { "code": "<script\n src=\"https://app.shippingeasy.com/easyship.js\"\n data-partner-key=\"XXX\"\n data-polling-interval=1000 \n data-token=\"dc02eec82448924a6a8d0ad4a1174af41e342fc2d45962e702ac137bd\">\n</script>", "language": "html" } ] } [/block] By default the success and failure functions do nothing, so you will need to customize them. A JSON object representing the shipment will be passed into the callback functions and is identical to the payload sent via the EasyShip webhooks. [block:code] { "codes": [ { "code": "<script>\n EasyShip.success = function (shipment) {\n\t console.log(\"Success: \" + shipment.tracking_number);\n }\n\n EasyShip.failure = function (shipment) {\n\t console.log(\"Failure: \" + shipment.order_number);\n }\n</script>", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Example JSON payload" } [/block] [block:code] { "codes": [ { "code": "{ \n \"order_number\":\"w123456xxZs77\",\n \"refund_url\":\"http://127.0.0.1:5000/easyship/shipments/22d1483733a4ec3211028fe3307e2a7c4c0a3466a6adf1032095d717b558906cfebc5b3ca3853f6406e96bda5a89b290d46d2b7aaf0a3fa77f996e2acbed6cff/cancellations\",\n \"tracking_url\":\"https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1=0405510200830101052348\",\n \"tracking_number\":\"0405510200830101052348\",\n \"ship_date\":\"2015-05-12\",\n \"carrier\":\"USPS\",\n \"status\":\"purchased\",\n \"carrier_service\":\"Priority Mail (2-3 days)\",\n \"shipper\":{ \n \"external_identifier\":\"9283788\",\n \"name\":\"EasyShip Test Customer\",\n \"email\":\"product+easyship+consumer@shippingeasy.com\"\n }\n}", "language": "json" } ] } [/block]