Skip to content

Commit

Permalink
IOS Payment (#964)
Browse files Browse the repository at this point in the history
* Added Plan V3 API.

* Enable Upgrade screen and start working on currency format.

* Completed pre process of plans.

* Use dynamic local.

* Append currency code.

* Added In App Purchase for IOS.

* Started working on API.

* Implement helper for purchase request.

* Implemented Promo code flow.

* Fixed issue device nog showing up.

* Calculate Expirydate

* Added mock API call for apple pay.

* Run mode tidy and removes static data.

* Fetch user id for first time user.

* Fixed iOS IAP.  (#962)

* Update flashlight and go versions (#949)

* Update flashlight and go versions

* Do not enforce go version in Makefile

* Revert "Update flashlight and go versions (#949)" (#950)

This reverts commit eab0e69.

* add a template for GitHub integrated release notes

* generate automatic changelog on release, then trigger automation repo

* fixed IAP flow

* fixed IAP flow

---------

Co-authored-by: Myles Horton <[email protected]>
Co-authored-by: atavism <[email protected]>
Co-authored-by: Jay <[email protected]>

* Added error handling.

* Implemented email warning widget.

* Changes in logs.

* Fixed merge conflicts.

---------

Co-authored-by: Eli Yukelzon <[email protected]>
Co-authored-by: Myles Horton <[email protected]>
Co-authored-by: atavism <[email protected]>
Co-authored-by: Jay <[email protected]>
  • Loading branch information
5 people committed Mar 14, 2024
1 parent e7b0865 commit 538b370
Show file tree
Hide file tree
Showing 26 changed files with 2,027 additions and 912 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public String getFormattedPrice(Map<String, Long> price) {
return getFormattedPrice(price, false);
}

private String getFormattedPrice(Map<String, Long> price, boolean formatFloat) {
private String getFormattedPrice(Map<String, Long> price, boolean formatFloat) {
final String formattedPrice;
Long currencyPrice = price.get(currencyCode);
if (currencyPrice == null) {
Expand Down
4 changes: 4 additions & 0 deletions assets/locales/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -1562,3 +1562,7 @@ msgstr "Your new confirmation code has been sent"
msgid "signup_error"
msgstr "It looks like something went wrong with the sign-up. Please try again, and if the issue continues, reach out to our support team"

msgid "confirm_email_error"
msgstr "Please confirm your email"


303 changes: 137 additions & 166 deletions go.mod

Large diffs are not rendered by default.

1,020 changes: 627 additions & 393 deletions go.sum

Large diffs are not rendered by default.

179 changes: 114 additions & 65 deletions internalsdk/apimodels/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"time"

"math/big"
"net/http"
"net/http/httputil"
Expand All @@ -17,13 +18,14 @@ import (
"google.golang.org/protobuf/proto"
)

// https://api.iantem.io/v1/users/salt
const (
publicBaseUrl = "https://api.iantem.io/v1"
baseUrl = "https://api.getiantem.org"
userGroup = publicBaseUrl + "/users"
userDetailUrl = baseUrl + "/user-data"
userCreateUrl = baseUrl + "/user-create"
plansV3Url = baseUrl + "/plans-v3"
purchaseUrl = baseUrl + "/purchase"
//Sign up urls
signUpUrl = userGroup + "/signup"
signUpCompleteUrl = userGroup + "/signup/complete/email"
Expand All @@ -40,6 +42,13 @@ const (
changeEmailUrl = userGroup + "/change_email"
)

const (
headerDeviceId = "X-Lantern-Device-Id"
headerUserId = "X-Lantern-User-Id"
headerProToken = "X-Lantern-Pro-Token"
headerContentType = "Content-Type"
)

var (
log = golog.LoggerFor("lantern-internalsdk-http")
httpClient = &http.Client{
Expand All @@ -56,13 +65,11 @@ func FechUserDetail(deviceId string, userId string, token string) (*UserDetailRe
}

// Add headers
req.Header.Set("X-Lantern-Device-Id", deviceId)
req.Header.Set("X-Lantern-User-Id", userId)
req.Header.Set("X-Lantern-Pro-Token", token)
req.Header.Set(headerDeviceId, deviceId)
req.Header.Set(headerUserId, userId)
req.Header.Set(headerProToken, token)
log.Debugf("Headers set")

// Initialize a new http client
client := &http.Client{}
// Send the request
resp, err := httpClient.Do(req)
if err != nil {
Expand All @@ -88,21 +95,21 @@ func UserCreate(deviceId string, local string) (*UserResponse, error) {
}

// Marshal the map to JSON
requestBody, err := json.Marshal(requestBodyMap)
requestBody, err := createJsonBody(requestBodyMap)
if err != nil {
log.Errorf("Error marshaling request body: %v", err)
return nil, err
}

// Create a new request
req, err := http.NewRequest("POST", userCreateUrl, bytes.NewBuffer(requestBody))
req, err := http.NewRequest("POST", userCreateUrl, requestBody)
if err != nil {
log.Errorf("Error creating new request: %v", err)
return nil, err
}

// Add headers
req.Header.Set("X-Lantern-Device-Id", deviceId)
req.Header.Set(headerDeviceId, deviceId)
log.Debugf("Headers set")

// Send the request
Expand All @@ -121,6 +128,81 @@ func UserCreate(deviceId string, local string) (*UserResponse, error) {
return &userResponse, nil
}

func PlansV3(deviceId string, userId string, local string, token string, countryCode string) (*PlansResponse, error) {
req, err := http.NewRequest("GET", plansV3Url, nil)
if err != nil {
log.Errorf("Error creating plans request: %v", err)
return nil, err
}
//Add query params
q := req.URL.Query()
q.Add("locale", local)
q.Add("countrycode", countryCode)
req.URL.RawQuery = q.Encode()

// Add headers
req.Header.Set(headerDeviceId, deviceId)
req.Header.Set(headerUserId, userId)
req.Header.Set(headerProToken, token)
log.Debugf("Plans Headers set")

// Send the request
resp, err := httpClient.Do(req)
if err != nil {
log.Errorf("Error sending plans request: %v", err)
return nil, err
}

defer resp.Body.Close()

var plans PlansResponse
// Read and decode the response body
if err := json.NewDecoder(resp.Body).Decode(&plans); err != nil {
log.Errorf("Error decoding response body: %v", err)
return nil, err
}
return &plans, nil
}

func PurchaseRequest(data map[string]string, deviceId string, userId string, token string) (*PurchaseResponse, error) {
log.Debugf("purchase request body %v", data)
body, err := createJsonBody(data)
if err != nil {
log.Errorf("Error while creating json body")
return nil, err
}

log.Debugf("Encoded body %v", body)
// Create a new request
req, err := http.NewRequest("POST", purchaseUrl, body)
if err != nil {
log.Errorf("Error creating new request: %v", err)
return nil, err
}

// Add headers
req.Header.Set(headerDeviceId, deviceId)
req.Header.Set(headerUserId, userId)
req.Header.Set(headerProToken, token)
req.Header.Set(headerContentType, "application/json")

resp, err := httpClient.Do(req)
if err != nil {
log.Errorf("Error sending puchase request: %v", err)
return nil, err
}

defer resp.Body.Close()

var purchase PurchaseResponse
// Read and decode the response body
if err := json.NewDecoder(resp.Body).Decode(&purchase); err != nil {
log.Errorf("Error decoding response body: %v", err)
return nil, err
}
return &purchase, nil
}

///Signup APIS

func Signup(signupBody *protos.SignupRequest, userId string, token string) (bool, error) {
Expand All @@ -138,21 +220,18 @@ func Signup(signupBody *protos.SignupRequest, userId string, token string) (bool
}

// Add headers
req.Header.Set("X-Lantern-User-Id", userId)
req.Header.Set("X-Lantern-Pro-Token", token)
req.Header.Set("Content-Type", "application/x-protobuf")

// Initialize a new http client
client := &http.Client{}
req.Header.Set(headerUserId, userId)
req.Header.Set(headerProToken, token)
req.Header.Set(headerContentType, "application/x-protobuf")
// Send the request
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
log.Errorf("Error sending user details request: %v", err)
return false, err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
Expand All @@ -179,13 +258,10 @@ func SignupEmailResendCode(signupEmailResendBody *protos.SignupEmailResendReques
}

// Add headers
req.Header.Set("Content-Type", "application/x-protobuf")
log.Debugf("Headers set")
req.Header.Set(headerContentType, "application/x-protobuf")

// Initialize a new http client
client := &http.Client{}
// Send the request
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
log.Errorf("Error sending user details request: %v", err)
return false, err
Expand All @@ -212,13 +288,11 @@ func SignupEmailConfirmation(signupEmailResendBody *protos.ConfirmSignupRequest)
}

// Add headers
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set(headerContentType, "application/x-protobuf")
log.Debugf("Headers set")

// Initialize a new http client
client := &http.Client{}
// Send the request
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
log.Errorf("Error sending user details request: %v", err)
return false, err
Expand All @@ -239,11 +313,10 @@ func GetSalt(email string) (*protos.GetSaltResponse, error) {
log.Errorf("Error getting user salt: %v", err)
return nil, err
}
req.Header.Set("Content-Type", "application/x-protobuf")
// Initialize a new http client
client := &http.Client{}
req.Header.Set(headerContentType, "application/x-protobuf")

// Send the request
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
log.Errorf("Error sending user details request: %v", err)
return nil, err
Expand All @@ -264,6 +337,7 @@ func GetSalt(email string) (*protos.GetSaltResponse, error) {
return &slatResponse, nil
}

// Login APIS
func LoginPrepare(prepareBody *protos.PrepareRequest) (*protos.PrepareResponse, error) {
// Marshal the map to JSON
requestBody, err := proto.Marshal(prepareBody)
Expand All @@ -277,11 +351,9 @@ func LoginPrepare(prepareBody *protos.PrepareRequest) (*protos.PrepareResponse,
log.Errorf("Error creating signup request: %v", err)
return nil, err
}
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set(headerContentType, "application/x-protobuf")

client := &http.Client{}
// Send the request
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
log.Errorf("Error sending login prepare request: %v", err)
return nil, err
Expand Down Expand Up @@ -320,11 +392,9 @@ func Login(loginBody map[string]interface{}) (*big.Int, error) {
log.Errorf("Error creating login request: %v", err)
return nil, err
}
req.Header.Set("Content-Type", "application/x-protobuf")
req.Header.Set(headerContentType, "application/x-protobuf")

client := &http.Client{}
// Send the request
resp, err := client.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
log.Errorf("Error sending login prepare request: %v", err)
return nil, err
Expand All @@ -339,32 +409,11 @@ func Login(loginBody map[string]interface{}) (*big.Int, error) {
return &srpB.SrpB, nil
}

// ToCurlCommand converts an http.Request into a cURL command string
func ToCurlCommand(req *http.Request) (string, error) {
_, err := httputil.DumpRequestOut(req, true)
// Utils methods convert json body
func createJsonBody(data map[string]string) (*bytes.Buffer, error) {
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}

curl := "curl -X " + req.Method
for header, values := range req.Header {
for _, value := range values {
curl += fmt.Sprintf(" -H '%s: %s'", header, value)
}
}

if req.Body != nil {
bodyBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
return "", err
}
// Reset the request body to the original io.Reader
req.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))

curl += fmt.Sprintf(" -d '%s'", string(bodyBytes))
return nil, err
}

curl += " " + req.URL.String()

return curl, nil
return bytes.NewBuffer(jsonData), nil
}
Loading

0 comments on commit 538b370

Please sign in to comment.