Skip to content

Instantly share code, notes, and snippets.

@ismailalabou
Forked from andrewwiik/example.js
Created May 28, 2021 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ismailalabou/25039f2f9d3cabd498a0b44e5c6f5bf2 to your computer and use it in GitHub Desktop.
Save ismailalabou/25039f2f9d3cabd498a0b44e5c6f5bf2 to your computer and use it in GitHub Desktop.
Add Tracking Info to a PayPal Transaction
/**
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment