From 2a36b31750b93bd3c1124ea708dacf43a70d61bb Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 25 Nov 2024 14:37:37 +0300 Subject: [PATCH] update the format to be +254xxxxxxxxx --- common/recipient.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/common/recipient.go b/common/recipient.go index 9b44926..708d405 100644 --- a/common/recipient.go +++ b/common/recipient.go @@ -1,6 +1,7 @@ package common import ( + "errors" "fmt" "regexp" "strings" @@ -48,8 +49,12 @@ func CheckRecipient(recipient string) (string, error) { return "", fmt.Errorf("invalid recipient: must be a phone number, address or alias") } -// FormatPhoneNumber formats a Kenyan phone number to "254xxxxxxxx". +// FormatPhoneNumber formats a Kenyan phone number to "+254xxxxxxxx". func FormatPhoneNumber(phone string) (string, error) { + if !IsValidPhoneNumber(phone) { + return "", errors.New("invalid phone number") + } + // Remove any leading "+" and spaces phone = strings.TrimPrefix(phone, "+") phone = strings.ReplaceAll(phone, " ", "") @@ -59,5 +64,10 @@ func FormatPhoneNumber(phone string) (string, error) { phone = "254" + phone[1:] } - return phone, nil -} \ No newline at end of file + // Add "+" if not already present + if !strings.HasPrefix(phone, "254") { + return "", errors.New("unexpected format") + } + + return "+" + phone, nil +}