Skip to content

Commit

Permalink
Merge pull request #115 from plutov/roopakv/fix_auth_capture
Browse files Browse the repository at this point in the history
Fix payment capture API
  • Loading branch information
plutov authored Sep 7, 2019
2 parents c0ae3c0 + fee7108 commit 8e38113
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
18 changes: 7 additions & 11 deletions authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"net/http"
"strconv"
)

// GetAuthorization returns an authorization by ID
Expand All @@ -24,20 +23,17 @@ func (c *Client) GetAuthorization(authID string) (*Authorization, error) {

// CaptureAuthorization captures and process an existing authorization.
// To use this method, the original payment must have Intent set to "authorize"
// Endpoint: POST /v2/payments/authorization/ID/capture
func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture bool) (*Capture, error) {
isFinalStr := strconv.FormatBool(isFinalCapture)

buf := bytes.NewBuffer([]byte(`{"amount":{"currency":"` + a.Currency + `,"total":"` + a.Total + `"},"is_final_capture":` + isFinalStr + `}`))
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorization/"+authID+"/capture"), buf)
capture := &Capture{}
// Endpoint: POST /v2/payments/authorizations/ID/capture
func (c *Client) CaptureAuthorization(authID string, paymentCaptureRequest *PaymentCaptureRequest) (*PaymentCaptureResponse, error) {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/authorizations/"+authID+"/capture"), paymentCaptureRequest)
paymentCaptureResponse := &PaymentCaptureResponse{}

if err != nil {
return capture, err
return paymentCaptureResponse, err
}

err = c.SendWithAuth(req, capture)
return capture, err
err = c.SendWithAuth(req, paymentCaptureResponse)
return paymentCaptureResponse, err
}

// VoidAuthorization voids a previously authorized payment
Expand Down
36 changes: 36 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,42 @@ type (
ApplicationContext ApplicationContext `json:"application_context,omitempty"`
}

// https://developer.paypal.com/docs/api/payments/v2/#definition-platform_fee
PlatformFee struct {
Amount *Money `json:"amount,omitempty"`
Payee *PayeeForOrders `json:"payee,omitempty"`
}

// https://developer.paypal.com/docs/api/payments/v2/#definition-payment_instruction
PaymentInstruction struct {
PlatformFees []PlatformFee `json:"platform_fees,omitempty"`
DisbursementMode string `json:"disbursement_mode,omitempty"`
}

// https://developer.paypal.com/docs/api/payments/v2/#authorizations_capture
PaymentCaptureRequest struct {
InvoiceID string `json:"invoice_id,omitempty"`
NoteToPayer string `json:"note_to_payer,omitempty"`
SoftDescriptor string `json:"soft_descriptor,omitempty"`
Amount *Money `json:"amount,omitempty"`
FinalCapture bool `json:"final_capture,omitempty"`
}

// https://developer.paypal.com/docs/api/payments/v2/#definition-capture_status_details
CaptureStatusDetails struct {
Reason string `json:"reason,omitempty"`
}

PaymentCaptureResponse struct {
Status string `json:"status,omitempty"`
StatusDetails *CaptureStatusDetails `json:"status_details,omitempty"`
ID string `json:"id,omitempty"`
Amount *Money `json:"amount,omitempty"`
InvoiceID string `json:"invoice_id,omitempty"`
FinalCapture bool `json:"final_capture,omitempty"`
DisbursementMode string `json:"disbursement_mode,omitempty"`
}

// CaptureOrderRequest - https://developer.paypal.com/docs/api/orders/v2/#orders_capture
CaptureOrderRequest struct {
PaymentSource *PaymentSource `json:"payment_source"`
Expand Down

0 comments on commit 8e38113

Please sign in to comment.