Skip to content

Commit

Permalink
Add Klarna Payment Context Support
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-rodriguez-cko committed May 2, 2024
1 parent f82bfe3 commit 87bd275
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 18 deletions.
2 changes: 1 addition & 1 deletion payments/contexts/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestCreateAPaymentContext(t *testing.T) {
{
name: "when request is correct then create a payment context",
request: PaymentContextsRequest{
Source: contexts.NewPaymentContextsPaypalSource(),
Source: contexts.NewPaymentContextsPayPalSource(),
Amount: 2000,
Currency: common.EUR,
PaymentType: payments.Regular,
Expand Down
8 changes: 8 additions & 0 deletions payments/contexts/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
"github.com/checkout/checkout-sdk-go/payments/nas/sources/apm"
)

type PaymentContextDetailsStatusType string

const (
Created PaymentContextDetailsStatusType = "Created"
Approved PaymentContextDetailsStatusType = "Approved"
)

const PaymentContextsPath = "payment-contexts"

type (
Expand Down Expand Up @@ -126,6 +133,7 @@ type (

PaymentContextDetailsResponse struct {
HttpMetadata common.HttpMetadata
Status PaymentContextDetailsStatusType `json:"status,omitempty"`
PaymentRequest *PaymentContextsResponse `json:"payment_request,omitempty"`
PartnerMetadata *PaymentContextsPartnerMetadata `json:"partner_metadata,omitempty"`
}
Expand Down
26 changes: 21 additions & 5 deletions payments/nas/sources/contexts/contexts.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
package contexts

import "github.com/checkout/checkout-sdk-go/payments"
import (
"github.com/checkout/checkout-sdk-go/common"
"github.com/checkout/checkout-sdk-go/payments"
)

type (
requestPaymentContextsPaypalSource struct {
requestPaymentContextsKlarnaSource struct {
Type payments.SourceType `json:"type,omitempty"`
AccountHolder *common.AccountHolder `json:"account_holder,omitempty"`
}

requestPaymentContextsPayPalSource struct {
Type payments.SourceType `json:"type,omitempty"`
}
)

func NewPaymentContextsPaypalSource() *requestPaymentContextsPaypalSource {
return &requestPaymentContextsPaypalSource{Type: payments.PayPalSource}
func NewPaymentContextsKlarnaSource() *requestPaymentContextsKlarnaSource {
return &requestPaymentContextsKlarnaSource{Type: payments.KlarnaSource}
}

func NewPaymentContextsPayPalSource() *requestPaymentContextsPayPalSource {
return &requestPaymentContextsPayPalSource{Type: payments.PayPalSource}
}

func (s *requestPaymentContextsKlarnaSource) GetType() payments.SourceType {
return s.Type
}

func (s *requestPaymentContextsPaypalSource) GetType() payments.SourceType {
func (s *requestPaymentContextsPayPalSource) GetType() payments.SourceType {
return s.Type
}
84 changes: 72 additions & 12 deletions test/payment_context_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package test

import (
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/checkout/checkout-sdk-go/common"
"github.com/checkout/checkout-sdk-go/errors"
"github.com/checkout/checkout-sdk-go/payments"
"github.com/checkout/checkout-sdk-go/payments/contexts"
sources "github.com/checkout/checkout-sdk-go/payments/nas/sources/contexts"
)

var (
paymentContextSource = sources.NewPaymentContextsPaypalSource()
paymentContextPayPalSource = sources.NewPaymentContextsPayPalSource()

paymentContextKlarnaSource = getKlarnaPaymentContextsSource()

item = contexts.PaymentContextsItems{
Name: "mask",
Quantity: 1,
UnitPrice: 2000,
Name: "mask",
Quantity: 1,
UnitPrice: 1000,
TotalAmount: 1000,
}

paymentContextsRequest = contexts.PaymentContextsRequest{
Source: paymentContextSource,
Amount: 2000,
paymentContextsPayPalRequest = contexts.PaymentContextsRequest{
Source: paymentContextPayPalSource,
Amount: 1000,
Currency: common.EUR,
PaymentType: payments.Regular,
Capture: true,
Expand All @@ -34,17 +39,33 @@ var (
item,
},
}

processing = contexts.PaymentContextsProcessing{
Locale: "en-GB",
}

paymentContextsKlarnaRequest = contexts.PaymentContextsRequest{
Source: paymentContextKlarnaSource,
Amount: 1000,
Currency: common.EUR,
PaymentType: payments.Regular,
ProcessingChannelId: os.Getenv("CHECKOUT_PROCESSING_CHANNEL_ID"),
Items: []contexts.PaymentContextsItems{
item,
},
Processing: &processing,
}
)

func TestRequestPaymentContext(t *testing.T) {
func TestRequestPaymentContextPayPal(t *testing.T) {
cases := []struct {
name string
request contexts.PaymentContextsRequest
checker func(response *contexts.PaymentContextsRequestResponse, err error)
}{
{
name: "when payment context is valid the return a response",
request: paymentContextsRequest,
request: paymentContextsPayPalRequest,
checker: func(response *contexts.PaymentContextsRequestResponse, err error) {
assert.Nil(t, err)
assert.NotNil(t, response)
Expand All @@ -67,9 +88,48 @@ func TestRequestPaymentContext(t *testing.T) {

}

func TestRequestPaymentContextKlarna(t *testing.T) {
cases := []struct {
name string
request contexts.PaymentContextsRequest
checker func(response *contexts.PaymentContextsRequestResponse, err error)
}{
{
name: "test Klarna source for request payment contexts",
request: paymentContextsKlarnaRequest,
checker: func(response *contexts.PaymentContextsRequestResponse, err error) {
assert.NotNil(t, err)
assert.Nil(t, response)
ckoErr := err.(errors.CheckoutAPIError)
assert.Equal(t, http.StatusUnprocessableEntity, ckoErr.StatusCode)
assert.Equal(t, "apm_service_unavailable", ckoErr.Data.ErrorCodes[0])
},
},
}

client := DefaultApi().Contexts

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tc.checker(client.RequestPaymentContexts(tc.request))
})
}
}

func getKlarnaPaymentContextsSource() payments.PaymentSource {
source := sources.NewPaymentContextsKlarnaSource()
source.AccountHolder = &common.AccountHolder{
BillingAddress: &common.Address{
Country: common.DE,
},
}

return source
}

func makePaymentContextRequest(t *testing.T) *contexts.PaymentContextsRequestResponse {

response, err := DefaultApi().Contexts.RequestPaymentContexts(paymentContextsRequest)
response, err := DefaultApi().Contexts.RequestPaymentContexts(paymentContextsPayPalRequest)

assert.Nil(t, err)
assert.NotNil(t, response)
Expand All @@ -96,13 +156,13 @@ func TestGetPaymentContext(t *testing.T) {
checker: func(response *contexts.PaymentContextDetailsResponse, err error) {
assert.Nil(t, err)
assert.NotNil(t, response)
assert.Equal(t, int64(2000), response.PaymentRequest.Amount)
assert.Equal(t, int64(1000), response.PaymentRequest.Amount)
assert.Equal(t, common.EUR, response.PaymentRequest.Currency)
assert.Equal(t, payments.Regular, response.PaymentRequest.PaymentType)
assert.Equal(t, true, response.PaymentRequest.Capture)
assert.Equal(t, "mask", response.PaymentRequest.Items[0].Name)
assert.Equal(t, 1, response.PaymentRequest.Items[0].Quantity)
assert.Equal(t, 2000, response.PaymentRequest.Items[0].UnitPrice)
assert.Equal(t, 1000, response.PaymentRequest.Items[0].UnitPrice)
assert.Equal(t, "https://example.com/payments/success", response.PaymentRequest.SuccessUrl)
assert.Equal(t, "https://example.com/payments/fail", response.PaymentRequest.FailureUrl)
assert.NotNil(t, response.PartnerMetadata)
Expand Down

0 comments on commit 87bd275

Please sign in to comment.