From eeba3fbf2650b069277b2a55f1bf774023367dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Rodr=C3=ADguez?= <127134616+armando-rodriguez-cko@users.noreply.github.com> Date: Thu, 1 Aug 2024 20:21:05 +0200 Subject: [PATCH] Fix account holder payment context source --- common/common.go | 5 +- payments/nas/sources/contexts/contexts.go | 3 +- test/deserializer_test.go | 41 +++++++++++++++ ...yment_context_paypal_details_response.json | 50 +++++++++++++++++++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 test/resources/payment_context_paypal_details_response.json diff --git a/common/common.go b/common/common.go index 88b20ea..4c6cf5a 100644 --- a/common/common.go +++ b/common/common.go @@ -182,9 +182,12 @@ type ( AccountHolder struct { Type AccountHolderType `json:"type,omitempty"` Title string `json:"title,omitempty"` + FullName string `json:"full_name,omitempty"` FirstName string `json:"first_name,omitempty"` MiddleName string `json:"middle_name,omitempty"` LastName string `json:"last_name,omitempty"` + Email string `json:"email,omitempty"` + Gender string `json:"gender,omitempty"` CompanyName string `json:"company_name,omitempty"` TaxId string `json:"tax_id,omitempty"` DateOfBirth string `json:"date_of_birth,omitempty"` @@ -193,8 +196,6 @@ type ( BillingAddress *Address `json:"billing_address,omitempty"` Phone *Phone `json:"phone,omitempty"` Identification *AccountHolderIdentification `json:"identification,omitempty"` - Email string `json:"email,omitempty"` - Gender string `json:"gender,omitempty"` } ) diff --git a/payments/nas/sources/contexts/contexts.go b/payments/nas/sources/contexts/contexts.go index 4a60b80..f88d77d 100644 --- a/payments/nas/sources/contexts/contexts.go +++ b/payments/nas/sources/contexts/contexts.go @@ -12,7 +12,8 @@ type ( } requestPaymentContextsPayPalSource struct { - Type payments.SourceType `json:"type,omitempty"` + Type payments.SourceType `json:"type,omitempty"` + AccountHolder *common.AccountHolder `json:"account_holder,omitempty"` } ) diff --git a/test/deserializer_test.go b/test/deserializer_test.go index 3f1e46d..0150450 100644 --- a/test/deserializer_test.go +++ b/test/deserializer_test.go @@ -107,3 +107,44 @@ func TestControlsUnmarshallJson(t *testing.T) { }) } } + +func TestPaymentContextUnmarshallJson(t *testing.T) { + paypalResponse, _ := ioutil.ReadFile("resources/payment_context_paypal_details_response.json") + + cases := []struct { + name string + json []byte + checker func(*bytes.Buffer, error) + }{ + { + name: "when deserializing payment_context_paypal_details_response type must be PayPal", + json: paypalResponse, + checker: func(serialized *bytes.Buffer, err error) { + assert.Nil(t, err) + assert.NotNil(t, serialized) + + var deserialized map[string]interface{} + unmErr := json.Unmarshal(serialized.Bytes(), &deserialized) + + assert.Nil(t, unmErr) + assert.NotNil(t, deserialized["payment_request"]) + paymentRequest := deserialized["payment_request"].(map[string]interface{}) + source := paymentRequest["source"].(map[string]interface{}) + + assert.Equal(t, "paypal", source["type"]) + assert.Contains(t, source, "type") + assert.Contains(t, source, "account_holder") + + accountHolder := source["account_holder"].(map[string]interface{}) + assert.Contains(t, accountHolder, "full_name") + assert.Equal(t, "Andrey Young", accountHolder["full_name"]) + }, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + tc.checker(bytes.NewBuffer(tc.json), nil) + }) + } +} diff --git a/test/resources/payment_context_paypal_details_response.json b/test/resources/payment_context_paypal_details_response.json new file mode 100644 index 0000000..b0375fe --- /dev/null +++ b/test/resources/payment_context_paypal_details_response.json @@ -0,0 +1,50 @@ +{ + "partner_metadata": { + "customer_id": "EWR4HCRWOEIW4", + "order_id": "NF89G5J9E5G905E90" + }, + "payment_request": { + "amount": 2500, + "authorization_type": "Final", + "capture": true, + "currency": "GBP", + "failure_url": "https://url/payment-failed", + "items": [ + { + "discount_amount": 0, + "name": "£25.00 Voucher", + "quantity": 1, + "tax_amount": 0, + "total_amount": 2500, + "unit_price": 2500 + } + ], + "payment_type": "Regular", + "processing": { + "shipping_amount": 0, + "user_action": "PAY_NOW" + }, + "processing_channel_id": "pc_strugfrty47ellyymdfg6fzhc4i", + "reference": "543454", + "shipping": { + "address": { + "address_line1": "Main Address", + "address_line2": "Flor 1", + "city": "London", + "country": "GB", + "state": "State", + "zip": "ZIP" + }, + "first_name": "Andrey Young" + }, + "source": { + "account_holder": { + "email": "ayoung@fakedomain.com", + "full_name": "Andrey Young" + }, + "type": "paypal" + }, + "success_url": "https://url/payment-successful" + }, + "status": "Created" +} \ No newline at end of file