Created
March 19, 2019 06:04
-
-
Save cergey-obr/9df09d3f761a95be2cd9182aab29f8dc to your computer and use it in GitHub Desktop.
Migrate customer tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'abstract.php'; | |
class Optimax_Shell_Zooz extends Mage_Shell_Abstract | |
{ | |
const MIGRATE_URL = 'https://app.zooz.com/mobile/optimax_migrate_token.jsp'; | |
public function run() | |
{ | |
$customerId = $this->getArg('id'); | |
$appEmulation = Mage::getSingleton('core/app_emulation'); | |
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation(1); | |
$api = Mage::getModel('payments/api'); | |
$cards = $api->getPaymentMethods($customerId); | |
foreach ($cards as $card) { | |
if ($token = $card->getData('paymentMethodToken')) { | |
$this->migrate((string)$customerId, $token); | |
} | |
} | |
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo); | |
} | |
/** | |
* @param string $customerId | |
* @param string $token | |
* | |
* @return bool | |
*/ | |
private function migrate(string $customerId, string $token) | |
{ | |
echo "Start migrate $token ...\r\n"; | |
$requestData = [ | |
'paymentMethodToken' => $token, | |
'customerLoginId' => $customerId, | |
'firstName' => 'Sergey', | |
'lastName' => 'Obraztsov', | |
'email' => 'cergey.obr@gmail.com', | |
]; | |
try { | |
$response = $this->getApiClient() | |
->setRawData(http_build_query($requestData), Zend_Http_Client::ENC_URLENCODED) | |
->request(); | |
$responseBody = json_decode($response->getBody(), true) ?: []; | |
if (!$response->isSuccessful()) { | |
echo "Fail response: " . print_r($responseBody, 1) . "\r\n"; | |
throw new Zooz_Payments_Exception($this->prepareErrorMessage($response), $response->getStatus()); | |
} else { | |
echo "Success response: " . print_r($responseBody, 1) . "\r\n"; | |
} | |
echo "Success finish migrate $token ...\r\n"; | |
return true; | |
} catch (Exception $e) { | |
echo "Error with message: " . $e->getMessage() . "\r\n"; | |
} | |
echo "Fail finish migrate $token ...\r\n"; | |
return false; | |
} | |
/** | |
* @return Varien_Http_Client | |
* @throws Zend_Http_Client_Exception | |
*/ | |
private function getApiClient(): Varien_Http_Client | |
{ | |
$client = new Varien_Http_Client(); | |
$client->setUri(self::MIGRATE_URL); | |
$client->setHeaders([ | |
'programId' => $this->getConfigData('program_id'), | |
'ZoozServerAPIKey' => Mage::helper('core')->decrypt($this->getConfigData('program_key')), | |
]); | |
$client->setMethod(Zend_Http_Client::POST); | |
return $client; | |
} | |
/** | |
* @param array $responseBody | |
* | |
* @return string | |
*/ | |
private function prepareErrorMessage(array $responseBody): string | |
{ | |
$errorMessage = 'Unexpected error from migrate api'; | |
if (($error = $responseBody['error'] ?? null) && is_array($error)) { | |
$errorMessage = implode(': ', $error['paymentsos'] ?? []); | |
} | |
return $errorMessage; | |
} | |
/** | |
* @param string $field | |
* | |
* @return mixed | |
*/ | |
private function getConfigData(string $field) | |
{ | |
return Mage::getStoreConfig(Zooz_Payments_Model_Api::XML_PATH_CONFIG_GROUP . $field); | |
} | |
/** | |
* View help text | |
* @return string | |
*/ | |
public function usageHelp(): string | |
{ | |
return <<<USAGE | |
Usage: php -f zooz.php -- [options] | |
--id customer zooz id | |
help This help | |
USAGE; | |
} | |
} | |
$shell = new Optimax_Shell_Zooz(); | |
$shell->runSingle(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment