Skip to content

Webhook Entegrasyonu ve İmza Doğrulama

Shopier, mağazanızda gerçekleşen olayları (sipariş, iade, ürün değişiklikleri) belirlediğiniz Bildirim URL'ine (Notification URL) HTTP POST isteği olarak iletir. İsteklerin Shopier'dan geldiğini doğrulamak için tüm bildirimler uygulamanızın webhook token'ı ile HS256 (HMAC-SHA256) algoritmasıyla imzalanır.


Webhook Başlıkları (Headers)

BaşlıkAçıklama
Shopier-SignatureHMAC-SHA256 imza değeri (hex veya base64)
Shopier-EventOlay tipi adı (örn. order.created)
Shopier-Webhook-IdBildirimin benzersiz kimlik numarası
Shopier-TimestampSaniye cinsinden UTC Unix zaman damgası
Shopier-Account-Idİlgili mağazanın hesap kimliği
Shopier-Api-VersionAPI sürüm numarası

Desteklenen Olaylar

Olay SabitiOlay AdıVeri ModeliTetiklenme Anı
webhook.EventOrderCreatedorder.created*shopier.OrderYeni bir sipariş ödendiğinde
webhook.EventOrderAddressUpdatedorder.addressUpdated*shopier.OrderAlıcı teslimat adresi güncellendiğinde
webhook.EventOrderFulfilledorder.fulfilled*shopier.OrderSipariş kargolandığında/tamamlandığında
webhook.EventProductCreatedproduct.created*shopier.ProductYeni bir ürün yayınlandığında
webhook.EventProductUpdatedproduct.updated*shopier.ProductMevcut ürün güncellendiğinde
webhook.EventRefundRequestedrefund.requested*shopier.Refundİade talebi oluşturulduğunda
webhook.EventRefundUpdatedrefund.updated*shopier.Refundİade onaylandığında veya reddedildiğinde

1. Hazır http.Handler ile Webhook Dinleyici

En hızlı ve standart net/http uyumlu webhook dinleyici kurulumu:

go
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/AdisGroup/shopier-go/webhook"
)

func main() {
	secret := os.Getenv("SHOPIER_WEBHOOK_SECRET")

	handler := webhook.NewHandler(secret, func(ctx context.Context, evt *webhook.Event) error {
		log.Printf("Olay alındı: %s (ID: %s)", evt.Header.Event, evt.Header.WebhookID)

		switch evt.Header.Event {
		case webhook.EventOrderCreated:
			order, err := evt.Order()
			if err != nil {
				return err
			}
			fmt.Printf("Yeni Sipariş: #%s (%s %s)\n", order.ID, order.Totals.Total, order.Currency)

		case webhook.EventRefundRequested:
			refund, err := evt.Refund()
			if err != nil {
				return err
			}
			fmt.Printf("İade Talebi: #%s (Sipariş: %s)\n", refund.ID, refund.OrderID)
		}

		return nil
	})

	http.Handle("/webhooks/shopier", handler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

2. Popüler Web Framework'leri ile Kullanım (Gin, Chi, Fiber)

webhook.Parse Kullanımı (Gin, Chi)

go
event, err := webhook.Parse(c.Request, webhookSecret)
if err != nil {
	c.String(http.StatusUnauthorized, "Yetkisiz istek: %v", err)
	return
}

order, _ := event.Order()

Doğrudan İmza Doğrulama (webhook.VerifySignature - Fiber)

go
app.Post("/webhooks/shopier", func(c *fiber.Ctx) error {
	sig := c.Get("Shopier-Signature")
	body := c.Body()

	if !webhook.VerifySignature(body, sig, webhookSecret) {
		return c.Status(fiber.StatusUnauthorized).SendString("Geçersiz imza")
	}

	// Payload'ı işleyin...
	return c.SendStatus(fiber.StatusOK)
})