# paynex-go

Go client for the Paynex merchant API. Zero dependencies.

```bash
# vendor it into your project (the zip ships go.mod; rename go.mod.txt → go.mod if you took the raw file):
mkdir -p internal/paynex && cp paynex.go internal/paynex/
```

```go
import "yourapp/internal/paynex"

c := paynex.New("https://pay.example.com", os.Getenv("PAYNEX_KEY_ID"), os.Getenv("PAYNEX_SECRET"))

// 1. create a payment (server side) and send the customer to CheckoutURL
p, err := c.CreatePayment(ctx, paynex.CreatePaymentParams{
    Amount: "1500.00", MerchantRef: "INV-1042", Description: "Hosting renewal",
    Customer:  map[string]any{"name": "Rahim", "email": "r@example.com", "phone": "017…"},
    ReturnURL: "https://shop.example.com/thanks", CancelURL: "https://shop.example.com/cart",
    // mobile app: ReturnURL: "myapp://paynex/return", CancelURL: "myapp://paynex/cancel"
}, "INV-1042-1500.00")

// 2. webhook receiver
http.Handle("/paynex/webhook", paynex.WebhookHandler(os.Getenv("PAYNEX_WEBHOOK_SECRET"), func(ctx context.Context, ev *paynex.Event) error {
    if ev.Event != "payment.paid" { return nil }
    d, _ := ev.Payment()
    full, err := c.GetPayment(ctx, d.ID)   // source of truth
    if err != nil { return err }
    if full.Paid() { markOrderPaid(full.MerchantRef, full.ID) } // idempotent by full.ID
    return nil
}))

// 3. return redirect / deep link (UX only)
rp, err := paynex.VerifyReturn(webhookSecret, r.URL.Query(), 5*time.Minute)
```

Other calls: `GetPayment`, `ListPayments`, `CancelPayment`, `VerifyPayment`, `CreateRefund`,
`GetRefund`, `GetStore`. Errors are `*paynex.APIError{HTTPStatus, Code, Message}`.
