I am using Magento SOAP API v2 for web services. I want to update customer password API.
For this I have created a function as follows :
$complexFilter = array(
'complex_filter' => array(
array(
'key' => 'email',
'value' => array('key' => 'eq', 'value' => $customer_email)
)
)
);
$customerData = $soapClient->customerCustomerList($sessionId, $complexFilter);
$match = customerPasswordMatching($customer_old_pwd,$customerData[0]->password_hash); //it return true if password match
if($match == TRUE)
{
$customerInfo = array(
'firstname' => $first_name,
'password' => $customer_new_pwd,
'website_id' => $customerData[0]->website_id,
'store_id' => $customerData[0]->store_id,
'group_id' => $customerData[0]->group_id);
$result = $soapClient->customerCustomerUpdate($sessionId, $customer_id ,$customerInfo );
return $result;
}
else
{
return array('Success' => 0, 'Message' => 'Wrong Password');
}
This code update customer 'firstname' but not update customer password.
Any one can provide me details how can I update customer password in Magento soap API v2?
2 Answers 2
You've to create a module for this to get it working:
etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Vendor_ChangePasswordWithApi>
<version>0.1.0</version>
</Vendor_ChangePasswordWithApi>
</modules>
<global>
<models>
<customer>
<rewrite>
<customer_api>Vendor_ChangePasswordWithApi_Model_Customer_Api</customer_api>
<customer_api_v2>Vendor_ChangePasswordWithApi_Model_Customer_Api_V2</customer_api_v2>
</rewrite>
</customer>
</models>
</global>
</config>
Model/Customer/Api.php
<?php
class Vendor_ChangePasswordWithApi_Model_Customer_Api extends Mage_Customer_Model_Customer_Api
{
public function update($customerId, $customerData)
{
$customerData = $this->_prepareData($customerData);
$customer = Mage::getModel('customer/customer')->load($customerId);
if (!$customer->getId()) {
$this->_fault('not_exists');
}
foreach ($this->getAllowedAttributes($customer) as $attributeCode=>$attribute) {
if (isset($customerData[$attributeCode])) {
$customer->setData($attributeCode, $customerData[$attributeCode]);
}
}
// Added password support
if(isset($customerData['password'])){
$customer->setPassword($customerData['password']);
}
//
$customer->save();
return true;
}
}
Model/Customer/Api/V2.php
<?php
class Vendor_ChangePasswordWithApi_Model_Customer_Api_V2 extends Mage_Customer_Model_Customer_Api_V2 {
public function update($customerId, $customerData)
{
$customerData = $this->_prepareData($customerData);
$customer = Mage::getModel('customer/customer')->load($customerId);
if (!$customer->getId()) {
$this->_fault('not_exists');
}
foreach ($this->getAllowedAttributes($customer) as $attributeCode=>$attribute) {
if (isset($customerData[$attributeCode])) {
$customer->setData($attributeCode, $customerData[$attributeCode]);
}
}
// Added password support
if(isset($customerData['password'])){
$customer->setPassword($customerData['password']);
}
//
$customer->save();
return true;
}
}
I've striped this code out of a project of mine, that project is on EE 1.14.2.0. So maybe it needs some changes for other versions. Check the original update() functions in the Mage_Customer_Model_Customer_Api and Mage_Customer_Model_Customer_Api_V2 classes. I've only added the commented part:
if(isset($customerData['password'])){
$customer->setPassword($customerData['password']);
}
-
Thanks for reply. Can you tell me how to call
updatefunction in my webservice?Vinaya Maheshwari– Vinaya Maheshwari2015年10月24日 06:31:13 +00:00Commented Oct 24, 2015 at 6:31 -
It just works with your example code. The code is used by the
customerCustomerUpdate()function your using.Roy– Roy2015年10月25日 12:14:30 +00:00Commented Oct 25, 2015 at 12:14
Use below function to update customer password via Custom Magento API :
/* This method is responsible to update customer account password
@Ahsan Horani
*/
public function changePassword($info)
{
$customer_email = $info['customer_email'];
$customer_old_pswd = $info['customer_old_pswd'];
$customer_new_pswd = $info['customer_new_pswd'];
try
{
//Authenticate User Email and Old Password
$authenticate = Mage::getModel('customer/customer')->setWebsiteId(1)->authenticate($customer_email, $customer_old_pswd);
}
catch(Exception $e)
{
return "fail"; //Failed authentication
}
if($authenticate)
{
//Load Customer by Email
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(1); //Customers are based on Website
$customer->loadByEmail($customer_email);
// Check if customer exists
if ($customer->getId())
{
$customer = Mage::getModel('customer/customer')->load($customer->getId());
$customer->setEmail($customer_email);
$customer->setPassword($customer_new_pswd);
try
{
$customer->save();
return "success";
}
catch (Exception $ex)
{
return "error";
}
}
else
{
return "error";
}
}
else
{
return "fail";
}
}
Use below code to call custom API from external script e.g. from Slim framework
// GET route
$app->post(
'/change-password',
function () use ($app) {
$info['customer_email'] = $app->request()->post('email');
$info['customer_new_pswd'] = $app->request()->post('new_password');
$info['customer_old_pswd'] = $app->request()->post('old_password');
$msg = 'Password updated successfully'; //Default message
if($info['customer_email'] == "" || $info['customer_old_pswd'] == "" || $info['customer_new_pswd'] == "")
{
$msg = 'Please provide proper customer info';
$responseData = array('message'=>$msg,'status'=>'error');
$responseData = json_encode($responseData);
print_r($responseData);
die();
}
try
{
if ($info['customer_email'] && $info['customer_old_pswd'] && $info['customer_new_pswd'])
{
$response = changePassword($info);
}
}
catch (Exception $e)
{
$msg = $e->getMessage();
$responseData = array('message'=>$msg,'status'=>'error');
$responseData = json_encode($responseData);
print_r($responseData);
die();
}
if($response == 'error')
{
$msg = 'Unable to update password';
}
else if($response == 'fail')
{
$msg = 'Wrong Password';
}
$responseData = array('message'=>$msg,'status'=>$response);
$responseData = json_encode($responseData);
print_r($responseData); //Print response from API
}
);
/* Change customer password using custom API */
function changePassword($info)
{
$client_v1 = new SoapClient(MAGENTO_API_URL);
$session_id_v1 = $client_v1->login(MAGENTO_API_V2_USERNAME, MAGENTO_API_V2_PASSWORD);
$responseData = $client_v1->call($session_id_v1, 'customapi.changePassword',array($info));
return $responseData;
}