Skip to content

Commit

Permalink
Add Context to NAS Payments
Browse files Browse the repository at this point in the history
  • Loading branch information
jamieaitken authored and armando-rodriguez-cko committed Nov 3, 2023
1 parent 186d755 commit 7c7414c
Showing 1 changed file with 63 additions and 8 deletions.
71 changes: 63 additions & 8 deletions payments/nas/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nas

import (
"context"

"github.com/checkout/checkout-sdk-go/client"
"github.com/checkout/checkout-sdk-go/common"
"github.com/checkout/checkout-sdk-go/configuration"
Expand All @@ -20,13 +22,18 @@ func NewClient(configuration *configuration.Configuration, apiClient client.Http
}

func (c *Client) RequestPayment(request PaymentRequest, idempotencyKey *string) (*PaymentResponse, error) {
return c.RequestPaymentWithContext(context.Background(), request, idempotencyKey)
}

func (c *Client) RequestPaymentWithContext(ctx context.Context, request PaymentRequest, idempotencyKey *string) (*PaymentResponse, error) {
auth, err := c.configuration.Credentials.GetAuthorization(configuration.SecretKeyOrOauth)
if err != nil {
return nil, err
}

var response PaymentResponse
err = c.apiClient.Post(
err = c.apiClient.PostWithContext(
ctx,
common.BuildPath(payments.PathPayments),
auth,
request,
Expand All @@ -41,6 +48,10 @@ func (c *Client) RequestPayment(request PaymentRequest, idempotencyKey *string)
}

func (c *Client) RequestPaymentList(request payments.QueryRequest) (*GetPaymentListResponse, error) {
return c.RequestPaymentListWithContext(context.Background(), request)
}

func (c *Client) RequestPaymentListWithContext(ctx context.Context, request payments.QueryRequest) (*GetPaymentListResponse, error) {
auth, err := c.configuration.Credentials.GetAuthorization(configuration.SecretKeyOrOauth)
if err != nil {
return nil, err
Expand All @@ -52,7 +63,7 @@ func (c *Client) RequestPaymentList(request payments.QueryRequest) (*GetPaymentL
}

var response GetPaymentListResponse
err = c.apiClient.Get(url, auth, &response)
err = c.apiClient.GetWithContext(ctx, url, auth, &response)
if err != nil {
return nil, err
}
Expand All @@ -61,13 +72,18 @@ func (c *Client) RequestPaymentList(request payments.QueryRequest) (*GetPaymentL
}

func (c *Client) RequestPayout(request PayoutRequest, idempotencyKey *string) (*PayoutResponse, error) {
return c.RequestPayoutWithContext(context.Background(), request, idempotencyKey)
}

func (c *Client) RequestPayoutWithContext(ctx context.Context, request PayoutRequest, idempotencyKey *string) (*PayoutResponse, error) {
auth, err := c.configuration.Credentials.GetAuthorization(configuration.SecretKeyOrOauth)
if err != nil {
return nil, err
}

var response PayoutResponse
err = c.apiClient.Post(
err = c.apiClient.PostWithContext(
ctx,
common.BuildPath(payments.PathPayments),
auth,
request,
Expand All @@ -82,13 +98,17 @@ func (c *Client) RequestPayout(request PayoutRequest, idempotencyKey *string) (*
}

func (c *Client) GetPaymentDetails(paymentId string) (*GetPaymentResponse, error) {
return c.GetPaymentDetailsWithContext(context.Background(), paymentId)
}

func (c *Client) GetPaymentDetailsWithContext(ctx context.Context, paymentId string) (*GetPaymentResponse, error) {
auth, err := c.configuration.Credentials.GetAuthorization(configuration.SecretKeyOrOauth)
if err != nil {
return nil, err
}

var response GetPaymentResponse
err = c.apiClient.Get(common.BuildPath(payments.PathPayments, paymentId), auth, &response)
err = c.apiClient.GetWithContext(ctx, common.BuildPath(payments.PathPayments, paymentId), auth, &response)
if err != nil {
return nil, err
}
Expand All @@ -97,13 +117,18 @@ func (c *Client) GetPaymentDetails(paymentId string) (*GetPaymentResponse, error
}

func (c *Client) GetPaymentActions(paymentId string) (*GetPaymentActionsResponse, error) {
return c.GetPaymentActionsWithContext(context.Background(), paymentId)
}

func (c *Client) GetPaymentActionsWithContext(ctx context.Context, paymentId string) (*GetPaymentActionsResponse, error) {
auth, err := c.configuration.Credentials.GetAuthorization(configuration.SecretKeyOrOauth)
if err != nil {
return nil, err
}

var response GetPaymentActionsResponse
err = c.apiClient.Get(
err = c.apiClient.GetWithContext(
ctx,
common.BuildPath(payments.PathPayments, paymentId, "actions"),
auth,
&response,
Expand Down Expand Up @@ -144,14 +169,24 @@ func (c *Client) CapturePayment(
paymentId string,
captureRequest CaptureRequest,
idempotencyKey *string,
) (*payments.CaptureResponse, error) {
return c.CapturePaymentWithContext(context.Background(), paymentId, captureRequest, idempotencyKey)
}

func (c *Client) CapturePaymentWithContext(
ctx context.Context,
paymentId string,
captureRequest CaptureRequest,
idempotencyKey *string,
) (*payments.CaptureResponse, error) {
auth, err := c.configuration.Credentials.GetAuthorization(configuration.SecretKeyOrOauth)
if err != nil {
return nil, err
}

var response payments.CaptureResponse
err = c.apiClient.Post(
err = c.apiClient.PostWithContext(
ctx,
common.BuildPath(payments.PathPayments, paymentId, "captures"),
auth,
captureRequest,
Expand Down Expand Up @@ -193,14 +228,24 @@ func (c *Client) RefundPayment(
paymentId string,
refundRequest *payments.RefundRequest,
idempotencyKey *string,
) (*payments.RefundResponse, error) {
return c.RefundPaymentWithContext(context.Background(), paymentId, refundRequest, idempotencyKey)
}

func (c *Client) RefundPaymentWithContext(
ctx context.Context,
paymentId string,
refundRequest *payments.RefundRequest,
idempotencyKey *string,
) (*payments.RefundResponse, error) {
auth, err := c.configuration.Credentials.GetAuthorization(configuration.SecretKeyOrOauth)
if err != nil {
return nil, err
}

var response payments.RefundResponse
err = c.apiClient.Post(
err = c.apiClient.PostWithContext(
ctx,
common.BuildPath(payments.PathPayments, paymentId, "refunds"),
auth,
refundRequest,
Expand All @@ -218,14 +263,24 @@ func (c *Client) VoidPayment(
paymentId string,
voidRequest *payments.VoidRequest,
idempotencyKey *string,
) (*payments.VoidResponse, error) {
return c.VoidPaymentWithContext(context.Background(), paymentId, voidRequest, idempotencyKey)
}

func (c *Client) VoidPaymentWithContext(
ctx context.Context,
paymentId string,
voidRequest *payments.VoidRequest,
idempotencyKey *string,
) (*payments.VoidResponse, error) {
auth, err := c.configuration.Credentials.GetAuthorization(configuration.SecretKeyOrOauth)
if err != nil {
return nil, err
}

var response payments.VoidResponse
err = c.apiClient.Post(
err = c.apiClient.PostWithContext(
ctx,
common.BuildPath(payments.PathPayments, paymentId, "voids"),
auth,
voidRequest,
Expand Down

0 comments on commit 7c7414c

Please sign in to comment.