Created
June 25, 2017 13:51
-
-
Save Realetive/4f7378a0fe8870398395c9da031b76f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
switch ($modx->event->name) { | |
case "msOnChangeOrderStatus": | |
// API сервиса смс-рассылок | |
$url = $modx->getOption("mirSMS__url"); | |
$source = $modx->getOption("mirSMS__source"); | |
$login = $modx->getOption("mirSMS__login"); | |
$password = $modx->getOption("mirSMS__password"); | |
$orderObject = $modx->getObject("msOrder", $order->get( 'id' ) ); | |
$order = $orderObject->toArray(); | |
$orderNumber = $order[ "num" ]; | |
$orderCost = $order[ "cost" ]; | |
$orderCartCost = $order[ "cart_cost" ]; | |
$orderDelivery = $order[ "delivery_cost" ]; | |
$orderWeight = $order[ "weight" ]; | |
$orderAddress = $orderObject->getOne("Address")->toArray(); | |
$userName = $orderAddress[ "receiver" ]; | |
$userPhone = $orderAddress[ "phone" ]; | |
$userComment = $orderAddress[ "comment" ]; | |
$deliveryType = $modx->getObject( 'msDelivery', $order[ "delivery" ] )->toArray(); | |
$deliveryName = $deliveryType[ "name" ]; | |
$placeholders = array( | |
"order" => $orderNumber // номер заказа | |
, "cost" => $orderCost // итоговая стоимость | |
, "cart" => $orderCartCost // стоимость товаров | |
, "delivery" => $orderDelivery // стоимость доставки | |
, "deliveryType" => $deliveryName // тип доставки или самовывоз | |
, "weight" => $orderWeight // вес | |
, "name" => $userName // имя пользователя | |
, "phone" => $userPhone // телефон пользователя | |
, "comment" => $userComment // комментарий пользователя к заказу | |
); | |
$messageXML = ""; | |
$pdoTools = $modx->getService('pdoTools'); | |
if ( $modx->getOption( 'mirSMS__manager-message' ) && $status == $modx->getOption( "mirSMS__status_new" ) ) { | |
$tpl = "@INLINE " . $modx->getOption( 'mirSMS__message_manager' ); | |
$text = $pdoTools->getChunk( $tpl, $placeholders );; | |
$managerPhoneArray = array_map( "trim" , explode( ",", $modx->getOption( "mirSMS__manager-phone" ) ) ); | |
foreach ( $managerPhoneArray as $key => $managerPhone ) { | |
$messageXML .= "<sms sms_id=\"new_" . $orderNumber . "_" . $userPhone . "\" number=\"" . $managerPhone . "\" source_number=\"" . $source . "\" >" . $text . "</sms>\n"; | |
} | |
} | |
if ( $status == $modx->getOption( "mirSMS__status_success" ) ) { | |
$tpl = "@INLINE " . $modx->getOption( 'mirSMS__message_success' ); | |
$text = $pdoTools->getChunk( $tpl, $placeholders );; | |
$messageXML = "<sms sms_id=\"success_" . $orderNumber . "_" . $userPhone . "\" number=\"" . $userPhone . "\" source_number=\"" . $source . "\" >" . $text . "</sms>\n"; | |
} | |
if ( $status == $modx->getOption( "mirSMS__status_reject" ) ) { | |
$tpl = "@INLINE " . $modx->getOption( 'mirSMS__message_reject' ); | |
$text = $pdoTools->getChunk( $tpl, $placeholders );; | |
$messageXML = "<sms sms_id=\"reject_" . $orderNumber . "_" . $userPhone . "\" number=\"" . $userPhone . "\" source_number=\"" . $source . "\" >" . $text . "</sms>\n"; | |
} | |
$smsXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"; | |
$smsXML .= "<xml_request name=\"sms_send\">\n"; | |
$smsXML .= "<xml_user lgn=\"" . $login . "\" pwd=\"" . $password . "\"/>\n"; | |
$smsXML .= $messageXML; | |
$smsXML .= "</xml_request>"; | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, $url ); | |
curl_setopt( $ch, CURLOPT_POST, true ); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded; charset=\"utf-8\"") ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, htmlspecialchars_decode( $smsXML ) ); | |
$response = curl_exec( $ch ); | |
if ( curl_errno( $ch ) ) { | |
$modx->log(modX::LOG_LEVEL_ERROR, "Error in plugin `onOrderSMS`: " . curl_error( $ch ) ); | |
} else { | |
curl_close( $ch ); | |
} | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment