add pluggable PayBridge and Dojo payment flow with CMS credentials
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
package paybridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
||||
"gitea.futuresens.co.uk/futuresens/hardlink/internal/types"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
URL string
|
||||
APIKey string
|
||||
TimeoutSeconds int
|
||||
}
|
||||
|
||||
func NewClient(url, apiKey string, timeoutSeconds int) *Client {
|
||||
if timeoutSeconds <= 0 {
|
||||
timeoutSeconds = 300
|
||||
}
|
||||
|
||||
return &Client{
|
||||
URL: url,
|
||||
APIKey: apiKey,
|
||||
TimeoutSeconds: timeoutSeconds,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Sale(ctx context.Context, req paymentsvc.SaleRequest) (*paymentsvc.Result, error) {
|
||||
if req.Currency == "" {
|
||||
req.Currency = "GBP"
|
||||
}
|
||||
|
||||
payBridgeReq := PaymentRequest{
|
||||
RequestID: req.RequestID,
|
||||
Amount: req.Amount,
|
||||
Currency: req.Currency,
|
||||
Operation: "SALE",
|
||||
TimeoutSeconds: c.TimeoutSeconds,
|
||||
}
|
||||
|
||||
return c.doPayment(ctx, payBridgeReq)
|
||||
}
|
||||
|
||||
func (c *Client) doPayment(ctx context.Context, req PaymentRequest) (*paymentsvc.Result, error) {
|
||||
connectURL, err := c.connectURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ws, _, err := websocket.DefaultDialer.DialContext(ctx, connectURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrConnectionFailed, err)
|
||||
}
|
||||
defer ws.Close()
|
||||
|
||||
done := make(chan struct{})
|
||||
defer close(done)
|
||||
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
_ = ws.Close()
|
||||
case <-done:
|
||||
}
|
||||
}()
|
||||
|
||||
jwt, err := c.readAuthSuccess(ws)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ws.WriteJSON(Envelope{
|
||||
Type: types.MesTypePaymentRequest,
|
||||
JWT: jwt,
|
||||
Data: req,
|
||||
Timestamp: time.Now().UnixMilli(),
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("send PayBridge payment_request: %w", err)
|
||||
}
|
||||
|
||||
for {
|
||||
_, raw, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return nil, fmt.Errorf("read PayBridge message: %w", err)
|
||||
}
|
||||
|
||||
var head struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &head); err != nil {
|
||||
return nil, fmt.Errorf("decode PayBridge message header: %w", err)
|
||||
}
|
||||
|
||||
switch strings.ToLower(head.Type) {
|
||||
case types.MesTypePaymentResult:
|
||||
var result PaymentResultEnvelope
|
||||
if err := json.Unmarshal(raw, &result); err != nil {
|
||||
return nil, fmt.Errorf("decode payment_result: %w", err)
|
||||
}
|
||||
return mapPaymentResult(result), nil
|
||||
|
||||
case types.MesTypePaymentError:
|
||||
var paymentErr PaymentErrorEnvelope
|
||||
if err := json.Unmarshal(raw, &paymentErr); err != nil {
|
||||
return nil, fmt.Errorf("decode payment_error: %w", err)
|
||||
}
|
||||
return mapPaymentError(req, paymentErr), nil
|
||||
|
||||
case types.ResultError:
|
||||
var genericErr struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &genericErr); err != nil {
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnexpectedMessage, string(raw))
|
||||
}
|
||||
if genericErr.Error.Message != "" {
|
||||
return nil, fmt.Errorf("%w: %s: %s", ErrUnexpectedMessage, genericErr.Error.Code, genericErr.Error.Message)
|
||||
}
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnexpectedMessage, string(raw))
|
||||
|
||||
case types.MesTypePaymentStatusUpdate, types.MesTypePaymentAccepted, types.MesTypeAuthSuccess:
|
||||
// Intermediate message. Keep waiting for payment_result or payment_error.
|
||||
|
||||
default:
|
||||
// PayBridge may introduce additional intermediate message types.
|
||||
// Ignore them and continue waiting for the final result.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) connectURL() (string, error) {
|
||||
u, err := url.Parse(c.URL)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse PayBridge WebSocket URL: %w", err)
|
||||
}
|
||||
|
||||
q := u.Query()
|
||||
q.Set("api_key", c.APIKey)
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func (c *Client) readAuthSuccess(ws *websocket.Conn) (string, error) {
|
||||
if err := ws.SetReadDeadline(time.Now().Add(10 * time.Second)); err != nil {
|
||||
return "", fmt.Errorf("%w: set auth read deadline: %v", ErrAuthFailed, err)
|
||||
}
|
||||
defer ws.SetReadDeadline(time.Time{})
|
||||
|
||||
_, raw, err := ws.ReadMessage()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: read auth_success: %v", ErrAuthFailed, err)
|
||||
}
|
||||
|
||||
var auth struct {
|
||||
Type string `json:"type"`
|
||||
JWT string `json:"jwt"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &auth); err != nil {
|
||||
return "", fmt.Errorf("%w: decode auth_success: %v", ErrAuthFailed, err)
|
||||
}
|
||||
if !strings.EqualFold(auth.Type, types.MesTypeAuthSuccess) {
|
||||
return "", fmt.Errorf("%w: expected auth_success, got %s: %s", ErrAuthFailed, auth.Type, string(raw))
|
||||
}
|
||||
if auth.JWT == "" {
|
||||
return "", fmt.Errorf("%w: auth_success did not contain jwt", ErrAuthFailed)
|
||||
}
|
||||
|
||||
return auth.JWT, nil
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package paybridge
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrConnectionFailed = errors.New("paybridge connection failed")
|
||||
ErrAuthFailed = errors.New("paybridge authentication failed")
|
||||
ErrUnexpectedMessage = errors.New("paybridge unexpected message")
|
||||
)
|
||||
@@ -0,0 +1,47 @@
|
||||
package paybridge
|
||||
|
||||
import "gitea.futuresens.co.uk/futuresens/hardlink/internal/paymentsvc"
|
||||
|
||||
func mapPaymentResult(res PaymentResultEnvelope) *paymentsvc.Result {
|
||||
var merchantReceipt string
|
||||
if res.Data.ReceiptData.Merchant != nil {
|
||||
merchantReceipt = *res.Data.ReceiptData.Merchant
|
||||
}
|
||||
|
||||
return &paymentsvc.Result{
|
||||
Success: res.Data.Success,
|
||||
TransactionID: res.Data.TransactionID,
|
||||
RequestID: res.Data.RequestID,
|
||||
Operation: res.Data.Operation,
|
||||
Status: res.Data.Status,
|
||||
Message: res.Data.Message,
|
||||
ErrorMessage: res.Data.ErrorMessage,
|
||||
Amount: res.Data.Amount,
|
||||
Currency: res.Data.Currency,
|
||||
AuthCode: res.Data.AuthCode,
|
||||
DeviceUsed: res.Data.DeviceUsed,
|
||||
DeviceType: res.Data.DeviceType,
|
||||
ReferenceNumber: res.Data.ReferenceNumber,
|
||||
LastFourDigits: res.Data.LastFourDigits,
|
||||
CardType: res.Data.CardType,
|
||||
CardNumber: res.Data.CardNumber,
|
||||
ExpiryDate: res.Data.ExpiryDate,
|
||||
CardHash: res.Data.CardHash,
|
||||
CardReference: res.Data.CardReference,
|
||||
CustomerReceipt: res.Data.ReceiptData.Customer,
|
||||
MerchantReceipt: merchantReceipt,
|
||||
}
|
||||
}
|
||||
|
||||
func mapPaymentError(req PaymentRequest, res PaymentErrorEnvelope) *paymentsvc.Result {
|
||||
return &paymentsvc.Result{
|
||||
Success: false,
|
||||
TransactionID: res.Data.TransactionID,
|
||||
RequestID: req.RequestID,
|
||||
Operation: req.Operation,
|
||||
Status: res.Data.Status,
|
||||
ErrorMessage: res.Data.Error,
|
||||
Amount: req.Amount,
|
||||
Currency: req.Currency,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package paybridge
|
||||
|
||||
type Envelope struct {
|
||||
Type string `json:"type"`
|
||||
Data any `json:"data,omitempty"`
|
||||
Timestamp int64 `json:"timestamp,omitempty"`
|
||||
JWT string `json:"jwt,omitempty"`
|
||||
}
|
||||
|
||||
type PaymentRequest struct {
|
||||
RequestID string `json:"requestId"`
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
Operation string `json:"operation"`
|
||||
TimeoutSeconds int `json:"timeoutSeconds,omitempty"`
|
||||
}
|
||||
|
||||
type PaymentResultEnvelope struct {
|
||||
Type string `json:"type"`
|
||||
Data struct {
|
||||
Success bool `json:"success"`
|
||||
TransactionID string `json:"transactionId"`
|
||||
RequestID string `json:"requestId"`
|
||||
Operation string `json:"operation"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
ErrorMessage string `json:"errorMessage"`
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
AuthCode string `json:"authCode"`
|
||||
DeviceUsed string `json:"deviceUsed"`
|
||||
DeviceType string `json:"deviceType"`
|
||||
ReferenceNumber string `json:"referenceNumber"`
|
||||
LastFourDigits string `json:"lastFourDigits"`
|
||||
CardType string `json:"cardType"`
|
||||
CardNumber string `json:"cardNumber"`
|
||||
ExpiryDate string `json:"expiryDate"`
|
||||
CardHash string `json:"cardHash"`
|
||||
CardReference string `json:"cardReference"`
|
||||
|
||||
ReceiptData struct {
|
||||
Merchant *string `json:"merchant"`
|
||||
Customer string `json:"customer"`
|
||||
} `json:"receiptData"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type PaymentErrorEnvelope struct {
|
||||
Type string `json:"type"`
|
||||
Data struct {
|
||||
TransactionID string `json:"transactionId"`
|
||||
Error string `json:"error"`
|
||||
Status string `json:"status"`
|
||||
} `json:"data"`
|
||||
}
|
||||
Reference in New Issue
Block a user