Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ea48de5 authored by Nivesh Krishna's avatar Nivesh Krishna
Browse files

send wp request for rewarding user on signup

parent ba84b9d9
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ To be able to create an account on WP, set the ENV variables:
1. `E_SHOP_URL`, ex: "http://localhost:1080/"
1. `E_SHOP_USERNAME` with the privileged user account able to create other accounts, ex: "root"
1. `E_SHOP_APP_PASS` the user application password created as described in [here](https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/)
1. `E_SHOP_ENABLE_REWARDS: true/false` set this to true to enable referral signup rewards 

Usage
-----
+1 −5
Original line number Diff line number Diff line
@@ -144,9 +144,7 @@ $(document).ready(function () {
          $("#submitButton").attr("disabled", false);
        } else {

          const create_gitlab_account = $("#create-gitlab-account").is(":checked");
          const create_eshop_account = searchParams.has('ref');
          const refCode = create_eshop_account? searchParams.get('ref') : null;
          const refCode = searchParams.has('ref')? searchParams.get('ref') : null;

          //post data
          url = $("#registrationform").attr("action");
@@ -160,8 +158,6 @@ $(document).ready(function () {
              repassword: repassword,
              authmail: authmail,
              authsecret: authsecret,
              create_gitlab_account: create_gitlab_account,
              create_eshop_account: create_eshop_account,
              ref: refCode,
            },
            function (response) {
+0 −12
Original line number Diff line number Diff line
@@ -118,18 +118,6 @@ layout: default
            {% include flash.html %}
          </div>
        </div>
        <div class="field is-hidden">
          <div class="columns control">
            <input class="column" type="checkbox" id="create-eshop-account" name="create-eshop-account">
            <label class="column is-11" for="create-eshop-account">{% t form_create_eshop_account %}</label>
          </div>
        </div>
        <div class="field is-hidden">
          <div class="columns control">
            <input class="column" type="checkbox" id="create-gitlab-account" name="create-gitlab-account">
            <label class="column is-11" for="create-gitlab-account">{% t form_create_gitlab_account %}</label>
          </div>
        </div>
        
      </div>
      <div class="field has-text-centered" id="buttonField">
+16 −0
Original line number Diff line number Diff line
@@ -156,6 +156,13 @@ function shouldCreateEShopAccount(): bool
        $E_SHOP_APP_PASS !== false;
}

function shouldRewardReferrer(object $userData): bool
{
    $enableRewards = filter_var(getenv("E_SHOP_ENABLE_REWARDS"), FILTER_VALIDATE_BOOLEAN);
    return $enableRewards !== false &&
        $userData->referrerCode != null;
}

function createAccounts(array $accountsCreators, object $userData)
{
    foreach($accountsCreators as $accountCreator)
@@ -168,6 +175,15 @@ function createAccounts(array $accountsCreators, object $userData)
            sendAPIResponse(400, $response);
        }
    }

    if (shouldRewardReferrer($userData))
    {
        $E_SHOP_URL = getenv("E_SHOP_URL");
        $E_SHOP_USERNAME = getenv("E_SHOP_USERNAME");
        $E_SHOP_APP_PASS = getenv("E_SHOP_APP_PASS");
        $wpShop = new \WPAccountCreator($E_SHOP_URL, $E_SHOP_USERNAME, $E_SHOP_APP_PASS);
        $status = $wpShop->sendWPRewardRequest($userData);
    }
}

function validateAccountOnAllServices(array $accountsCreators, object $userData)
+36 −1
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ class WPAccountCreator implements AccountCreator
    {
        $this->wordPressUrl = endsWith($wordPressUrl, "/") ? $wordPressUrl : $wordPressUrl . "/";
        $this->wordPressUserUrl = $this->wordPressUrl . "?rest_route=/wp/v2/users";
        $this->wordPressReferralRewardsUrl = $this->wordPressUrl . "?rest_route=/referral-program/v1/reward-referrer";
        $this->wordPressCredentials = base64_encode($username . ":" . $appPassword);
    }
    public function validateData(object $userData): ValidatedData
@@ -76,7 +77,7 @@ class WPAccountCreator implements AccountCreator
        return $users;
    }

    public function getRefCodeCookie(object $userData) {
    private function getRefCodeCookie(object $userData) {
        $url = $this->wordPressUrl;
        $url .= "?&ref=" . $userData->referrerCode;
        $curl = curl_init();
@@ -103,6 +104,40 @@ class WPAccountCreator implements AccountCreator
   
    }

    public function sendWPRewardRequest(object $userData): bool
    {
        if ($userData->referrerCode == null) {
            return false;
        }
        $curl = curl_init();
        $url = $this->wordPressReferralRewardsUrl;
        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode(array(
                "ref_code" => $userData->referrerCode,
                "ecloud_username" => $userData->email,
            )),
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
                "content-type: application/json",
                "Authorization: Basic " . $this->wordPressCredentials                
            ),
        ));
        $res = curl_exec($curl);
        $statusCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
        $err = curl_error($curl);
        if (!empty($err) || $statusCode != 200) {
            return false;
        }
        return true;
    }
            

    private function sendWPRequest(object $userData): object
    {