ec51e87d2d
A generic multi-client payment gateway so FlatRender, meezi.ir and bargevasat.ir can all pay through ZarinPal's single verified callback domain (pay.flatrender.ir). New Go service services/payment (clones the notification skeleton + vendored deps): - migration 31_payment_broker.sql — `payment` schema: client_apps, transactions, webhook_deliveries. - ZarinPal v4 client ported from the proven identity PaymentService (request.json -> StartPay -> verify.json; codes 100/101). - client API: POST /v1/pay/request + /v1/pay/inquiry, authed by X-Api-Key + HMAC body signature; GET /callback/zarinpal (the single verified endpoint) verifies, then 302s the user back to the site's return_url (signed) and fires a signed, retried webhook. - per-client ZarinPal merchant override (default = shared merchant); amount stored canonically in Rial, unit to ZarinPal env-configurable. - admin API /v1/admin/* (FlatRender admin JWT): client-app CRUD + key issue/rotate + transactions list. Deploy wiring: payment-svc in docker-compose.v2.yml (host port 1607), pay.flatrender.ir server block in mirror-nginx conf, ENV_FILE + README updates (cert SAN + manual migration note). Admin UI: src/components/admin/PaymentsAdmin.tsx (client apps with one-time key reveal + rotate, transactions table) + /admin/payments page + nav link + fa/en strings; pay-admin proxy route to payment-svc. Docs/SDK: deploy/PAYMENTS.md (integration contract) + deploy/sdk/flatpay.js (zero-dep Node client + webhook verifier) for meezi/any site. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
117 lines
3.7 KiB
Go
117 lines
3.7 KiB
Go
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package sse
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"io/ioutil"
|
|
)
|
|
|
|
type decoder struct {
|
|
events []Event
|
|
}
|
|
|
|
func Decode(r io.Reader) ([]Event, error) {
|
|
var dec decoder
|
|
return dec.decode(r)
|
|
}
|
|
|
|
func (d *decoder) dispatchEvent(event Event, data string) {
|
|
dataLength := len(data)
|
|
if dataLength > 0 {
|
|
//If the data buffer's last character is a U+000A LINE FEED (LF) character, then remove the last character from the data buffer.
|
|
data = data[:dataLength-1]
|
|
dataLength--
|
|
}
|
|
if dataLength == 0 && event.Event == "" {
|
|
return
|
|
}
|
|
if event.Event == "" {
|
|
event.Event = "message"
|
|
}
|
|
event.Data = data
|
|
d.events = append(d.events, event)
|
|
}
|
|
|
|
func (d *decoder) decode(r io.Reader) ([]Event, error) {
|
|
buf, err := ioutil.ReadAll(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var currentEvent Event
|
|
var dataBuffer *bytes.Buffer = new(bytes.Buffer)
|
|
// TODO (and unit tests)
|
|
// Lines must be separated by either a U+000D CARRIAGE RETURN U+000A LINE FEED (CRLF) character pair,
|
|
// a single U+000A LINE FEED (LF) character,
|
|
// or a single U+000D CARRIAGE RETURN (CR) character.
|
|
lines := bytes.Split(buf, []byte{'\n'})
|
|
for _, line := range lines {
|
|
if len(line) == 0 {
|
|
// If the line is empty (a blank line). Dispatch the event.
|
|
d.dispatchEvent(currentEvent, dataBuffer.String())
|
|
|
|
// reset current event and data buffer
|
|
currentEvent = Event{}
|
|
dataBuffer.Reset()
|
|
continue
|
|
}
|
|
if line[0] == byte(':') {
|
|
// If the line starts with a U+003A COLON character (:), ignore the line.
|
|
continue
|
|
}
|
|
|
|
var field, value []byte
|
|
colonIndex := bytes.IndexRune(line, ':')
|
|
if colonIndex != -1 {
|
|
// If the line contains a U+003A COLON character character (:)
|
|
// Collect the characters on the line before the first U+003A COLON character (:),
|
|
// and let field be that string.
|
|
field = line[:colonIndex]
|
|
// Collect the characters on the line after the first U+003A COLON character (:),
|
|
// and let value be that string.
|
|
value = line[colonIndex+1:]
|
|
// If value starts with a single U+0020 SPACE character, remove it from value.
|
|
if len(value) > 0 && value[0] == ' ' {
|
|
value = value[1:]
|
|
}
|
|
} else {
|
|
// Otherwise, the string is not empty but does not contain a U+003A COLON character character (:)
|
|
// Use the whole line as the field name, and the empty string as the field value.
|
|
field = line
|
|
value = []byte{}
|
|
}
|
|
// The steps to process the field given a field name and a field value depend on the field name,
|
|
// as given in the following list. Field names must be compared literally,
|
|
// with no case folding performed.
|
|
switch string(field) {
|
|
case "event":
|
|
// Set the event name buffer to field value.
|
|
currentEvent.Event = string(value)
|
|
case "id":
|
|
// Set the event stream's last event ID to the field value.
|
|
currentEvent.Id = string(value)
|
|
case "retry":
|
|
// If the field value consists of only characters in the range U+0030 DIGIT ZERO (0) to U+0039 DIGIT NINE (9),
|
|
// then interpret the field value as an integer in base ten, and set the event stream's reconnection time to that integer.
|
|
// Otherwise, ignore the field.
|
|
currentEvent.Id = string(value)
|
|
case "data":
|
|
// Append the field value to the data buffer,
|
|
dataBuffer.Write(value)
|
|
// then append a single U+000A LINE FEED (LF) character to the data buffer.
|
|
dataBuffer.WriteString("\n")
|
|
default:
|
|
//Otherwise. The field is ignored.
|
|
continue
|
|
}
|
|
}
|
|
// Once the end of the file is reached, the user agent must dispatch the event one final time.
|
|
d.dispatchEvent(currentEvent, dataBuffer.String())
|
|
|
|
return d.events, nil
|
|
}
|