-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(paypal): add tracking number api
- Loading branch information
Showing
4 changed files
with
127 additions
and
2 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
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package paypal | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-pay/gopay" | ||
) | ||
|
||
// AddTrackingNumber 添加物流单号 | ||
// Code = 0 is success | ||
// 文档:https://developer.paypal.com/docs/api/orders/v2/#orders_track_create | ||
func (c *Client) AddTrackingNumber(ctx context.Context, orderId string, bm gopay.BodyMap) (ppRsp *AddTrackingNumberRsp, err error) { | ||
if err = bm.CheckEmptyError("tracking_number", "carrier", "capture_id"); err != nil { | ||
return nil, err | ||
} | ||
|
||
url := fmt.Sprintf(addTrackingNumber, orderId) | ||
res, bs, err := c.doPayPalPost(ctx, bm, url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ppRsp = &AddTrackingNumberRsp{Code: Success} | ||
ppRsp.Response = new(OrderDetail) | ||
if err = json.Unmarshal(bs, ppRsp.Response); err != nil { | ||
return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs)) | ||
} | ||
if res.StatusCode != http.StatusCreated { | ||
ppRsp.Code = res.StatusCode | ||
ppRsp.Error = string(bs) | ||
ppRsp.ErrorResponse = new(ErrorResponse) | ||
_ = json.Unmarshal(bs, ppRsp.ErrorResponse) | ||
} | ||
return ppRsp, 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package paypal | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-pay/xlog" | ||
|
||
"github.com/go-pay/gopay" | ||
) | ||
|
||
func TestAddTrackingNumber(t *testing.T) { | ||
var items []*ShipItem | ||
var item = &ShipItem{ | ||
Name: "T-Shirt", | ||
Quantity: 1, | ||
Sku: "sku02", | ||
Url: "https://www.example.com/example.jpg", | ||
ImageUrl: "https://www.example.com/example", | ||
} | ||
items = append(items, item) | ||
|
||
bm := make(gopay.BodyMap) | ||
bm.Set("capture_id", "1DW71051X94135205"). | ||
Set("tracking_number", "UJ639398620YP"). | ||
Set("carrier", "YANWEN"). | ||
Set("items", items) | ||
|
||
xlog.Debug("bm:", bm.JsonBody()) | ||
|
||
ppRsp, err := client.AddTrackingNumber(ctx, "3TH70640XJ5862H", bm) | ||
if err != nil { | ||
xlog.Error(err) | ||
return | ||
} | ||
if ppRsp.Code != Success { | ||
xlog.Debugf("ppRsp.Code: %+v", ppRsp.Code) | ||
xlog.Debugf("ppRsp.Error: %+v", ppRsp.Error) | ||
xlog.Debugf("ppRsp.ErrorResponse: %+v", ppRsp.ErrorResponse) | ||
return | ||
} | ||
xlog.Debugf("ppRsp.Response: %+v", ppRsp.Response) | ||
for _, v := range ppRsp.Response.Links { | ||
xlog.Debugf("ppRsp.Response.Links: %+v", v) | ||
} | ||
} |