-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from coldbrewcloud/update_2017_03_28
Version 2017-03-29
- Loading branch information
Showing
22 changed files
with
266 additions
and
260 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ package main | |
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
|
@@ -16,97 +17,33 @@ import ( | |
"github.com/coldbrewcloud/go-shippo/models" | ||
) | ||
|
||
var ( | ||
privateToken = os.Getenv("PRIVATE_TOKEN") | ||
upsUserName = os.Getenv("UPS_USERNAME") | ||
upsPassword = os.Getenv("UPS_PASSWORD") | ||
upsAccountNumber = os.Getenv("UPS_ACCOUNT_NUMBER") | ||
) | ||
|
||
func main() { | ||
privateToken := os.Getenv("PRIVATE_TOKEN") | ||
if privateToken == "" { | ||
panic(errors.New("Please set $PRIVATE_TOKEN with your Shippo API private token.")) | ||
} | ||
|
||
// create a Shippo Client instance | ||
c := shippo.NewClient(privateToken) | ||
|
||
// create or update carrier account | ||
carrierAccountObjectID := prepareCarrierAccount(c) | ||
|
||
// create shipment using the carrier account | ||
shipment := createShipmentUsingCarrierAccount(c, carrierAccountObjectID) | ||
// create shipment | ||
shipment := createShipment(c) | ||
|
||
// purchase shipping label | ||
purchaseShippingLabel(c, shipment) | ||
} | ||
|
||
func prepareCarrierAccount(c *client.Client) string { | ||
// list all registered carrier account | ||
allCarrierAccounts, err := c.ListAllCarrierAccounts() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// create UPS carrier account if not added | ||
carrierAccountObjectID := "" | ||
for _, ca := range allCarrierAccounts { | ||
if ca.Carrier == models.CarrierUPS && ca.AccountID == upsUserName { | ||
carrierAccountObjectID = ca.ObjectID | ||
break | ||
} | ||
} | ||
if carrierAccountObjectID == "" { | ||
// create a new carrier account | ||
input := &models.CarrierAccountInput{ | ||
Carrier: models.CarrierUPS, | ||
AccountID: upsUserName, | ||
Parameters: map[string]interface{}{ | ||
"password": upsPassword, | ||
"account_number": upsAccountNumber, | ||
}, | ||
Test: true, | ||
Active: true, | ||
} | ||
carrierAccount, err := c.CreateCarrierAccount(input) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("Carrier account registered: %s\n", dump(carrierAccount)) | ||
} else { | ||
// update existing carrier account | ||
input := &models.CarrierAccountInput{ | ||
Carrier: models.CarrierUPS, | ||
AccountID: upsUserName, | ||
Parameters: map[string]interface{}{ | ||
"password": upsPassword, | ||
"account_number": upsAccountNumber, | ||
}, | ||
Test: true, | ||
Active: true, | ||
} | ||
carrierAccount, err := c.UpdateCarrierAccount(carrierAccountObjectID, input) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
carrierAccountObjectID = carrierAccount.ObjectID | ||
|
||
fmt.Printf("Carrier account updated: %s\n", dump(carrierAccount)) | ||
} | ||
|
||
return carrierAccountObjectID | ||
} | ||
|
||
func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID string) *models.Shipment { | ||
func createShipment(c *client.Client) *models.Shipment { | ||
// create a sending address | ||
addressFromInput := &models.AddressInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
Name: "Mr. Hippo", | ||
Street1: "215 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94117", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
Name: "Mr. Hippo", | ||
Street1: "215 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94117", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
} | ||
addressFrom, err := c.CreateAddress(addressFromInput) | ||
if err != nil { | ||
|
@@ -115,15 +52,14 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID | |
|
||
// create a receiving address | ||
addressToInput := &models.AddressInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
Name: "Mrs. Hippo", | ||
Street1: "965 Mission St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94105", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
Name: "Mrs. Hippo", | ||
Street1: "965 Mission St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94105", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
} | ||
addressTo, err := c.CreateAddress(addressToInput) | ||
if err != nil { | ||
|
@@ -146,12 +82,10 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID | |
|
||
// create a shipment | ||
shipmentInput := &models.ShipmentInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
AddressFrom: addressFrom.ObjectID, | ||
AddressTo: addressTo.ObjectID, | ||
Parcel: parcel.ObjectID, | ||
CarrierAccounts: []string{carrierAccountObjectID}, | ||
Async: false, | ||
AddressFrom: addressFrom.ObjectID, | ||
AddressTo: addressTo.ObjectID, | ||
Parcels: []string{parcel.ObjectID}, | ||
Async: false, | ||
} | ||
shipment, err := c.CreateShipment(shipmentInput) | ||
if err != nil { | ||
|
@@ -165,7 +99,7 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID | |
|
||
func purchaseShippingLabel(c *client.Client, shipment *models.Shipment) { | ||
transactionInput := &models.TransactionInput{ | ||
Rate: shipment.RatesList[0].ObjectID, | ||
Rate: shipment.Rates[len(shipment.Rates)-1].ObjectID, | ||
LabelFileType: models.LabelFileTypePDF, | ||
Async: false, | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.4.1 | ||
1.5.0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,15 @@ func main() { | |
|
||
// Address #1: Valid | ||
address1Input := &models.AddressInput{ | ||
Name: "Shawn Ippotle", | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
Company: "Shippo", | ||
Street1: "215 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94117", | ||
Country: "US", | ||
Email: "[email protected]", | ||
Validate: true, | ||
Name: "Shawn Ippotle", | ||
Company: "Shippo", | ||
Street1: "215 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94117", | ||
Country: "US", | ||
Email: "[email protected]", | ||
Validate: true, | ||
} | ||
address1, err := c.CreateAddress(address1Input) | ||
if err != nil { | ||
|
@@ -38,15 +37,14 @@ func main() { | |
|
||
// Address #2: Wrong street name | ||
address2Input := &models.AddressInput{ | ||
Name: "Mr. Hippo", | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
Street1: "4610 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94107", | ||
Country: "US", | ||
Email: "[email protected]", | ||
Validate: true, | ||
Name: "Mr. Hippo", | ||
Street1: "4610 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94107", | ||
Country: "US", | ||
Email: "[email protected]", | ||
Validate: true, | ||
} | ||
address2, err := c.CreateAddress(address2Input) | ||
if err != nil { | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,14 +55,15 @@ func prepareCarrierAccount(c *client.Client) string { | |
"password": upsPassword, | ||
"account_number": upsAccountNumber, | ||
}, | ||
Test: true, | ||
Active: true, | ||
} | ||
carrierAccount, err := c.CreateCarrierAccount(input) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
carrierAccountObjectID = carrierAccount.ObjectID | ||
|
||
fmt.Printf("Carrier account registered: %s\n", dump(carrierAccount)) | ||
} else { | ||
// update existing carrier account | ||
|
@@ -73,7 +74,6 @@ func prepareCarrierAccount(c *client.Client) string { | |
"password": upsPassword, | ||
"account_number": upsAccountNumber, | ||
}, | ||
Test: true, | ||
Active: true, | ||
} | ||
carrierAccount, err := c.UpdateCarrierAccount(carrierAccountObjectID, input) | ||
|
@@ -92,15 +92,14 @@ func prepareCarrierAccount(c *client.Client) string { | |
func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID string) *models.Shipment { | ||
// create a sending address | ||
addressFromInput := &models.AddressInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
Name: "Mr. Hippo", | ||
Street1: "215 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94117", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
Name: "Mr. Hippo", | ||
Street1: "215 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94117", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
} | ||
addressFrom, err := c.CreateAddress(addressFromInput) | ||
if err != nil { | ||
|
@@ -109,15 +108,14 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID | |
|
||
// create a receiving address | ||
addressToInput := &models.AddressInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
Name: "Mrs. Hippo", | ||
Street1: "965 Mission St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94105", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
Name: "Mrs. Hippo", | ||
Street1: "965 Mission St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94105", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
} | ||
addressTo, err := c.CreateAddress(addressToInput) | ||
if err != nil { | ||
|
@@ -140,10 +138,9 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID | |
|
||
// create a shipment | ||
shipmentInput := &models.ShipmentInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
AddressFrom: addressFrom.ObjectID, | ||
AddressTo: addressTo.ObjectID, | ||
Parcel: parcel.ObjectID, | ||
Parcels: []string{parcel.ObjectID}, | ||
CarrierAccounts: []string{carrierAccountObjectID}, | ||
Async: false, | ||
} | ||
|
@@ -159,7 +156,7 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID | |
|
||
func purchaseShippingLabel(c *client.Client, shipment *models.Shipment) { | ||
transactionInput := &models.TransactionInput{ | ||
Rate: shipment.RatesList[0].ObjectID, | ||
Rate: shipment.Rates[0].ObjectID, | ||
LabelFileType: models.LabelFileTypePDF, | ||
Async: false, | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,15 +30,14 @@ func main() { | |
func createShipment(c *client.Client) *models.Shipment { | ||
// create a sending address | ||
addressFromInput := &models.AddressInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
Name: "Mr. Hippo", | ||
Street1: "215 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94117", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
Name: "Mr. Hippo", | ||
Street1: "215 Clayton St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94117", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
} | ||
addressFrom, err := c.CreateAddress(addressFromInput) | ||
if err != nil { | ||
|
@@ -47,15 +46,14 @@ func createShipment(c *client.Client) *models.Shipment { | |
|
||
// create a receiving address | ||
addressToInput := &models.AddressInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
Name: "Mrs. Hippo", | ||
Street1: "965 Mission St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94105", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
Name: "Mrs. Hippo", | ||
Street1: "965 Mission St.", | ||
City: "San Francisco", | ||
State: "CA", | ||
Zip: "94105", | ||
Country: "US", | ||
Phone: "+1 555 341 9393", | ||
Email: "[email protected]", | ||
} | ||
addressTo, err := c.CreateAddress(addressToInput) | ||
if err != nil { | ||
|
@@ -78,11 +76,10 @@ func createShipment(c *client.Client) *models.Shipment { | |
|
||
// create a shipment | ||
shipmentInput := &models.ShipmentInput{ | ||
ObjectPurpose: models.ObjectPurposePurchase, | ||
AddressFrom: addressFrom.ObjectID, | ||
AddressTo: addressTo.ObjectID, | ||
Parcel: parcel.ObjectID, | ||
Async: false, | ||
AddressFrom: addressFrom.ObjectID, | ||
AddressTo: addressTo.ObjectID, | ||
Parcels: []string{parcel.ObjectID}, | ||
Async: false, | ||
} | ||
shipment, err := c.CreateShipment(shipmentInput) | ||
if err != nil { | ||
|
@@ -96,7 +93,7 @@ func createShipment(c *client.Client) *models.Shipment { | |
|
||
func purchaseShippingLabel(c *client.Client, shipment *models.Shipment) { | ||
transactionInput := &models.TransactionInput{ | ||
Rate: shipment.RatesList[len(shipment.RatesList)-1].ObjectID, | ||
Rate: shipment.Rates[len(shipment.Rates)-1].ObjectID, | ||
LabelFileType: models.LabelFileTypePDF, | ||
Async: false, | ||
} | ||
|
Oops, something went wrong.