Shippo: Using generated transaction's label_url variable in other functions - javascript

I'm hoping someone with more experience working with Shippo's API in Node can help me figure this out.
My end goal is to press a button on an angular form front end, have the shipment transaction created, and use the transaction's label URL to create a custom PDF. Everything is working except for pushing the generated label URL to the PDF template.
First, I pasted Shippo's single click label creation example into an Express POST route. It worked just fine, generating transactions that I could check out by pinging Shippo's API for a list of recent transactions and viewing the most recent one.
Then, I added lines 102-111 of this code example and replaced the generic error message generator in the instalabel code with it, so my current app.js Shippo transaction logic looks like this:
shippo.transaction.create({
"shipment": shipment,
"carrier_account": "xxxxxxxxxxxxxxxxxxxxxx",
"servicelevel_token": "ups_ground",
"label_file_type": "PNG"
},function(transaction, err ){
console.log("transaction : %s", JSON.stringify(transaction, null, 4));
// print label_url and tracking_number
if(transaction.object_status == "SUCCESS") {
console.log("Label URL: %s", transaction.label_url);
console.log("Tracking Number: %s", transaction.tracking_number);
exports.label_url = transaction.label_url;
exports.tracking_number = transaction.tracking_number;
}else{
//Deal with an error with the transaction
console.log("Message: %s", transaction.messages);
}
});
I need to grab the label_url & tracking_number for the transaction that was just created and use it in another function, but everything I've tried so far seems to end up with an error like the following:
/src/app.js:88
if(transaction.object_status == "SUCCESS") {
^
TypeError: Cannot read property 'object_status' of null
I'm guessing this is because the function to create a shipping label isn't exporting the response Shippo is sending back, so I can't use it in other functions... Is that accurate, or am I barking up the wrong tree here? For reference, this is what Shippo's response is supposed to] look like:
{
"object_state":"VALID",
"object_status":"SUCCESS",
"object_created":"2014-07-25T02:09:34.422Z",
"object_updated":"2014-07-25T02:09:34.513Z",
"object_id":"ef8808606f4241ee848aa5990a09933c",
"object_owner":"shippotle#goshippo.com",
"was_test":true,
"rate":"ee81fab0372e419ab52245c8952ccaeb",
"tracking_number":"tracking_number_goes_here",
"tracking_status":null,
"tracking_url_provider":"",
"label_url":"label_url_goes_here",
"commercial_invoice_url": "",
"messages":[
],
"customs_note":"",
"submission_note":"",
"metadata":""
}
What can I do to use the values from this response outside the shippo.transaction.create function itself?
Thanks for reading.

The parameters you're passing to the callback used in shippo.transaction.create has its injected params reversed. It should be (err, transaction) not (transaction, err). Alternatively, you can chain on a .then() and pass a callback that only takes (transaction) and access any error messages in transaction.messages.
Below is a working example with the latest version of the shippo-node-wrapper at the time of this writing.
var shippo = require('./lib/shippo')('<API KEY>');
var addressFrom = {
"object_purpose":"PURCHASE",
"name":"Ms Hippo",
"company":"Shippo",
"street1":"215 Clayton St.",
"city":"San Francisco",
"state":"CA",
"zip":"94117",
"country":"US", //iso2 country code
"phone":"+1 555 341 9393",
"email":"ms-hippo#goshippo.com",
};
// example address_to object dict
var addressTo = {
"object_purpose":"PURCHASE",
"name":"Mr Hippo",
"company":"Happy Hippo",
"street1":"965 Mission St",
"street2":"Suite 425",
"city":"San Francisco",
"state":"CA",
"zip":"94103",
"country":"US", //iso2 country code
"phone":"949-123-4567",
"email":"mrhippo#goshippo.com",
"metadata" : "Hippo T-Shirt Order #1043"
};
// parcel object dict
var parcel = {
"length":"5",
"width":"5",
"height":"5",
"distance_unit":"in",
"weight":"2",
"mass_unit":"lb",
};
var shipment = {
"object_purpose": "PURCHASE",
"address_from": addressFrom,
"address_to": addressTo,
"parcel": parcel,
"async": false
};
shippo.transaction.create({
"shipment": shipment,
"carrier_account": "07280f4f96f34cc8b75e593c4835dc38",
"servicelevel_token": "usps_priority",
"label_file_type": "PNG"
}, function (err, transaction) {
console.log("transaction : %s", JSON.stringify(transaction, null, 4));
// print label_url and tracking_number
if (transaction.object_status == "SUCCESS") {
console.log("Label URL: %s", transaction.label_url);
console.log("Tracking Number: %s", transaction.tracking_number);
} else {
//Deal with an error with the transaction
console.log("Message: %s", transaction.messages);
}
});
// OR
shippo.transaction.create({
"shipment": shipment,
"carrier_account": "07280f4f96f34cc8b75e593c4835dc38",
"servicelevel_token": "usps_priority",
"label_file_type": "PNG"
}).then(function (transaction) {
console.log("transaction : %s", JSON.stringify(transaction, null, 4));
// print label_url and tracking_number
if (transaction.object_status == "SUCCESS") {
console.log("Label URL: %s", transaction.label_url);
console.log("Tracking Number: %s", transaction.tracking_number);
} else {
//Deal with an error with the transaction
console.log("Message: %s", transaction.messages);
}
});

Related

Blockcypher transaction signing error using bitcoinjs

Im trying to sign a Bitcoin testnet transaction using blockcypher and the bitcoinjs lib described here, but I have run into this error and I am not usre what I am doing wrong.
{"error": "Couldn't deserialize request: invalid character 'x' in literal true (expecting 'r')"}
When searching around I cannot find a solution to the problem, I have contacted blockcypher but they never respond. Here is the code im using to sign the transaction, does anyone know why its giving me this error?
var bitcoin = require("bitcoinjs-lib");
var buffer = require('buffer');
var keys = new bitcoin.ECPair.fromWIF('cMvPQZiG5mLARSjxbBwMxKwzhTHaxgpTsXB6ymx7SGAeYUqF8HAT', bitcoin.networks.testnet);
var newtx = {
inputs: [{addresses: ['ms9ySK54aEC2ykDviet9jo4GZE6GxEZMzf']}],
outputs: [{addresses: ['msWccFYm5PPCn6TNPbNEnprA4hydPGadBN'], value: 1000}]
};
// calling the new endpoint, same as above
$.post('https://api.blockcypher.com/v1/btc/test3/txs/new', JSON.stringify(newtx))
.then(function(tmptx) {
// signing each of the hex-encoded string required to finalize the transaction
tmptx.pubkeys = [];
tmptx.signatures = tmptx.tosign.map(function(tosign, n) {
tmptx.pubkeys.push(keys.publicKey.toString("hex"));
const SIGHASH_ALL = 0x01;
return bitcoin.script.signature.encode(keys.sign(new buffer.Buffer(tosign, "hex")), SIGHASH_ALL,).toString("hex");
});
// sending back the transaction with all the signatures to broadcast
$.post('https://api.blockcypher.com/v1/btc/test3/txs/send', tmptx).then(function(finaltx) {
console.log(finaltx);
}).catch(function (response) {
console.log(response.responseText);
});
}).catch(function (response) {
console.log(response.responseText);
});

Parse Server Cloud Code Update User

I am attempting to update a parse user field and the function stops in the middle of it:
Parse.Cloud.define("modifyAdminStatus", function(request, response) {
var userQuery = new Parse.Query(Parse.User);
var isAdmin = request.params.adminStatus;
console.log("isAdmin:" + isAdmin);
userQuery.equalTo("username", request.params.username);
userQuery.find({ useMasterKey: true,
success: function(user) {
console.log(user.length);
console.log("Got User")
console.log(user);
user.set("isAdmin", isAdmin);
console.log("Set Status");
user.save(null, {useMasterKey: true,
success: function(user) {
response.success();
},
error: function(error) {
response.error(error.message);
}
});
},
error: function(error) {
response.error(error.message);
}
});
});
I dont get any syntax errors, when i run the code i get:
1
Got User
[ ParseUser { _objCount: 2, className: '_User', id: '2vigcitsl6' } ]
in my console. However, it seems to stop the code after i attempt to set the admin status. I have tried running it using useMasterKey but that didnt do anything so maybe I'm missing something and where the useMasterKey should go?
The answer is:
query.find({
... code here
});
Returns an array, using query.first (or selecting one object from the array) instead will get one object and allow you to set things on it.
When you're trying to save the user, parse expects two parameters. The first should be an object containing any changes, and the second should be the save options.
So in your case, simply change your save to user.save (null, {useMasterKey:true, success...})
The way you have it now would create a column on Parse.User entitled useMasterKey, if permissions allow.

How to prevent current user get notified?

I'm making an app that allows user to like and comment on other user post. I'm using Parse as my backend. I'm able to notified user everytime their post liked or commented. However if current user like or comment on their own post this current user still notified. How can I prevent this?
Here is the js code that I use:
Parse.Cloud.afterSave('Likes', function(request) {
// read pointer async
request.object.get("likedPost").fetch().then(function(like){
// 'post' is the commentedPost object here
var liker = like.get('createdBy');
// proceed with the rest of your code - unchanged
var query = new Parse.Query(Parse.Installation);
query.equalTo('jooveUser', liker);
Parse.Push.send({
where: query, // Set our Installation query.
data: {
alert: message = request.user.get('username') + ' liked your post',
badge: "Increment",
sound: "facebook_pop.mp3",
t : "l",
lid : request.object.id,
pid: request.object.get('likedPostId'),
lu : request.user.get('username'),
ca : request.object.createdAt,
pf : request.user.get('profilePicture')
}
}, {
success: function() {
console.log("push sent")
},
error: function(err) {
console.log("push not sent");
}
});
});
});
If I understand the context of where this code is correctly,
I recommend checking
if request.user.get("username") != Parse.CurrentUser.get("username")
Before sending out the push notification
Where is your cloud function being called from? If you're calling it from your ios code, then before you call the cloud code function, just prelude it with something like this:
if (PFUser.currentUser?.valueForKey("userName") as! String) != (parseUser.valueForKey("userName") as! String)

JSON parsing issues are giving me a 'Cannot read property of undefined' error.

I'm working on cloud code that pings eBay, returns JSON, parses it, and stores the top two categories into an array. The parameters sent to eBay are based on what a user types into itemSearch bar the iOS app. When I attempt to send a query like "iPhone", it gives this error:
TypeError: Cannot read property 'findItemsByKeywordsResponse' of undefined
at Object.Parse.Cloud.httpRequest.success (main.js:37:15)
at Object.<anonymous> (<anonymous>:565:19) (Code: 141, Version: 1.2.18)
Here is my objective-c code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.nextButton) return;
if (self.itemSearch.text.length > 0) {
[PFCloud callFunctionInBackground:#"eBayCategorySearch"
withParameters:#{#"item": self.itemSearch.text}
block:^(NSString *result, NSError *error) {
if (!error) {
NSLog(#"Successfully pinged eBay!");
}
}];
}
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
The cloud code (main.js) running on Parse:
Parse.Cloud.define("eBayCategorySearch", function(request, response) {
url = 'http://svcs.ebay.com/services/search/FindingService/v1';
Parse.Cloud.httpRequest({
url: url,
params: {
'OPERATION-NAME' : 'findItemsByKeywords',
'SERVICE-VERSION' : '1.12.0',
'SECURITY-APPNAME' : '*APP ID GOES HERE*',
'GLOBAL-ID' : 'EBAY-US',
'RESPONSE-DATA-FORMAT' : 'JSON',
'itemFilter(0).name=ListingType' : 'itemFilter(0).value=FixedPrice',
'keywords' : request.params.item,
// your other params
},
success: function (httpResponse) {
var response = JSON.parse(httpResponse.text)
// count number of times each unique primaryCategory shows up (based on categoryId), return top two (done with a for loop?)
var userCategories = {};
var data = httpResponse.data
data.findItemsByKeywordsResponse.searchResult[0].item.forEach(function(item)
{
var id = item.primaryCategory[0].categoryId;
if (userCategories[id]) userCategories[id]++;
else userCategories[id] = 1;
});
var top2 = Object.keys(userCategories).sort(function(a, b)
{return userCategories[b]-userCategories[a]; }).slice(0, 2);
console.log('Top two categories: ' + top2.join(', '));
response.success(httpResponse.data)
// if user has criteria info for one of the categories already, default to that, send to matchcenter
// if user has criteria for both categories, ask which one, default to selected categories criteria, send to matchcenter
// if user has no criteria for either category, redirect to criteriaViewController, save the criteria user inputs, send to matchcenter
// deal with success and respond to query
},
error: function (httpResponse) {
console.log('error!!!');
console.error('Request failed with response code ' + httpResponse.status);
}
});
});
The JSON usually looks like this when returned:
{
"findItemsByKeywordsResponse":[
{
"ack":[
"Success"
],
"version":[
"1.12.0"
],
"timestamp":[
"2014-03-26T18:29:40.583Z"
],
"searchResult":[
{
"#count":"100",
"item":[
{
"itemId":[
"151258132867"
],
"title":[
"Apple iPhone 4 - clean esn - Black (Verizon) Smartphone"
],
"globalId":[
"EBAY-US"
],
"primaryCategory":[
{
"categoryId":[
"9355"
],
"categoryName":[
"Cell Phones & Smartphones"
I don't think it's being returned properly, hence its inability to find "findItemsByKeywordsResponse". Is there a way I can print out the JSON being returned, to see whether I'm parsing the wrong thing?
You are parsing your response:
var response = JSON.parse(httpResponse.text)
But after that you don't use it. You work instead with httpResponse.data.
So try using your response object:
response.findItemsByKeywordsResponse.searchResult[0]......

web sql error "current version of the database and `oldVersion` argument do not match"

I am trying to run these set of functions:
function erorr(e) {
// error getting database
alert(e.message);
}
window.onload = function() {
prepareDatabase(erorr);
};
function prepareDatabase(error) {
return openDatabase('tasks13', '', 'Offline task storage', 3*1024*1024, function (db) {
db.changeVersion('', '1.0', function (t) {
t.executeSql('CREATE TABLE tasks (id, detail,status)');
}, error);
});
}
But, after running this I get an error current version of the database and 'oldVersion' argument do not match.
Not sure what wrong I am doing here.
Correct code:
function prepareDatabase(error) {
return openDatabase('tasks13', '', 'Offline task storage', 3*1024*1024, function (db) {
db.changeVersion(db.version, '1.0', function (t) {
t.executeSql('CREATE TABLE tasks (id, detail,status)');
}, error);
});
}
While it's possible to open any available version of a WebSQL db (by passing an empty string as version identifier), you need to explicitly specify the current version of the db when calling db.changeVersion. The current version of the db is made available as db.version.
This is what the specification says:
Check that the value of the first argument to the changeVersion() method exactly matches the database's actual version. If it does not, then the preflight operation fails.
From http://www.w3.org/TR/webdatabase/#asynchronous-database-api
I ran into the same error.
I refrained from using db.changeVersion and used the following more imperative style of logic instead:
this.db = window.openDatabase('myDb', '1.0', 'a comment', 5*1024*1024);
if (this.db) {
this.db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS myTable(...)',
[],
function(tx, rs) { },
function(tx, err) { alert("Error in create table occurred: " + err) }
);
});
}
Hope it works for you as well.
/Fredrik

Categories

Resources