Fix payment capture

This commit is contained in:
Roopak Venkatakrishnan 2019-09-06 08:00:21 -07:00
parent c0ae3c0d06
commit fee7108fae
No known key found for this signature in database
GPG Key ID: 01FD0BAB58C09E04
2 changed files with 43 additions and 11 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"net/http"
"strconv"
)
// GetAuthorization returns an authorization by ID
@ -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

View File

@ -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"`