在Checkout上使用Opencart 2.1.0.1中的cURL发送短信

时间:2017-06-01 05:19:02

标签: php curl opencart

我需要了解如何在Opencart 2.1.0.1中成功完成结账时向客户发送通知(结帐/成功)

我在网上阅读了一些论坛和开发人员帖子,因此对我的目录/ controller / checkout / success.php 文件进行了更改

class ControllerCheckoutSuccess extends Controller {
public function index() {
$this->load->language('checkout/success');

if (isset($this->session->data['order_id'])) {

我在这些行之间添加的代码

            $this->load->model('account/customer');
$cnum = $this->customer->gettelephone(); // get customer mobile number
$getorderid = $this->session->data['order_id']; // get order id


            //URL to shorten
$track_url = "https://www.example.com/index.php?route=account/order/info&order_id=".$getorderid; 

/////////////////////////////////


// EDIT THIS: your auth parameters
$signature = '4a6fe0b26c';

// EDIT THIS: the query parameters
$keyword = '';                        // optional keyword
$title   = '';                // optional, if omitted YOURLS will lookup title with an HTTP request
$format  = 'json';                       // output format: 'json', 'xml' or 'simple'

// EDIT THIS: the URL of the API file
$api_url = 'http://abc.abc/yourls-api.php';

// Init the CURL session
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );            // No header in the result
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
curl_setopt( $ch, CURLOPT_POST, 1 );              // This is a POST request
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(      // Data to POST
        'url'      => $track_url,
        'keyword'  => $keyword,
        'title'    => $title,
        'format'   => $format,
        'action'   => 'shorturl',
        'signature' => $signature,
    ) );

// Fetch and return content
$data = curl_exec($ch);
$json = json_decode($data);
$order_track_url = $json->shorturl;
curl_close($ch);

            $msg = "Your order $getorderid has been successfully placed and will be shipped once verified. Shipping is expected in 48 hours and Delivery in 5-6 days
            Track your order $order_track_url";
            $encode= rawurlencode($msg);
//request parameters array
$requestParams = array(
    'channel' => '2',
    'APIKey' => 'gatewaykey',
    'senderid' => 'ABCDEF',
    'Number' => $cnum,
    'Text' => $encode,
    'DCS' => '0',
    'Route' => '1',
    'Flashsms' => '0',
);

//merge API url and parameters
$apiUrl = "https://login.smscompanyapilink.com/api/mt/SendSMS?";
foreach($requestParams as $key => $val){
    $apiUrl .= $key.'='.($val).'&';
}
$apiUrl = rtrim($apiUrl, "&");

//API call
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$apiUrl);
 curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,0);

$data = curl_exec($ch);
if (curl_errno($ch) > 0) {
    print 'There was a cURL error: ' . $curl_error($ch);
}

$json = json_decode($data);
$JobID = $json->{'JobId'};
curl_close($ch);
  

$this->cart->clear();

新订单上的客户提醒电子邮件代码目录/型号/ checkout / order.php

// If order status is 0 then becomes greater than 0 send main html email
        if (!$order_info['order_status_id'] && $order_status_id) {
            // Check for any downloadable products
            $download_status = false;

            $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");

            foreach ($order_product_query->rows as $order_product) {
                // Check if there are any linked downloads
                $product_download_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_download` WHERE product_id = '" . (int)$order_product['product_id'] . "'");

                if ($product_download_query->row['total']) {
                    $download_status = true;
                }
            }

            // Load the language for any mails that might be required to be sent out
            $language = new Language($order_info['language_directory']);
            $language->load($order_info['language_directory']);
            $language->load('mail/order');

            $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$order_info['language_id'] . "'");

            if ($order_status_query->num_rows) {
                $order_status = $order_status_query->row['name'];
            } else {
                $order_status = '';
            }

            $subject = sprintf($language->get('text_new_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);

            // HTML Mail
            $data = array();

            $data['title'] = sprintf($language->get('text_new_subject'), $order_info['store_name'], $order_id);

            $data['text_greeting'] = sprintf($language->get('text_new_greeting'), $order_info['store_name']);
            $data['text_link'] = $language->get('text_new_link');
            $data['text_download'] = $language->get('text_new_download');
            $data['text_order_detail'] = $language->get('text_new_order_detail');
            $data['text_instruction'] = $language->get('text_new_instruction');
            $data['text_order_id'] = $language->get('text_new_order_id');
            $data['text_date_added'] = $language->get('text_new_date_added');
            $data['text_payment_method'] = $language->get('text_new_payment_method');
            $data['text_shipping_method'] = $language->get('text_new_shipping_method');
            $data['text_email'] = $language->get('text_new_email');
            $data['text_telephone'] = $language->get('text_new_telephone');
            $data['text_ip'] = $language->get('text_new_ip');
            $data['text_order_status'] = $language->get('text_new_order_status');
            $data['text_payment_address'] = $language->get('text_new_payment_address');
            $data['text_shipping_address'] = $language->get('text_new_shipping_address');
            $data['text_product'] = $language->get('text_new_product');
            $data['text_model'] = $language->get('text_new_model');
            $data['text_quantity'] = $language->get('text_new_quantity');
            $data['text_price'] = $language->get('text_new_price');
            $data['text_total'] = $language->get('text_new_total');
            $data['text_footer'] = $language->get('text_new_footer');

            $data['logo'] = $this->config->get('config_url') . 'image/' . $this->config->get('config_logo');
            $data['store_name'] = $order_info['store_name'];
            $data['store_url'] = $order_info['store_url'];
            $data['customer_id'] = $order_info['customer_id'];
            $data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id;

            if ($download_status) {
                $data['download'] = $order_info['store_url'] . 'index.php?route=account/download';
            } else {
                $data['download'] = '';
            }

            $data['order_id'] = $order_id;
            $data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added']));
            $data['payment_method'] = $order_info['payment_method'];
            $data['shipping_method'] = $order_info['shipping_method'];
            $data['email'] = $order_info['email'];
            $data['telephone'] = $order_info['telephone'];
            $data['ip'] = $order_info['ip'];
            $data['order_status'] = $order_status;

            if ($comment && $notify) {
                $data['comment'] = nl2br($comment);
            } else {
                $data['comment'] = '';
            }

            if ($order_info['payment_address_format']) {
                $format = $order_info['payment_address_format'];
            } else {
                $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
            }

            $find = array(
                '{firstname}',
                '{lastname}',
                '{company}',
                '{address_1}',
                '{address_2}',
                '{city}',
                '{postcode}',
                '{zone}',
                '{zone_code}',
                '{country}'
            );

            $replace = array(
                'firstname' => $order_info['payment_firstname'],
                'lastname'  => $order_info['payment_lastname'],
                'company'   => $order_info['payment_company'],
                'address_1' => $order_info['payment_address_1'],
                'address_2' => $order_info['payment_address_2'],
                'city'      => $order_info['payment_city'],
                'postcode'  => $order_info['payment_postcode'],
                'zone'      => $order_info['payment_zone'],
                'zone_code' => $order_info['payment_zone_code'],
                'country'   => $order_info['payment_country']
            );

            $data['payment_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

            if ($order_info['shipping_address_format']) {
                $format = $order_info['shipping_address_format'];
            } else {
                $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
            }

            $find = array(
                '{firstname}',
                '{lastname}',
                '{company}',
                '{address_1}',
                '{address_2}',
                '{city}',
                '{postcode}',
                '{zone}',
                '{zone_code}',
                '{country}'
            );

            $replace = array(
                'firstname' => $order_info['shipping_firstname'],
                'lastname'  => $order_info['shipping_lastname'],
                'company'   => $order_info['shipping_company'],
                'address_1' => $order_info['shipping_address_1'],
                'address_2' => $order_info['shipping_address_2'],
                'city'      => $order_info['shipping_city'],
                'postcode'  => $order_info['shipping_postcode'],
                'zone'      => $order_info['shipping_zone'],
                'zone_code' => $order_info['shipping_zone_code'],
                'country'   => $order_info['shipping_country']
            );

            $data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

            $this->load->model('tool/upload');

            // Products
            $data['products'] = array();

            foreach ($order_product_query->rows as $product) {
                $option_data = array();

                $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'");

                foreach ($order_option_query->rows as $option) {
                    if ($option['type'] != 'file') {
                        $value = $option['value'];
                    } else {
                        $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

                        if ($upload_info) {
                            $value = $upload_info['name'];
                        } else {
                            $value = '';
                        }
                    }

                    $option_data[] = array(
                        'name'  => $option['name'],
                        'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
                    );
                }

                $data['products'][] = array(
                    'name'     => $product['name'],
                    'model'    => $product['model'],
                    'option'   => $option_data,
                    'quantity' => $product['quantity'],
                    'price'    => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
                    'total'    => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
                );
            }

            // Vouchers
            $data['vouchers'] = array();

            $order_voucher_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");

            foreach ($order_voucher_query->rows as $voucher) {
                $data['vouchers'][] = array(
                    'description' => $voucher['description'],
                    'amount'      => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']),
                );
            }

            // Order Totals
            $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");

            foreach ($order_total_query->rows as $total) {
                $data['totals'][] = array(
                    'title' => $total['title'],
                    'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
                );
            }

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order.tpl')) {
                $html = $this->load->view($this->config->get('config_template') . '/template/mail/order.tpl', $data);
            } else {
                $html = $this->load->view('default/template/mail/order.tpl', $data);
            }

            // Text Mail
            $text  = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')) . "\n\n";
            $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
            $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
            $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";

            if ($comment && $notify) {
                $text .= $language->get('text_new_instruction') . "\n\n";
                $text .= $comment . "\n\n";
            }

            // Products
            $text .= $language->get('text_new_products') . "\n";

            foreach ($order_product_query->rows as $product) {
                $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";

                $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . $product['order_product_id'] . "'");

                foreach ($order_option_query->rows as $option) {
                    if ($option['type'] != 'file') {
                        $value = $option['value'];
                    } else {
                        $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

                        if ($upload_info) {
                            $value = $upload_info['name'];
                        } else {
                            $value = '';
                        }
                    }

                    $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "\n";
                }
            }

            foreach ($order_voucher_query->rows as $voucher) {
                $text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
            }

            $text .= "\n";

            $text .= $language->get('text_new_order_total') . "\n";

            foreach ($order_total_query->rows as $total) {
                $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
            }

            $text .= "\n";

            if ($order_info['customer_id']) {
                $text .= $language->get('text_new_link') . "\n";
                $text .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
            }

            if ($download_status) {
                $text .= $language->get('text_new_download') . "\n";
                $text .= $order_info['store_url'] . 'index.php?route=account/download' . "\n\n";
            }

            // Comment
            if ($order_info['comment']) {
                $text .= $language->get('text_new_comment') . "\n\n";
                $text .= $order_info['comment'] . "\n\n";
            }

            $text .= $language->get('text_new_footer') . "\n\n";

            $mail = new Mail();
            $mail->protocol = $this->config->get('config_mail_protocol');
            $mail->parameter = $this->config->get('config_mail_parameter');
            $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
            $mail->smtp_username = $this->config->get('config_mail_smtp_username');
            $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
            $mail->smtp_port = $this->config->get('config_mail_smtp_port');
            $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');

            $mail->setTo($order_info['email']);
            $mail->setFrom($this->config->get('config_email'));
            $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
            $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
            $mail->setHtml($html);
            $mail->setText($text);
            $mail->send();

&GT;管理员提醒邮件代码

我的短信代码

                                 $this->load->model('account/customer');
$cnum = $data['text_telephone'] = $language->get('text_new_telephone');
$getorderid = $data['text_order_id'] = $language->get('text_new_order_id');


//////   URL to shorten
$track_url = $order_info['store_url'] . 'index.php?route=account/order/info&order_id='. $order_id 

/////////////////////////////////


// EDIT THIS: your auth parameters
$signature = 'e4635e2730';

// EDIT THIS: the query parameters
$keyword = '';                        // optional keyword
$title   = '';                // optional, if omitted YOURLS will lookup title with an HTTP request
$format  = 'json';                       // output format: 'json', 'xml' or 'simple'

// EDIT THIS: the URL of the API file
$api_url = 'http://apna.co/yourls-api.php';

// Init the CURL session
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );            // No header in the result
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
curl_setopt( $ch, CURLOPT_POST, 1 );              // This is a POST request
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(      // Data to POST
        'url'      => $track_url,
        'keyword'  => $keyword,
        'title'    => $title,
        'format'   => $format,
        'action'   => 'shorturl',
        'signature' => $signature,
    ) );

// Fetch and return content
$data = curl_exec($ch);
$json = json_decode($data);
$order_track_url = $json->shorturl;
curl_close($ch);

            $msg = "Your order ".$getorderid." has been successfully placed and will be shipped once verified. Shipping is expected in 48 hours and Delivery in 5-6 days.
            Track your order ".$order_track_url;
            $encode= rawurlencode($msg);
//request parameters array
$requestParams = array(
    'channel' => '2',
    'APIKey' => 'J8yAjtZkTUqMnDy6SDQN6Q',
    'senderid' => 'APNAPR',
    'Number' => $cnum,
    'Text' => $encode,
    'DCS' => '0',
    'Route' => '1',
    'Flashsms' => '0',
);

//merge API url and parameters
$apiUrl = 'https://login.smscompanyapilink.com/api/mt/SendSMS?';
foreach($requestParams as $key => $val){
    $apiUrl .= $key.'='.($val).'&';
}
$apiUrl = rtrim($apiUrl, "&");

//API call
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$apiUrl);
 curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,0);

$data = curl_exec($ch);
if (curl_errno($ch) > 0) {
    print 'There was a cURL error: ' . $curl_error($ch);
}

$json = json_decode($data);
$JobID = $json->{'JobId'};
curl_close($ch);
  

关闭闭合大括号

网址短片和短信在所有其他代码中的功能就像魅力一样,经过全面测试。

这是错误的文件吗? 我是否需要修改一个或多个文件?

请提前谢谢! :)

1 个答案:

答案 0 :(得分:0)

如果您想在订购成功后向客户发送短信,则需要在此处集成代码catalog/model/checkout/order.php。在订单模型中,您将找到方法addOrderHistory()。按方法名称搜索。在此方法结束时,您将找到向客户和管理员发送邮件的代码。所以在这里集成你的短信代码。

示例代码

public function addOrderHistory($order_id, $order_status_id, $comment = '', $notify = false, $override = false) {
....
....
// default code
....
....

// your code to send sms
$this->load->model('account/customer');
$cnum = $this->customer->gettelephone(); // get customer mobile number

//URL to shorten
$track_url = "https://www.example.com/index.php?route=account/order/info&order_id=".$order_id; 

// EDIT THIS: your auth parameters
$signature = '4a6fe0b26c';
....
....
}