How can I programatically set login and trans_key for authorizenet payment method config?
-
09-10-2020 - |
Question
I am trying to automate the config and deployment of a Magento 1.9 store. I have a load script that does everything I need it to except for configuring the authorize.net payment method's login and trans_key properties.
Here is a sample code block that configures everything except the credentials (123456 in this example):
// already have Mage
Mage::getConfig()
->saveConfig('payment/authorizenet/active', '1', 'default', 0)
->saveConfig('payment/authorizenet/payment_action', 'authorize_capture', 'default', 0)
->saveConfig('payment/authorizenet/login', '123456', 'default', 0)
->saveConfig('payment/authorizenet/merchant_email', 'me@home.com', 'default', 0)
->saveConfig('payment/authorizenet/title', 'Credit Card (Authorize.net)', 'default', 0)
->saveConfig('payment/authorizenet/trans_key', '123456', 'default', 0)
->saveConfig('payment/authorizenet/cgi_url', 'https://test.authorize.net/gateway/transact.dll', 'default', 0)
->saveConfig('payment/authorizenet/cgi_url_td', 'https://api.authorize.net/xml/v1/request.api', 'default', 0)
->saveConfig('payment/authorizenet/order_status', 'processing', 'default', 0)
->saveConfig('payment/authorizenet/test', '0', 'default', 0)
->saveConfig('payment/authorizenet/debug', '0', 'default', 0)
->saveConfig('payment/authorizenet/currency', 'USD', 'default', 0)
->saveConfig('payment/authorizenet/email_customer', '0', 'default', 0)
->saveConfig('payment/authorizenet/cctypes', 'AE,VI,MC,DI', 'default', 0)
->saveConfig('payment/authorizenet/allowspecific', '0', 'default', 0)
->saveConfig('payment/authorizenet/useccv', '0', 'default', 0)
->saveConfig('payment/authorizenet/min_order_total', '0', 'default', 0)
->saveConfig('payment/authorizenet/min_order_total', '0', 'default', 0)
->saveConfig('payment/authorizenet/sort_order', NULL, 'default', 0)
->saveConfig('payment/authorizenet/allow_partial_authorization', '0', 'default', 0)
->saveConfig('payment/authorizenet/centinel', '0', 'default', 0);
Everything besides login and trans_key gets successfully set this way, but I need those fields to be set too.
How do I set the payment gateway credentials programatically assuming I have access to the Magento core API?
Solution
Both fields use <backend_model>adminhtml/system_config_backend_encrypted</backend_model>
If you take a look at Mage_Adminhtml_Model_System_Config_Backend_Encrypted
, you can find:
/**
* Encrypt value before saving
*
*/
protected function _beforeSave()
{
$value = (string)$this->getValue();
// don't change value, if an obscured value came
if (preg_match('/^\*+$/', $this->getValue())) {
$value = $this->getOldValue();
}
if (!empty($value) && ($encrypted = Mage::helper('core')->encrypt($value))) {
$this->setValue($encrypted);
}
}
Please try this ... replace (and same for login
)
->saveConfig('payment/authorizenet/trans_key', '123456', 'default', 0)
with
->saveConfig('payment/authorizenet/trans_key', Mage::helper('core')->encrypt('123456'), 'default', 0)
On load it will be decrypted via:
/**
* Decrypt value after loading
*
*/
protected function _afterLoad()
{
$value = (string)$this->getValue();
if (!empty($value) && ($decrypted = Mage::helper('core')->decrypt($value))) {
$this->setValue($decrypted);
}
}