Skip to content

Commit

Permalink
feat: add payment web on post transaction (#51)
Browse files Browse the repository at this point in the history
Co-authored-by: matheus.bezerra <matheus.bezerra@ifood.com.br>
  • Loading branch information
matheusbezerrah and matheusbezerraifood authored Jun 4, 2024
1 parent dc7e633 commit 6b4f828
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 10 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ assessment, err := client.RegisterPayment(&incognia.Payment{
})
```

This method registers a new **web** payment for the given installation and account, returning a `TransactionAssessment`, containing the risk assessment and supporting evidence.

```go
assessment, err := client.RegisterPayment(&incognia.Payment{
SessionToken: "session-token",
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
...
})
```


### Registering Login

This method registers a new login for the given installation and account, returning a `TransactionAssessment`, containing the risk assessment and supporting evidence.
Expand Down
10 changes: 6 additions & 4 deletions incognia.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ type IncogniaClientConfig struct {
}

type Payment struct {
InstallationID string
InstallationID *string
SessionToken *string
AccountID string
ExternalID string
PolicyID string
Expand Down Expand Up @@ -297,16 +298,17 @@ func (c *Client) registerPayment(payment *Payment) (ret *TransactionAssessment,
return nil, ErrMissingPayment
}

if payment.InstallationID == "" {
return nil, ErrMissingInstallationID
if payment.InstallationID == nil && payment.SessionToken == nil {
return nil, ErrMissingInstallationIDOrSessionToken
}

if payment.AccountID == "" {
return nil, ErrMissingAccountID
}

requestBody, err := json.Marshal(postTransactionRequestBody{
InstallationID: &payment.InstallationID,
InstallationID: payment.InstallationID,
SessionToken: payment.SessionToken,
Type: paymentType,
AccountID: payment.AccountID,
PolicyID: payment.PolicyID,
Expand Down
113 changes: 107 additions & 6 deletions incognia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,59 @@ var (
},
},
}
postPaymentWebRequestBodyFixture = &postTransactionRequestBody{
SessionToken: &sessionToken,
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
Type: paymentType,
Addresses: []*TransactionAddress{
{
Type: Billing,
StructuredAddress: &StructuredAddress{
Locale: "locale",
CountryName: "country-name",
CountryCode: "country-code",
State: "state",
City: "city",
Borough: "borough",
Neighborhood: "neighborhood",
Street: "street",
Number: "number",
Complements: "complements",
PostalCode: "postalcode",
},
AddressLine: "address line",
Coordinates: &Coordinates{
Lat: -23.561414,
Lng: -46.6558819,
},
},
},
PaymentValue: &PaymentValue{
Amount: 55.02,
Currency: "BRL",
},
PaymentMethods: []*PaymentMethod{
{
Type: CreditCard,
Identifier: "credit-card-hash-123",
CreditCard: &CardInfo{
Bin: "29282",
LastFourDigits: "2222",
ExpiryYear: "2020",
ExpiryMonth: "10",
},
},
},
}
postPaymentRequestBodyRequiredFieldsFixture = &postTransactionRequestBody{
InstallationID: &installationId,
AccountID: "account-id",
Type: paymentType,
}
paymentFixture = &Payment{
InstallationID: installationId,
InstallationID: &installationId,
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
Expand Down Expand Up @@ -249,19 +295,64 @@ var (
},
},
}
paymentWebFixture = &Payment{
SessionToken: &sessionToken,
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
Addresses: []*TransactionAddress{
{
Type: Billing,
StructuredAddress: &StructuredAddress{
Locale: "locale",
CountryName: "country-name",
CountryCode: "country-code",
State: "state",
City: "city",
Borough: "borough",
Neighborhood: "neighborhood",
Street: "street",
Number: "number",
Complements: "complements",
PostalCode: "postalcode",
},
AddressLine: "address line",
Coordinates: &Coordinates{
Lat: -23.561414,
Lng: -46.6558819,
},
},
},
Value: &PaymentValue{
Amount: 55.02,
Currency: "BRL",
},
Methods: []*PaymentMethod{
{
Type: CreditCard,
Identifier: "credit-card-hash-123",
CreditCard: &CardInfo{
Bin: "29282",
LastFourDigits: "2222",
ExpiryYear: "2020",
ExpiryMonth: "10",
},
},
},
}
paymentFixtureRequiredFields = &Payment{
InstallationID: installationId,
InstallationID: &installationId,
AccountID: "account-id",
}
simplePaymentFixtureWithShouldEval = &Payment{
InstallationID: installationId,
InstallationID: &installationId,
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
Eval: &shouldEval,
}
simplePaymentFixtureWithShouldNotEval = &Payment{
InstallationID: installationId,
InstallationID: &installationId,
AccountID: "account-id",
ExternalID: "external-id",
PolicyID: "policy-id",
Expand Down Expand Up @@ -617,6 +708,16 @@ func (suite *IncogniaTestSuite) TestSuccessRegisterPayment() {
suite.Equal(transactionAssessmentFixture, response)
}

func (suite *IncogniaTestSuite) TestSuccessRegisterPaymentWeb() {
transactionServer := suite.mockPostTransactionsEndpoint(token, postPaymentWebRequestBodyFixture, transactionAssessmentFixture, emptyQueryString)
defer transactionServer.Close()

response, err := suite.client.RegisterPayment(paymentWebFixture)

suite.NoError(err)
suite.Equal(transactionAssessmentFixture, response)
}

func (suite *IncogniaTestSuite) TestSuccessRegisterPaymentNilOptional() {
transactionServer := suite.mockPostTransactionsEndpoint(token, postPaymentRequestBodyRequiredFieldsFixture, transactionAssessmentFixture, emptyQueryString)
defer transactionServer.Close()
Expand Down Expand Up @@ -651,12 +752,12 @@ func (suite *IncogniaTestSuite) TestRegisterPaymentNilPayment() {

func (suite *IncogniaTestSuite) TestRegisterPaymentEmptyInstallationId() {
response, err := suite.client.RegisterPayment(&Payment{AccountID: "some-account-id"})
suite.EqualError(err, ErrMissingInstallationID.Error())
suite.EqualError(err, ErrMissingInstallationIDOrSessionToken.Error())
suite.Nil(response)
}

func (suite *IncogniaTestSuite) TestRegisterPaymentEmptyAccountId() {
response, err := suite.client.RegisterPayment(&Payment{InstallationID: "some-installation-id"})
response, err := suite.client.RegisterPayment(&Payment{InstallationID: &installationId})
suite.EqualError(err, ErrMissingAccountID.Error())
suite.Nil(response)
}
Expand Down

0 comments on commit 6b4f828

Please sign in to comment.