diff --git a/README.md b/README.md index 93ad9eb..c730821 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/incognia.go b/incognia.go index 4bd0163..ef9e3a4 100644 --- a/incognia.go +++ b/incognia.go @@ -55,7 +55,8 @@ type IncogniaClientConfig struct { } type Payment struct { - InstallationID string + InstallationID *string + SessionToken *string AccountID string ExternalID string PolicyID string @@ -297,8 +298,8 @@ 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 == "" { @@ -306,7 +307,8 @@ func (c *Client) registerPayment(payment *Payment) (ret *TransactionAssessment, } requestBody, err := json.Marshal(postTransactionRequestBody{ - InstallationID: &payment.InstallationID, + InstallationID: payment.InstallationID, + SessionToken: payment.SessionToken, Type: paymentType, AccountID: payment.AccountID, PolicyID: payment.PolicyID, diff --git a/incognia_test.go b/incognia_test.go index e27660c..e9d58f8 100644 --- a/incognia_test.go +++ b/incognia_test.go @@ -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", @@ -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", @@ -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() @@ -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) }