0

I've been trying without success to validate the email from my website against the MAGENTO customers API and throw a message (exist or not exist)

I mean, i have a website where customers are created; an email is given for every customer (The email is the primary key of every customer in MAGENTO) and everytime the customer is created in my website it is created in MAGENTO (using a middleware) too. The problem is that sometimes when a new customer is created in my website when it tries to create the customer in MAGENTO it throws an error message saying that the customer email already exist so i want to validate if the email exist or not in MAGENTO.

A few years ago i wrote this code and it validates whether the email exist or not but it is not using a MAGENTO API

Take a look:

<!DOCTYPE html>
<html>

<head>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<h2 style="color.darkgrey">Email validator </h2><br/>
<i id="status" class="" style="font-size:35px;color:green;display:inline-block;margin-left:5px;vertical-align:-5px;" ><i/>
<input id="email" type="email" class="form-control" style="display:inlane-block;width:300px;"  />
<button id="btn" class="btn btn-success">Validate</button>

</body>
<script type="text/javascript">
$("#btn").click(function (){
check();
});

$("#email").change(function (){
check();
});

</body>
<script type="text/javascript">
var restURL= "http://apilayer.net/api/check?access_key=c5118f1f9827f42a5fc4b231932130a8&email="+$("#email").val()+"&smtp=1&format=1"
$.ajax({
    type: 'GET',
    url: restURL,
    dataType:"json",
    success: renderList,
    });
    return false;
});

function renderList(data)
{
if((data.format_valid==true)&&(data.smtp_check==true))
    {
    $('#status').removeClass('fa fa-check-circle-o').addClass('fa fa-check-circle-o').css("color","green");
    }
    else
    {
    $('#status').removeClass('fa fa-check-circle-o').addClass('fa fa-check-circle-o').css("color","green");
    }
}


</script>
</html>

This is my MAGENTO API, take a look at this:

Image

This is an example of the JSON i got from MAGENTO when i click on "SEND" on POSTMAN:

{
        "magento_id": "54678",
        "firstname": "Richard",
        "lastname": "Herther",
        "email": "Richard.Herther@sels.com",
        "password": null,
        "website_id": "8",
        "group_id": "7",
        "disable_auto_group_change": "0",
        "prefix": null,
        "middlename": null,
        "suffix": null,
        "taxvat": null,
        "account_id": [
            "785784"
        ],
        "document": "66958746845",
        "document_type": "15",
        "gender": null,
        "telephone": null
    },

This is a brief explanation:

Image2

Could you please help me to solve this? I need to modify my code a little bit in order to use the MAGENTO API but i dont know how to do it.

Can you help me?

EDIT:

I know it is possible to use MAGENTO REST API through JAVASCRIPT (calling magento's xmlrpc apis) but it is not working for me.

Take a look at this:

$.xmlrpc({
    url: 'Your_magento_root/index.php/api/xmlrpc',
    methodName: 'login',
    params: ['my_username', "my_password"],
    success: function(response, status, jqXHR) {
        alert('Sucsses');       
        Session = response[0];            
    },
    error: function(jqXHR, status, error) {
        alert('error');
        console.log(status);  
    }
});

and

$.xmlrpc({
    url: 'Your_magento_root/index.php/api/xmlrpc',
    methodName: 'call',
    params: [Session, 'Your_methode_name',[0,'any other parameter',1]],
    success: function(response, status, jqXHR) {
        alert('Sucsses');
        var data = response[0];  
    },
    error: function(jqXHR, status, error) {
        alert('error-inside');
        console.log(error);
    }
});

But this is not working properly

Is it okay? what am i doing wrong?

Could you please help me to modify my html code in order to use MAGENTO REST API through JAVASCRIPT?

1

Please, Use API endpoint customers/isEmailAvailable to check whether the email is available on your Magento instance, see the below link for how to use it:

https://devdocs.magento.com/redoc/2.3/admin-rest-api.html#operation/customerAccountManagementV1IsEmailAvailablePost

See the example below, if you decide to use the JS script (Jquery Ajax):

$.ajax(
    {
        url: "http://funandfunction.com.test/rest/V1/customers/isEmailAvailable",
        type: 'POST',
        data: JSON.stringify({"customerEmail": "notexist@test.com"}),
        contentType: "application/json",
        dataType: "json",
        showLoader: true,
        success: function (response) {
            alert(response);
        }
    }
);

I have also made a video for the result, just in case you need https://drive.google.com/file/d/1cro38Km0_cTBT_k6s92HElKgAiD8rUCX/view

  • Thank you for replying!. Yeah, i've read that documentation before but it did not help me :/ – programming_amazing Aug 19 at 2:08
  • Can you post what you have tried using that API into the description? – Toan Tam Aug 19 at 2:09
  • Hi dude, the main problem is that i cannot modify my previous code in order to obtain the desired goal; i mean, the documentation you gave me is great (i've read it before) but everytime i try to make a new script it fails . Do you get me? – programming_amazing Aug 19 at 2:19
  • Yes, I got you, but I'm not sure what did you try since you didn't post the code snippet to call the API into the question? – Toan Tam Aug 19 at 2:21
  • Did you use ajax JS script or PHP script to call to that API endpoint? And what is the script that you have tried? – Toan Tam Aug 19 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.