From ebc7c22c7c57a8a1e387733874217263fa0be1ca Mon Sep 17 00:00:00 2001 From: Aliaksandr Pliutau Date: Wed, 17 Feb 2016 11:10:49 +0700 Subject: [PATCH] #7 CreateSinglePayout --- README.md | 25 +++++++++++++++++++ examples/main.go | 25 +++++++++++++++++++ payout.go | 22 +++++++++++++++++ payout_test.go | 32 +++++++++++++++++++++++++ types.go | 62 +++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 payout.go create mode 100644 payout_test.go diff --git a/README.md b/README.md index dbbda9d..7ca9447 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ * POST /v1/payments/orders/**ID**/do-void * POST /v1/identity/openidconnect/tokenservice * GET /v1/identity/openidconnect/userinfo/?schema=**SCHEMA** + * POST /v1/payments/payouts?sync_mode=true #### Missing endpoints It is possible that some endpoints are missing in this SDK Client, but you can use built-in **paypalsdk** functions to perform a request: **NewClient -> NewRequest -> SendWithAuth** @@ -207,6 +208,30 @@ token, err := c.GrantNewAccessTokenFromRefreshToken("") userInfo, err := c.GetUserInfo("openid") ``` +#### Create single payout to email + +```go +payout := paypalsdk.Payout{ + SenderBatchHeader: &paypalsdk.SenderBatchHeader{ + EmailSubject: "Subject will be displayed on PayPal", + }, + Items: []paypalsdk.PayoutItem{ + paypalsdk.PayoutItem{ + RecipientType: "EMAIL", + Receiver: "single-email-payout@mail.com", + Amount: &paypalsdk.AmountPayout{ + Value: "15.11", + Currency: "USD", + }, + Note: "Optional note", + SenderItemID: "Optional Item ID", + }, + }, +} + +payoutResp, err := c.CreateSinglePayout(payout) +``` + #### How to Contribute * Fork a repository diff --git a/examples/main.go b/examples/main.go index 794a905..da31d1c 100644 --- a/examples/main.go +++ b/examples/main.go @@ -144,4 +144,29 @@ func main() { } else { fmt.Println("ERROR: " + err.Error()) } + + payout := paypalsdk.Payout{ + SenderBatchHeader: &paypalsdk.SenderBatchHeader{ + EmailSubject: "Subject will be displayed on PayPal", + }, + Items: []paypalsdk.PayoutItem{ + paypalsdk.PayoutItem{ + RecipientType: "EMAIL", + Receiver: "single-email-payout@mail.com", + Amount: &paypalsdk.AmountPayout{ + Value: "15.11", + Currency: "USD", + }, + Note: "Optional note", + SenderItemID: "Optional Item ID", + }, + }, + } + + payoutResp, err := c.CreateSinglePayout(payout) + if err == nil && len(payoutResp.Items) > 0 { + fmt.Println("DEBUG: PayoutItemID=" + payoutResp.Items[0].PayoutItemID) + } else { + fmt.Println("ERROR: " + err.Error()) + } } diff --git a/payout.go b/payout.go new file mode 100644 index 0000000..13f52ec --- /dev/null +++ b/payout.go @@ -0,0 +1,22 @@ +package paypalsdk + +import "fmt" + +// CreateSinglePayout submits a payout with a synchronous API call, which immediately returns the results of a PayPal payment. +// For email payout set RecipientType: "EMAIL" and receiver email into Receiver +// Endpoint: POST /v1/payments/payouts?sync_mode=true +func (c *Client) CreateSinglePayout(p Payout) (*PayoutResponse, error) { + req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payouts?sync_mode=true"), p) + if err != nil { + return &PayoutResponse{}, err + } + + response := &PayoutResponse{} + + err = c.SendWithAuth(req, response) + if err != nil { + return response, err + } + + return response, nil +} diff --git a/payout_test.go b/payout_test.go new file mode 100644 index 0000000..549edd2 --- /dev/null +++ b/payout_test.go @@ -0,0 +1,32 @@ +package paypalsdk + +import "testing" + +func TestCreateSinglePayout(t *testing.T) { + c, _ := NewClient(testClientID, testSecret, APIBaseSandBox) + c.GetAccessToken() + + payout := Payout{ + SenderBatchHeader: &SenderBatchHeader{ + EmailSubject: "Subject will be displayed on PayPal", + }, + Items: []PayoutItem{ + PayoutItem{ + RecipientType: "EMAIL", + Receiver: "single-email-payout@mail.com", + Amount: &AmountPayout{ + Value: "15.11", + Currency: "USD", + }, + Note: "Optional note", + SenderItemID: "Optional Item ID", + }, + }, + } + + p, err := c.CreateSinglePayout(payout) + + if err != nil || len(p.Items) != 1 { + t.Errorf("Test single payout is not created") + } +} diff --git a/types.go b/types.go index ae7d30d..dabcac0 100644 --- a/types.go +++ b/types.go @@ -32,6 +32,12 @@ type ( Total string `json:"total"` } + // AmountPayout https://developer.paypal.com/docs/integration/direct/create-single-payout/ + AmountPayout struct { + Currency string `json:"currency"` + Value string `json:"value"` + } + // Authorization rhttps://developer.paypal.com/webapps/developer/docs/api/#authorization-object Authorization struct { Amount *Amount `json:"amount,omitempty"` @@ -47,6 +53,17 @@ type ( ProtectionEligibilityType string `json:"protection_eligibility_type,omitempty"` } + // BatchHeader https://developer.paypal.com/docs/integration/direct/create-single-payout/ + BatchHeader struct { + Amount *AmountPayout `json:"amount,omitempty"` + Fees *AmountPayout `json:"fees,omitempty"` + PayoutBatchID string `json:"payout_batch_id,omitempty"` + BatchStatus string `json:"batch_status,omitempty"` + TimeCreated *time.Time `json:"time_created,omitempty"` + TimeCompleted *time.Time `json:"time_completed,omitempty"` + SenderBatchHeader *SenderBatchHeader `json:"sender_batch_header,omitempty"` + } + // Capture https://developer.paypal.com/webapps/developer/docs/api/#capture-object Capture struct { Amount *Amount `json:"amount,omitempty"` @@ -149,9 +166,9 @@ type ( // Links https://developer.paypal.com/webapps/developer/docs/api/#itemlist-object Links struct { Href string `json:"href"` - Rel string `json:"rel"` - Method string `json:"method"` - Enctype string `json:"enctype"` + Rel string `json:"rel,omitempty"` + Method string `json:"method,omitempty"` + Enctype string `json:"enctype,omitempty"` } // Order https://developer.paypal.com/webapps/developer/docs/api/#order-object @@ -211,6 +228,40 @@ type ( Links []PaymentLink `json:"links"` } + // Payout https://developer.paypal.com/docs/integration/direct/create-single-payout/ + Payout struct { + SenderBatchHeader *SenderBatchHeader `json:"sender_batch_header"` + Items []PayoutItem `json:"items"` + } + + // PayoutItem https://developer.paypal.com/docs/integration/direct/create-single-payout/ + PayoutItem struct { + RecipientType string `json:"recipient_type"` + Receiver string `json:"receiver"` + Amount *AmountPayout `json:"amount"` + Note string `json:"note,omitempty"` + SenderItemID string `json:"sender_item_id,omitempty"` + } + + // PayoutItemResponse https://developer.paypal.com/docs/integration/direct/create-single-payout/ + PayoutItemResponse struct { + PayoutItemID string `json:"payout_item_id"` + TransactionID string `json:"transaction_id"` + TransactionStatus string `json:"transaction_status"` + PayoutBatchID string `json:"payout_batch_id,omitempty"` + PayoutItemFee *AmountPayout `json:"payout_item_fee,omitempty"` + PayoutItem *PayoutItem `json:"payout_item"` + TimeProcessed *time.Time `json:"time_processed,omitempty"` + Links []Links `json:"links"` + } + + // PayoutResponse https://developer.paypal.com/docs/integration/direct/create-single-payout/ + PayoutResponse struct { + BatchHeader *BatchHeader `json:"batch_header"` + Items []PayoutItemResponse `json:"items"` + Links []Links `json:"links"` + } + // RedirectURLs https://developer.paypal.com/webapps/developer/docs/api/#redirecturls-object RedirectURLs struct { ReturnURL string `json:"return_url,omitempty"` @@ -246,6 +297,11 @@ type ( Links []Links `json:"links,omitempty"` } + // SenderBatchHeader https://developer.paypal.com/docs/integration/direct/create-single-payout/ + SenderBatchHeader struct { + EmailSubject string `json:"email_subject"` + } + // ShippingAddress https://developer.paypal.com/webapps/developer/docs/api/#shippingaddredd-object ShippingAddress struct { RecipientName string `json:"recipient_name,omitempty"`