diff --git a/webhook/mccy_transactions.go b/webhook/mccy_transactions.go index 541eaab..31771eb 100644 --- a/webhook/mccy_transactions.go +++ b/webhook/mccy_transactions.go @@ -30,6 +30,8 @@ type WebhookMCTransactionCreatedPayload struct { AdditionalProperties []AdditionalProperty `json:"AdditionalProperties" validate:"required"` } +var _ Transaction = (*WebhookMCTransactionSettledPayload)(nil) + // WebhookMCTransactionSettledPayload // This webhook confirms that a multicurrency transaction has been settled type WebhookMCTransactionSettledPayload struct { @@ -52,6 +54,66 @@ type WebhookMCTransactionSettledPayload struct { AdditionalProperties []AdditionalProperty `json:"AdditionalProperties" validate:"required"` } +func (w WebhookMCTransactionSettledPayload) GetID() uuid.UUID { + return w.TransactionID +} + +func (w WebhookMCTransactionSettledPayload) GetEndToEndID() string { + return w.EndToEndID +} + +func (w WebhookMCTransactionSettledPayload) GetCurrency() string { + return w.InstructedCurrency +} + +func (w WebhookMCTransactionSettledPayload) GetAmount() float64 { + return w.InstructedAmount +} + +func (w WebhookMCTransactionSettledPayload) IsReturn() bool { + return false +} + +func (w WebhookMCTransactionSettledPayload) GetReference() string { + return w.Reference +} + +func (w WebhookMCTransactionSettledPayload) GetAccountIdentifier() string { + if len(w.UltimateCreditorAccountIdentifiers) == 0 { + return "" + } + if w.InstructedAmount > 0 { + return w.UltimateCreditorAccountIdentifiers[0].Identifier + } + return w.UltimateDebtorAccountIdentifiers[0].Identifier +} + +func (w WebhookMCTransactionSettledPayload) GetAccountOwner() string { + if w.InstructedAmount > 0 { + return w.UltimateCreditorName + } + return w.UltimateDebtorName +} + +func (w WebhookMCTransactionSettledPayload) GetCounterpartAccountIdentifier() string { + if len(w.UltimateCreditorAccountIdentifiers) == 0 { + return "" + } + if w.InstructedAmount > 0 { + return w.UltimateDebtorAccountIdentifiers[0].Identifier + } + return w.UltimateCreditorAccountIdentifiers[0].Identifier +} + +func (w WebhookMCTransactionSettledPayload) GetCounterpartAccountOwner() string { + if w.InstructedAmount > 0 { + return w.UltimateDebtorName + } + return w.UltimateCreditorName +} + +var _ Transaction = (*WebhookMCTransactionCancelledPayload)(nil) + // WebhookMCTransactionCancelledPayload // This webhook confirms that a multicurrency transaction has been canceled type WebhookMCTransactionCancelledPayload struct { @@ -74,6 +136,64 @@ type WebhookMCTransactionCancelledPayload struct { CancellationReason string `json:"CancellationReason" validate:"required"` } +func (w WebhookMCTransactionCancelledPayload) GetID() uuid.UUID { + return w.TransactionID +} + +func (w WebhookMCTransactionCancelledPayload) GetEndToEndID() string { + return w.EndToEndID +} + +func (w WebhookMCTransactionCancelledPayload) GetCurrency() string { + return w.InstructedCurrency +} + +func (w WebhookMCTransactionCancelledPayload) GetAmount() float64 { + return w.InstructedAmount +} + +func (w WebhookMCTransactionCancelledPayload) IsReturn() bool { + return false +} + +func (w WebhookMCTransactionCancelledPayload) GetReference() string { + return w.Reference +} + +func (w WebhookMCTransactionCancelledPayload) GetAccountIdentifier() string { + if len(w.UltimateCreditorAccountIdentifiers) == 0 { + return "" + } + if w.InstructedAmount > 0 { + return w.UltimateCreditorAccountIdentifiers[0].Identifier + } + return w.UltimateDebtorAccountIdentifiers[0].Identifier +} + +func (w WebhookMCTransactionCancelledPayload) GetAccountOwner() string { + if w.InstructedAmount > 0 { + return w.UltimateCreditorName + } + return w.UltimateDebtorName +} + +func (w WebhookMCTransactionCancelledPayload) GetCounterpartAccountIdentifier() string { + if len(w.UltimateCreditorAccountIdentifiers) == 0 { + return "" + } + if w.InstructedAmount > 0 { + return w.UltimateDebtorAccountIdentifiers[0].Identifier + } + return w.UltimateCreditorAccountIdentifiers[0].Identifier +} + +func (w WebhookMCTransactionCancelledPayload) GetCounterpartAccountOwner() string { + if w.InstructedAmount > 0 { + return w.UltimateDebtorName + } + return w.UltimateCreditorName +} + // WebhookMCPayloadAssessmentFailedPayload // This webhook confirms that a multicurrency payment has failed assessment type WebhookMCPayloadAssessmentFailedPayload struct { diff --git a/webhook/transactions.go b/webhook/transactions.go index fd91d2a..2734506 100644 --- a/webhook/transactions.go +++ b/webhook/transactions.go @@ -36,6 +36,8 @@ type TransactionAccount struct { InstitutionName string `json:"InstitutionName"` } +var _ Transaction = (*WebhookTransactionSettledPayload)(nil) + // WebhookTransactionSettledPayload // This sends a webhook notification confirming the transaction has settled type WebhookTransactionSettledPayload struct { @@ -66,6 +68,51 @@ type WebhookTransactionSettledPayload struct { } `json:"SupplementaryData"` } +func (w WebhookTransactionSettledPayload) GetID() uuid.UUID { + return w.TransactionID +} + +func (w WebhookTransactionSettledPayload) GetEndToEndID() string { + return w.EndToEndTransactionID +} + +func (w WebhookTransactionSettledPayload) GetCurrency() string { + return w.CurrencyCode +} + +func (w WebhookTransactionSettledPayload) GetAmount() float64 { + if w.DebitCreditCode == DebitCreditCodeDebit { + return -w.Amount + } + return w.Amount +} + +func (w WebhookTransactionSettledPayload) IsReturn() bool { + return w.Return +} + +func (w WebhookTransactionSettledPayload) GetReference() string { + return w.Reference +} + +func (w WebhookTransactionSettledPayload) GetAccountIdentifier() string { + return w.Account.IBAN +} + +func (w WebhookTransactionSettledPayload) GetAccountOwner() string { + return w.Account.OwnerName +} + +func (w WebhookTransactionSettledPayload) GetCounterpartAccountIdentifier() string { + return w.CounterpartAccount.IBAN +} + +func (w WebhookTransactionSettledPayload) GetCounterpartAccountOwner() string { + return w.CounterpartAccount.TransactionOwnerName +} + +var _ Transaction = (*WebhookTransactionRejectedPayload)(nil) + // WebhookTransactionRejectedPayload // This webhook confirms the payment has been rejected type WebhookTransactionRejectedPayload struct { @@ -85,6 +132,49 @@ type WebhookTransactionRejectedPayload struct { CounterpartAccount TransactionAccount `json:"CounterpartAccount" validate:"required"` } +func (w WebhookTransactionRejectedPayload) GetID() uuid.UUID { + return w.TransactionID +} + +func (w WebhookTransactionRejectedPayload) GetEndToEndID() string { + return w.EndToEndTransactionID +} + +func (w WebhookTransactionRejectedPayload) GetCurrency() string { + return w.CurrencyCode +} + +func (w WebhookTransactionRejectedPayload) GetAmount() float64 { + if w.DebitCreditCode == DebitCreditCodeDebit { + return -w.Amount + } + return w.Amount +} + +func (w WebhookTransactionRejectedPayload) IsReturn() bool { + return w.Return +} + +func (w WebhookTransactionRejectedPayload) GetReference() string { + return w.Reference +} + +func (w WebhookTransactionRejectedPayload) GetAccountIdentifier() string { + return w.Account.IBAN +} + +func (w WebhookTransactionRejectedPayload) GetAccountOwner() string { + return w.Account.OwnerName +} + +func (w WebhookTransactionRejectedPayload) GetCounterpartAccountIdentifier() string { + return w.CounterpartAccount.IBAN +} + +func (w WebhookTransactionRejectedPayload) GetCounterpartAccountOwner() string { + return w.CounterpartAccount.TransactionOwnerName +} + // WebhookPaymentMessageAssesmentFailedPayload // This webhook confirms the payment assessment has failed type WebhookPaymentMessageAssesmentFailedPayload struct { diff --git a/webhook/webhook.go b/webhook/webhook.go index 83fdd11..7edb13c 100644 --- a/webhook/webhook.go +++ b/webhook/webhook.go @@ -1,6 +1,10 @@ package webhook -import "encoding/json" +import ( + "encoding/json" + + "github.com/google/uuid" +) const ( Test = "FITestEvent" @@ -41,3 +45,17 @@ type WebhookResponse struct { // The value that you receive in the webhook request. Nonce int `json:"Nonce"` } + +// Transaction is a common interface for the transaction related webhooks. +type Transaction interface { + GetID() uuid.UUID + GetEndToEndID() string + GetCurrency() string + GetAmount() float64 + IsReturn() bool + GetReference() string + GetAccountIdentifier() string + GetAccountOwner() string + GetCounterpartAccountIdentifier() string + GetCounterpartAccountOwner() string +}