Yimaru-BackEnd/internal/domain/chapa.go
Yared Yemane a83745fd93 Fix Chapa verify JSON parsing when amount is numeric.
Accept string or number for amount in verify and webhook payloads so GET /payments/verify can complete successfully.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-02 02:27:13 -07:00

104 lines
2.9 KiB
Go

package domain
import (
"encoding/json"
"fmt"
)
// ChapaFlexibleString unmarshals JSON string or number (Chapa verify/webhook payloads vary).
type ChapaFlexibleString string
func (s *ChapaFlexibleString) UnmarshalJSON(data []byte) error {
if len(data) == 0 || string(data) == "null" {
*s = ""
return nil
}
switch data[0] {
case '"':
var v string
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*s = ChapaFlexibleString(v)
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
var n json.Number
if err := json.Unmarshal(data, &n); err != nil {
return err
}
*s = ChapaFlexibleString(n.String())
default:
return fmt.Errorf("chapa flexible string: unsupported json type %q", data[0])
}
return nil
}
func (s ChapaFlexibleString) String() string {
return string(s)
}
// ChapaInitializeRequest is sent to POST /transaction/initialize.
type ChapaInitializeRequest struct {
Amount string `json:"amount"`
Currency string `json:"currency"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
PhoneNumber string `json:"phone_number,omitempty"`
TxRef string `json:"tx_ref"`
CallbackURL string `json:"callback_url,omitempty"`
ReturnURL string `json:"return_url,omitempty"`
Customization struct {
Title string `json:"title"`
Description string `json:"description"`
} `json:"customization,omitempty"`
}
type ChapaInitializeResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Data struct {
CheckoutURL string `json:"checkout_url"`
} `json:"data"`
}
type ChapaVerifyResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Data ChapaTransactionData `json:"data"`
}
type ChapaTransactionData struct {
TxRef string `json:"tx_ref"`
Reference string `json:"reference"`
Amount ChapaFlexibleString `json:"amount"`
Currency string `json:"currency"`
Status string `json:"status"`
PaymentMethod string `json:"payment_method"`
Mode string `json:"mode"`
}
// ChapaWebhookPayload is the body POSTed to the webhook URL.
type ChapaWebhookPayload struct {
Event string `json:"event"`
Type string `json:"type"`
TxRef string `json:"tx_ref"`
Reference string `json:"reference"`
Status string `json:"status"`
Amount ChapaFlexibleString `json:"amount"`
Currency string `json:"currency"`
PaymentMethod string `json:"payment_method"`
Mode string `json:"mode"`
}
// ChapaCallbackQuery is sent to callback_url after payment (GET).
type ChapaCallbackQuery struct {
TrxRef string `json:"trx_ref"`
RefID string `json:"ref_id"`
Status string `json:"status"`
}
type ChapaPaymentMethod struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
}