package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.paycrest.io/v1"
)
// Get exchange rate
func getExchangeRate(token, amount, currency string) (string, error) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/rates/%s/%s/%s", BASE_URL, token, amount, currency), nil)
req.Header.Set("API-Key", API_KEY)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println("Rate retrieved:", string(body))
// Parse response to get rate (simplified)
var result map[string]interface{}
json.Unmarshal(body, &result)
return result["data"].(string), nil
}
// Create payment order
func createPaymentOrder() error {
// First, get the current rate
rate, err := getExchangeRate("USDT", "100", "NGN")
if err != nil {
return err
}
payload := map[string]interface{}{
"amount": "100",
"token": "USDT",
"network": "base",
"rate": rate,
"recipient": map[string]interface{}{
"institution": "GTB",
"accountIdentifier": "1234567890",
"accountName": "John Doe",
"currency": "NGN",
"memo": "Salary payment for January 2024",
},
"reference": "payment-123",
"returnAddress": "0x1234567890123456789012345678901234567890",
}
jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", BASE_URL+"/sender/orders", bytes.NewBuffer(jsonData))
req.Header.Set("API-Key", API_KEY)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println("Order created:", string(body))
return nil
}
// Get order status
func getOrderStatus(orderID string) error {
req, _ := http.NewRequest("GET", BASE_URL+"/sender/orders/"+orderID, nil)
req.Header.Set("API-Key", API_KEY)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println("Order status:", string(body))
return nil
}