Compare commits
No commits in common. "dda1d0d9db4b72b309c44cb089cc793461f228cc" and "04f33d0b6c1613703cf10c6f813d99e429f37c00" have entirely different histories.
dda1d0d9db
...
04f33d0b6c
@ -28,18 +28,15 @@ const (
|
|||||||
AliasUpdatePrefix = "/api/v1/internal/update"
|
AliasUpdatePrefix = "/api/v1/internal/update"
|
||||||
CreditSendPrefix = "/api/v1/credit-send"
|
CreditSendPrefix = "/api/v1/credit-send"
|
||||||
CreditSendReverseQuotePrefix = "/api/v1/pool/reverse-quote"
|
CreditSendReverseQuotePrefix = "/api/v1/pool/reverse-quote"
|
||||||
MpesaOnrampPath = "/api/v1/trigger-onramp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
custodialURLBase string
|
custodialURLBase string
|
||||||
dataURLBase string
|
dataURLBase string
|
||||||
BearerToken string
|
BearerToken string
|
||||||
aliasEnsURLBase string
|
aliasEnsURLBase string
|
||||||
externalSMSBase string
|
externalSMSBase string
|
||||||
IncludeStablesParam string
|
IncludeStablesParam string
|
||||||
MpesaOnrampBearerToken string
|
|
||||||
mpesaOnrampBase string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -64,7 +61,6 @@ var (
|
|||||||
AliasUpdateURL string
|
AliasUpdateURL string
|
||||||
CreditSendURL string
|
CreditSendURL string
|
||||||
CreditSendReverseQuoteURL string
|
CreditSendReverseQuoteURL string
|
||||||
MpesaOnrampURL string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setBase() error {
|
func setBase() error {
|
||||||
@ -76,8 +72,6 @@ func setBase() error {
|
|||||||
externalSMSBase = env.GetEnv("EXTERNAL_SMS_BASE", "http://localhost:5035")
|
externalSMSBase = env.GetEnv("EXTERNAL_SMS_BASE", "http://localhost:5035")
|
||||||
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
||||||
IncludeStablesParam = env.GetEnv("INCLUDE_STABLES_PARAM", "false")
|
IncludeStablesParam = env.GetEnv("INCLUDE_STABLES_PARAM", "false")
|
||||||
MpesaOnrampBearerToken = env.GetEnv("MPESA_BEARER_TOKEN", "")
|
|
||||||
mpesaOnrampBase = env.GetEnv("MPESA_ONRAMP_BASE", "https://pretium.v1.grassecon.net")
|
|
||||||
|
|
||||||
_, err = url.Parse(custodialURLBase)
|
_, err = url.Parse(custodialURLBase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -117,7 +111,6 @@ func LoadConfig() error {
|
|||||||
AliasUpdateURL, _ = url.JoinPath(aliasEnsURLBase, AliasUpdatePrefix)
|
AliasUpdateURL, _ = url.JoinPath(aliasEnsURLBase, AliasUpdatePrefix)
|
||||||
CreditSendURL, _ = url.JoinPath(dataURLBase, CreditSendPrefix)
|
CreditSendURL, _ = url.JoinPath(dataURLBase, CreditSendPrefix)
|
||||||
CreditSendReverseQuoteURL, _ = url.JoinPath(dataURLBase, CreditSendReverseQuotePrefix)
|
CreditSendReverseQuoteURL, _ = url.JoinPath(dataURLBase, CreditSendReverseQuotePrefix)
|
||||||
MpesaOnrampURL, _ = url.JoinPath(mpesaOnrampBase, MpesaOnrampPath)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -921,11 +921,3 @@ func (das *DevAccountService) GetCreditSendReverseQuote(ctx context.Context, poo
|
|||||||
OutputAmount: "40000000",
|
OutputAmount: "40000000",
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) MpesaTriggerOnramp(ctx context.Context, address, phoneNumber, asset string, amount int) (*models.MpesaOnrampResponse, error) {
|
|
||||||
return &models.MpesaOnrampResponse{
|
|
||||||
Message: "Success, kindly accept prompt sent.",
|
|
||||||
Status: "PENDING",
|
|
||||||
TransactionCode: "ae6fb33b-4653-4f38-a3b6-85dfea7a1e99",
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,7 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
type MpesaOnrampResponse struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
TransactionCode string `json:"transactionCode"`
|
|
||||||
}
|
|
||||||
@ -32,5 +32,4 @@ type AccountService interface {
|
|||||||
CheckTokenInPool(ctx context.Context, poolAddress, tokenAddress string) (*models.TokenInPoolResult, error)
|
CheckTokenInPool(ctx context.Context, poolAddress, tokenAddress string) (*models.TokenInPoolResult, error)
|
||||||
GetCreditSendMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.CreditSendLimitsResult, error)
|
GetCreditSendMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.CreditSendLimitsResult, error)
|
||||||
GetCreditSendReverseQuote(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, toTokenAMount string) (*models.CreditSendReverseQouteResult, error)
|
GetCreditSendReverseQuote(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, toTokenAMount string) (*models.CreditSendReverseQouteResult, error)
|
||||||
MpesaTriggerOnramp(ctx context.Context, address, phoneNumber, asset string, amount int) (*models.MpesaOnrampResponse, error)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
||||||
@ -22,7 +23,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logg = slogging.Get().With("component", "sarafu-api.devapi")
|
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||||
|
logg = slogging.Get().With("component", "sarafu-api.devapi")
|
||||||
)
|
)
|
||||||
|
|
||||||
type APIError struct {
|
type APIError struct {
|
||||||
@ -30,10 +32,6 @@ type APIError struct {
|
|||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ctxKey string
|
|
||||||
|
|
||||||
const ctxKeyAuthToken ctxKey = "authToken"
|
|
||||||
|
|
||||||
func (e *APIError) Error() string {
|
func (e *APIError) Error() string {
|
||||||
if e.Code != "" {
|
if e.Code != "" {
|
||||||
return fmt.Sprintf("[%s] %s", e.Code, e.Description)
|
return fmt.Sprintf("[%s] %s", e.Code, e.Description)
|
||||||
@ -784,59 +782,12 @@ func (as *HTTPAccountService) GetCreditSendReverseQuote(ctx context.Context, poo
|
|||||||
return &r, nil
|
return &r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MpesaTriggerOnramp calls the API to perform an STK Push.
|
|
||||||
// Parameters:
|
|
||||||
// - address: The user's public key.
|
|
||||||
// - phoneNumber: The user's phone number
|
|
||||||
// - asset: the intented USD voucher "USDT | USDC | cUSD"
|
|
||||||
// - amount: The amount in Kenyan shillings
|
|
||||||
func (as *HTTPAccountService) MpesaTriggerOnramp(ctx context.Context, address, phoneNumber, asset string, amount int) (*models.MpesaOnrampResponse, error) {
|
|
||||||
var r models.MpesaOnrampResponse
|
|
||||||
|
|
||||||
ctx = context.WithValue(ctx, ctxKeyAuthToken, config.MpesaOnrampBearerToken)
|
|
||||||
|
|
||||||
// Prepare payload
|
|
||||||
payload := struct {
|
|
||||||
Address string `json:"address"`
|
|
||||||
PhoneNumber string `json:"phoneNumber"`
|
|
||||||
Asset string `json:"asset"`
|
|
||||||
Amount int `json:"amount"`
|
|
||||||
}{
|
|
||||||
Address: strings.TrimSpace(address),
|
|
||||||
PhoneNumber: strings.TrimSpace(phoneNumber),
|
|
||||||
Asset: strings.TrimSpace(asset),
|
|
||||||
Amount: amount,
|
|
||||||
}
|
|
||||||
|
|
||||||
payloadBytes, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to marshal mpesa onramp payload: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", config.MpesaOnrampURL, bytes.NewBuffer(payloadBytes))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := doRequest(ctx, req, &r); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: remove eth-custodial api dependency
|
// TODO: remove eth-custodial api dependency
|
||||||
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
|
||||||
var okResponse api.OKResponse
|
var okResponse api.OKResponse
|
||||||
var errResponse api.ErrResponse
|
var errResponse api.ErrResponse
|
||||||
|
|
||||||
// Check if a custom Authorization token was provided
|
req.Header.Set("Authorization", "Bearer "+config.BearerToken)
|
||||||
if token, ok := ctx.Value(ctxKeyAuthToken).(string); ok && token != "" {
|
|
||||||
req.Header.Set("Authorization", "Bearer "+token)
|
|
||||||
} else {
|
|
||||||
req.Header.Set("Authorization", "Bearer "+config.BearerToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
// Log request
|
// Log request
|
||||||
|
|||||||
@ -130,8 +130,3 @@ func (m MockAccountService) GetCreditSendReverseQuote(ctx context.Context, poolA
|
|||||||
args := m.Called(poolAddress, fromTokenAddress, toTokenAddress, toTokenAMount)
|
args := m.Called(poolAddress, fromTokenAddress, toTokenAddress, toTokenAMount)
|
||||||
return args.Get(0).(*models.CreditSendReverseQouteResult), args.Error(1)
|
return args.Get(0).(*models.CreditSendReverseQouteResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m MockAccountService) MpesaTriggerOnramp(ctx context.Context, address, phoneNumber, asset string, amount int) (*models.MpesaOnrampResponse, error) {
|
|
||||||
args := m.Called(address, phoneNumber, asset, amount)
|
|
||||||
return args.Get(0).(*models.MpesaOnrampResponse), args.Error(1)
|
|
||||||
}
|
|
||||||
|
|||||||
@ -132,7 +132,3 @@ func (m TestAccountService) GetCreditSendMaxLimit(ctx context.Context, poolAddre
|
|||||||
func (m TestAccountService) GetCreditSendReverseQuote(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, toTokenAMount string) (*models.CreditSendReverseQouteResult, error) {
|
func (m TestAccountService) GetCreditSendReverseQuote(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, toTokenAMount string) (*models.CreditSendReverseQouteResult, error) {
|
||||||
return &models.CreditSendReverseQouteResult{}, nil
|
return &models.CreditSendReverseQouteResult{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m TestAccountService) MpesaTriggerOnramp(ctx context.Context, address, phoneNumber, asset string, amount int) (*models.MpesaOnrampResponse, error) {
|
|
||||||
return &models.MpesaOnrampResponse{}, nil
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user