Created
April 24, 2019 14:31
-
-
Save andrewwiik/dedc36e88d9ffb4aab491bfdd9672470 to your computer and use it in GitHub Desktop.
Add Tracking Info to a PayPal Transaction
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
/** | |
Possible Carrier Codes: | |
- UPS | |
- USPS | |
- FEDEX | |
- AIRBORNE_EXPRESS | |
- DHL | |
- DHL_GLOBAL_ECOMMERCE | |
- US_ASCENDIA | |
- US_DTDC | |
- US_ENSENDA | |
- US_FASTWAY | |
- US_GLOBEGISTICS | |
- US_ONTRAC | |
- US_PUROLATOR | |
- US_RL | |
- RRDONNELLEY | |
- SENDLE | |
- US_STARTRACK | |
- OTHER | |
*/ | |
// This the V2 of the PayPal Node SDK | |
const paypal = require('paypal-rest-sdk'); | |
const _ = require('lodash'); | |
class TrackersCreateRequest { | |
constructor() { | |
this.path = '/v1/shipping/trackers'; | |
this.verb = 'POST'; | |
this.body = null; | |
this.headers = { | |
'Content-Type': 'application/json' | |
}; | |
} | |
requestBody(tracker) { | |
this.body = tracker; | |
return this; | |
} | |
} | |
/** | |
* Add tracking info to a PayPal Transaction | |
* | |
* @async | |
* @param {String} transactionId - The ID of the PayPal Transaction | |
* @param {String?} trackingId - The Tracking Number for the transaction if their is | |
* one | |
* @param {String} trackingStatus - The Tracking Status, it can be "SHIPPED", | |
* "IN_PROCESS", "ON_HOLD", "CANCELLED", "RETURNED", or "PROCESSED" | |
* @param {String} carrier - The Carrier Code, it can be any of the carrier codes listed | |
* above, if it is "OTHER" and the status is not "PROCESSED" you should supply the | |
* "carrierName" argument. | |
* @param {String?} carrierName - The name of the carrier if the carrier code was | |
* specified as "OTHER" | |
* @param {PayPalHttpClient} client - The PayPal client to use to send the request | |
* @return {Promise<Object>} The Create Tracker response | |
*/ | |
async addTrackingInfo (transactionId, trackingId, trackingStatus, carrier, carrierName, client) { | |
try { | |
let createTrackerRequest = new TrackersCreateRequest(); | |
createTrackerRequest.requestBody({ | |
trackers: [ | |
_.omitBy({ | |
carrier: carrier, | |
carrier_name_other: carrierName || undefined, | |
status: trackingStatus, | |
tracking_number: trackingId || undefined, | |
transaction_id: transactionId | |
}, _.isNil) | |
] | |
}); | |
let createTrackerResponse = await client.execute(createTrackerRequest); | |
return createTrackerResponse; | |
} catch (err) { | |
return Promise.reject(err); | |
} | |
} |
PayPal has made an official API for this now so you do not need this anymore.
PayPal has made an official API for this now so you do not need this anymore.
okey thanks 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello @andrewwiik i just tried this, it's not working ...