Skip to content

Commit

Permalink
Update Payment Components. Klarna Payment Context Support (#110)
Browse files Browse the repository at this point in the history
* Update Payment Components

* Add Klarna Payment Context Support
  • Loading branch information
armando-rodriguez-cko authored May 3, 2024
1 parent ed5b46b commit 4971109
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 39 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
}
3 changes: 2 additions & 1 deletion payments/sessions/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package payment_sessions

import (
"github.com/checkout/checkout-sdk-go/payments"
"net/http"
"testing"

Expand Down Expand Up @@ -39,7 +40,7 @@ func TestCreateAPaymentSessions(t *testing.T) {
Amount: 2000,
Currency: common.GBP,
Reference: "ORD-123A",
Billing: &Billing{Address: &common.Address{
Billing: &payments.BillingInformation{Address: &common.Address{
Country: common.GB,
}},
Customer: &common.CustomerRequest{
Expand Down
93 changes: 82 additions & 11 deletions payments/sessions/sessions.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,92 @@
package payment_sessions

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

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

type StorePaymentDetailsType string

const (
Disabled StorePaymentDetailsType = "disabled"
Enabled StorePaymentDetailsType = "enabled"
)

type PaymentMethodsType string

const (
Applepay PaymentMethodsType = "applepay"
Bancontact PaymentMethodsType = "bancontact"
Card PaymentMethodsType = "card"
EPS PaymentMethodsType = "eps"
Giropay PaymentMethodsType = "giropay"
Googlepay PaymentMethodsType = "googlepay"
Ideal PaymentMethodsType = "ideal"
KNet PaymentMethodsType = "knet"
Multibanco PaymentMethodsType = "multibanco"
Przelewy24 PaymentMethodsType = "p24"
Paypal PaymentMethodsType = "paypal"
Sofort PaymentMethodsType = "sofort"
)

const PaymentSessionsPath = "payment-sessions"

type (
CardStorePaymentDetails struct {
StorePaymentDetails StorePaymentDetailsType `json:"store_payment_details,omitempty"`
}

PaymentMethodConfiguration struct {
Card *CardStorePaymentDetails `json:"card,omitempty"`
}

Billing struct {
Address *common.Address `json:"address,omitempty"`
}

PaymentCustomerRequest struct {
Id string `json:"id,omitempty"`
Email string `json:"email,omitempty"`
Name string `json:"name,omitempty"`
TaxNumber string `json:"tax_number,omitempty"`
Phone *common.Phone `json:"phone,omitempty"`
Default bool `json:"default,omitempty"`
}

PaymentSessionsRequest struct {
Amount int64 `json:"amount,omitempty"`
Currency common.Currency `json:"currency,omitempty"`
Reference string `json:"reference,omitempty"`
Billing *Billing `json:"billing,omitempty"`
Customer *common.CustomerRequest `json:"customer,omitempty"`
SuccessUrl string `json:"success_url,omitempty"`
FailureUrl string `json:"failure_url,omitempty"`
Amount int64 `json:"amount,omitempty"`
Currency common.Currency `json:"currency,omitempty"`
PaymentType payments.PaymentType `json:"payment_type,omitempty"`
Billing *payments.BillingInformation `json:"billing,omitempty"`
BillingDescriptor *payments.BillingDescriptor `json:"billing_descriptor,omitempty"`
Reference string `json:"reference,omitempty"`
Description string `json:"description,omitempty"`
Customer *common.CustomerRequest `json:"customer,omitempty"`
Shipping *payments.ShippingDetails `json:"shipping,omitempty"`
Recipient *payments.PaymentRecipient `json:"recipient,omitempty"`
Processing *payments.ProcessingSettings `json:"processing,omitempty"`
ProcessingChannelId string `json:"processing_channel_id,omitempty"`
ExpiresOn *time.Time `json:"expires_on,omitempty"`
PaymentMethodConfiguration *PaymentMethodConfiguration `json:"payment_method_configuration,omitempty"`
EnabledPaymentMethods PaymentMethodsType `json:"enabled_payment_methods,omitempty"`
DisabledPaymentMethods PaymentMethodsType `json:"disabled_payment_methods,omitempty"`
Items []payments.Product `json:"items,omitempty"`
AmountAllocations []common.AmountAllocations `json:"amount_allocations,omitempty"`
Risk *payments.RiskRequest `json:"risk,omitempty"`
CustomerRetry *payments.PaymentRetryRequest `json:"customer_retry,omitempty"`
DisplayName string `json:"display_name,omitempty"`
SuccessUrl string `json:"success_url,omitempty"`
FailureUrl string `json:"failure_url,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
Locale string `json:"locale,omitempty"`
ThreeDsRequest *payments.ThreeDsRequest `json:"3ds,omitempty"`
Sender *nas.Sender `json:"sender,omitempty"`
Capture bool `json:"capture"`
CaptureOn *time.Time `json:"capture_on,omitempty"`
IpAddress string `json:"ip_address,omitempty"`
}
)

Expand All @@ -27,8 +97,9 @@ type (
}

PaymentSessionsResponse struct {
HttpMetadata common.HttpMetadata
Id string `json:"id,omitempty"`
Links map[string]common.Link `json:"links,omitempty"`
HttpMetadata common.HttpMetadata
Id string `json:"id,omitempty"`
PaymentSessionToken string `json:"payment_session_token,omitempty"`
Links map[string]common.Link `json:"links,omitempty"`
}
)
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
15 changes: 6 additions & 9 deletions test/payment_sessions_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package test

import (
"github.com/checkout/checkout-sdk-go/payments/sessions"
"github.com/checkout/checkout-sdk-go/payments"
"testing"

"github.com/stretchr/testify/assert"

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

var (
paymentSessionsRequest = payment_sessions.PaymentSessionsRequest{
Amount: int64(2000),
Currency: common.GBP,
Reference: "ORD-123A",
Billing: &payment_sessions.Billing{Address: Address()},
Customer: &common.CustomerRequest{
Email: "[email protected]",
Name: "John Smith",
},
Amount: int64(2000),
Currency: common.GBP,
Reference: "ORD-123A",
Billing: &payments.BillingInformation{Address: Address()},
SuccessUrl: "https://example.com/payments/success",
FailureUrl: "https://example.com/payments/fail",
}
Expand Down

0 comments on commit 4971109

Please sign in to comment.