RezinaLMS-BackEnd/internal/services/chapa/multipart.go
Yared Yemane dd82a1b27f feat: add Chapa Direct Charge for in-app USSD subscription payments
Adds /payments/chapa/direct initiate and authorize endpoints alongside existing hosted checkout. Includes 3DES OTP authorization, verify routing for CHAPA_DIRECT payments, tests, and client app integration guide.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-02 04:04:29 -07:00

45 lines
1.1 KiB
Go

package chapa
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"strings"
)
func (s *Service) postMultipart(ctx context.Context, endpoint string, fields map[string]string) ([]byte, int, error) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
for key, value := range fields {
if err := writer.WriteField(key, value); err != nil {
return nil, 0, fmt.Errorf("write multipart field %s: %w", key, err)
}
}
if err := writer.Close(); err != nil {
return nil, 0, err
}
url := strings.TrimSuffix(s.cfg.CHAPA_BASE_URL, "/") + endpoint
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &body)
if err != nil {
return nil, 0, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("Authorization", "Bearer "+s.cfg.CHAPA_SECRET_KEY)
resp, err := s.httpClient.Do(req)
if err != nil {
return nil, 0, fmt.Errorf("chapa multipart request failed: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, resp.StatusCode, err
}
return respBody, resp.StatusCode, nil
}