Skip to content

Commit

Permalink
3DS Callback & webhook verify method (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlicanAkkus committed Dec 12, 2022
1 parent 7a5b021 commit 4fb7ea1
Show file tree
Hide file tree
Showing 6 changed files with 899 additions and 759 deletions.
2 changes: 2 additions & 0 deletions adapter/craftgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type Client struct {
SettlementReporting *SettlementReporting
FileReporting *FileReporting
Fraud *Fraud
Hook *Hook
}

func New(apiKey, apiSecret, baseURL string, opts ...ClientOption) (*Client, error) {
Expand Down Expand Up @@ -117,6 +118,7 @@ func newClient(apiKey, secretKey string) *Client {
client.SettlementReporting = &SettlementReporting{Client: client}
client.FileReporting = &FileReporting{Client: client}
client.Fraud = &Fraud{Client: client}
client.Hook = &Hook{Client: client}

return client
}
Expand Down
22 changes: 22 additions & 0 deletions adapter/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package adapter

import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
)

type Hook struct {
Client *Client
}

func (c *Hook) IsWebhookVerified(merchantHookKey, incomingSignature string, webhookData WebhookData) bool {
data := fmt.Sprintf("%s%d%s%s", webhookData.EventType, webhookData.EventTimestamp, webhookData.Status, webhookData.PayloadId)

hmac := hmac.New(sha256.New, []byte(merchantHookKey))
hmac.Write([]byte(data))
signature := base64.StdEncoding.EncodeToString(hmac.Sum(nil))

return incomingSignature == signature
}
26 changes: 25 additions & 1 deletion adapter/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type FraudCheckStatus string
type ApmAdditionalAction string
type ReportFileType string
type WalletTransactionType string
type WebhookEventType string
type WebhookStatus string

const (
ApiKeyHeaderName = "x-api-key"
Expand Down Expand Up @@ -103,7 +105,7 @@ const (
USD = "USD"
EUR = "EUR"
GBP = "GBP"
CNY = "CNY"
CNY = "CNY"
)

// payment group declaration
Expand Down Expand Up @@ -313,6 +315,20 @@ const (
DEPOSIT_FROM_FUND_TRANSFER = "DEPOSIT_FROM_FUND_TRANSFER"
)

const (
_ WebhookEventType = ""
API_AUTH = "API_AUTH"
API_VERIFY_AND_AUTH = "API_VERIFY_AND_AUTH"
CHECKOUTFORM_AUTH = "CHECKOUTFORM_AUTH"
THREEDS_VERIFY = "THREEDS_VERIFY"
)

const (
_ WebhookStatus = ""
WebhookStatusSUCCESS = "SUCCESS"
WebhookStatusFAILURE = "FAILURE"
)

// requests
type CreatePaymentRequest struct {
Price float64 `json:"price,omitempty"`
Expand Down Expand Up @@ -1317,6 +1333,14 @@ type FraudValue struct {
ExpireInSeconds *int `json:"expireInSeconds"`
}

type WebhookData struct {
EventType WebhookEventType
EventTime time.Time
EventTimestamp int64
Status WebhookStatus
PayloadId string
}

type RequestOptions struct {
BaseURL string
ApiKey string
Expand Down
Loading

0 comments on commit 4fb7ea1

Please sign in to comment.