Пример #1
0
 /**
  * @return PilipayLogger
  */
 public static function instance()
 {
     if (!self::$instance) {
         self::$instance = new PilipayLogger();
     }
     return self::$instance;
 }
Пример #2
0
 /**
  * @param $appSecret
  * @param bool $throws whether throws exception when fails
  * @return bool whether is valid request
  * @throws PilipayError
  */
 public function verify($appSecret, $throws = false)
 {
     $calcedSignMsg = md5($this->_merchantNO . $this->_orderNo . intval($this->_orderAmount) . $this->_sendTime . $appSecret);
     if ($calcedSignMsg != $this->_signMsg) {
         PilipayLogger::instance()->log("error", "Invalid signMsg: " . $this->_signMsg . " with secret: " . $appSecret . " with data: " . json_encode(get_object_vars($this)));
         if ($throws) {
             throw new PilipayError(PilipayError::INVALID_SIGN, $this->_signMsg);
         }
         return false;
     }
     return true;
 }
Пример #3
0
 /**
  * Update track number (logistics number)
  * @param $logisticsNo
  * @throws PilipayError
  */
 public function updateTrackNo($logisticsNo)
 {
     $params = array('orderNo' => $this->orderNo, 'merchantNo' => $this->merchantNO, 'logisticsNo' => $logisticsNo);
     PilipayLogger::instance()->log('info', "Update track NO: " . json_encode($params));
     $curl = new PilipayCurl();
     $response = $curl->post(PilipayConfig::getUpdateTrackNoUrl(), $params);
     PilipayLogger::instance()->log('info', 'Update track NO result: ' . print_r(array('request' => $params, 'response' => array('statusCode' => $curl->getResponseStatusCode(), 'statusText' => $curl->getResponseStatusText(), 'content' => $curl->getResponseContent())), true));
     if (!$response) {
         throw new PilipayError(PilipayError::EMPTY_RESPONSE, 'Updating tacking number');
     }
     if (strcasecmp(trim($response), 'success') !== 0) {
         throw new PilipayError(PilipayError::UPDATE_FAILED, 'Update tracking number failed: ' . $response);
     }
 }
Пример #4
0
 /**
  * Module constructor
  * @throws PrestaShopException
  */
 public function __construct()
 {
     $this->name = 'pilipay';
     $this->tab = 'payments_gateways';
     $this->version = '1.2.9';
     $this->author = 'Pilibaba';
     $this->controllers = array('payment', 'validation');
     $this->is_eu_compatible = 0;
     // = 1; todo: what should I do to be compatible with EU?
     $this->ps_versions_compliancy = array('min' => '1.5.1', 'max' => _PS_VERSION_);
     $this->module_key = '1d52b16e6ed130c60b22ac7896f69bd2';
     $this->currencies = true;
     $this->currencies_mode = 'checkbox';
     $config = Configuration::getMultiple(array(self::PILIPAY_MERCHANT_NO, self::PILIPAY_APP_SECRET, self::PILIPAY_WAREHOUSES, self::PILIPAY_TESTMODE));
     if (!empty($config[self::PILIPAY_MERCHANT_NO])) {
         $this->merchantNo = $config[self::PILIPAY_MERCHANT_NO];
     }
     if (!empty($config[self::PILIPAY_APP_SECRET])) {
         $this->appSecret = $config[self::PILIPAY_APP_SECRET];
     }
     if (!empty($config[self::PILIPAY_CURRENCY])) {
         $this->currency = $config[self::PILIPAY_CURRENCY];
     }
     if (!empty($config[self::PILIPAY_WAREHOUSES])) {
         $this->warehouse = $config[self::PILIPAY_WAREHOUSES];
     }
     if (!empty($config[self::PILIPAY_TESTMODE])) {
         $this->testmode = $config[self::PILIPAY_TESTMODE];
     }
     $this->bootstrap = true;
     parent::__construct();
     $this->displayName = $this->l('Pilipay');
     $this->description = $this->l('Pilibaba All-in-One gateway provides a unique combined Payment & Logistics solution & Customs compliance for eCommerce platforms to China market. By using Pilibaba service, merchants will be able to sell to 1.3 Billion Chinese customers instantly.Here are core benefits from Pilibaba for both merchants and Chinese customers.');
     $this->confirmUninstall = $this->l('Are you sure about removing these details?');
     if (empty($this->merchantNo) || empty($this->appSecret)) {
         $this->warning = $this->l('Merchant number and secret key must be configured before using this module.');
     }
     if (!count(Currency::checkPaymentCurrencies($this->id))) {
         $this->warning = $this->l('No currency has been set for this module.');
     }
     PilipayLogger::instance()->setHandler(array(__CLASS__, 'log'));
 }
Пример #5
0
 protected function _requestViaFsockopen($method, $url, $params, $timeout)
 {
     // prepare
     $additionalHeaders = array_merge(array('Connection' => 'close', 'User-Agent' => 'curl', 'Accept' => 'text/html,text/*,*/*'), (array) $this->additionalHeaders);
     $requestContent = '';
     $urlInfo = parse_url($url);
     switch ($method) {
         case 'GET':
             if (!empty($params)) {
                 $urlInfo['query'] = !empty($urlInfo['query']) ? $urlInfo['query'] . '&' . http_build_query($params) : http_build_query($params);
             }
             break;
         default:
             // POST, DELETE...
             if (!empty($params)) {
                 $requestContent = http_build_query($params);
                 $additionalHeaders['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
                 $additionalHeaders['Content-Length'] = strlen($requestContent);
             }
             break;
     }
     // build headers of request
     $requestHeaders = array(strtr('{method} {pathAndQuery} HTTP/1.1', array('{method}' => strtoupper($method), '{pathAndQuery}' => (!empty($urlInfo['path']) ? $urlInfo['path'] : '/') . (!empty($urlInfo['query']) ? '?' . $urlInfo['query'] : ''))), 'Host: ' . $urlInfo['host']);
     foreach ($additionalHeaders as $headerKey => $headerValue) {
         $requestHeaders[] = "{$headerKey}: {$headerValue}";
     }
     $request = implode(self::CRLF, $requestHeaders) . self::CRLFCRLF . $requestContent;
     // open sock file
     if ($urlInfo['scheme'] == 'https') {
         $sockFileName = 'ssl://' . $urlInfo['host'];
         $sockPort = isset($urlInfo['port']) ? intval($urlInfo['port']) : 443;
     } else {
         $sockFileName = $urlInfo['host'];
         $sockPort = isset($urlInfo['port']) ? intval($urlInfo['port']) : 80;
     }
     $sockFile = fsockopen($sockFileName, $sockPort, $sockErrCode, $sockErrMsg, $timeout);
     if (!$sockFile) {
         throw new PilipayError(PilipayError::CURL_ERROR, "failed to open sock file: {$sockErrMsg} ({$sockErrCode})");
     }
     // write the request
     fwrite($sockFile, $request);
     // read the response
     $gotResponseHeaderEnd = false;
     $hasResponseContentLenInHeader = false;
     $responseBodyLen = false;
     $responseHeader = '';
     $responseBody = '';
     while (!feof($sockFile)) {
         $line = fgets($sockFile);
         if (!$gotResponseHeaderEnd) {
             if ($line === self::CRLF) {
                 $gotResponseHeaderEnd = true;
                 $this->responseHeaders = $this->parseResponseHeader($responseHeader);
                 if ($this->responseHeaders['Content-Length']) {
                     $responseBodyLen = $this->responseHeaders['Content-Length'];
                     $hasResponseContentLenInHeader = true;
                 }
             } else {
                 $responseHeader .= $line;
             }
         } else {
             $responseBody .= $line;
         }
     }
     fclose($sockFile);
     if (!$hasResponseContentLenInHeader) {
         // parse the response-body-len from hex token
         // then, cut down the response body
         $responseBody = $this->parseResponseBody($responseBody);
     }
     PilipayLogger::instance()->log('debug', "CURL: " . print_r(array('request' => array('method' => $method, 'url' => $url, 'params' => $params, 'headers' => $requestHeaders), 'response' => array('errno' => $sockErrCode, 'error' => $sockErrMsg, 'header' => $responseHeader, 'body' => $responseBody)), true));
     $this->responseContent = $responseBody;
     return $responseBody;
 }
 /**
  * Update track number (logistics number)
  *
  * @param $logisticsNo
  */
 public function updateTrackNo($logisticsNo)
 {
     $params = array('orderNo' => pSQL($this->orderNo), 'merchantNo' => pSQL($this->merchantNo), 'logisticsNo' => pSQL($logisticsNo), 'signMsg' => md5($this->orderNo . $logisticsNo . $this->merchantNo . $this->appSecret));
     PilipayLogger::instance()->log('info', "Update track NO: " . Tools::jsonEncode($params));
     $curl = new PilipayCurl();
     $curl->get(PilipayConfig::getUpdateTrackNoUrl(), $params);
     PilipayLogger::instance()->log('info', 'Update track NO result: ' . print_r(array('request' => $params, 'response' => array('statusCode' => $curl->getResponseStatusCode(), 'statusText' => $curl->getResponseStatusText(), 'content' => $curl->getResponseContent())), true));
 }
 /**
  * @param      $appSecret
  * @param bool $throws whether throws exception when fails
  *
  * @return bool whether is valid request
  * @throws PilipayError
  */
 public function verify($appSecret, $throws = false)
 {
     $signature = $this->_merchantNo;
     $signature .= $this->_orderNo;
     $signature .= $this->_orderAmount;
     $signature .= $this->_signType;
     $signature .= $this->_fee;
     $signature .= $this->_orderTime;
     $signature .= $this->_customerMail;
     $signature .= $appSecret;
     $calcedSignMsg = md5($signature);
     if (strcasecmp($calcedSignMsg, $this->_signMsg) !== 0) {
         PilipayLogger::instance()->log("error", "Invalid signMsg: " . $this->_signMsg . " with secret: " . $appSecret . " with data: " . Tools::jsonEncode(get_object_vars($this)));
         if ($throws) {
             throw new PilipayError(PilipayError::INVALID_SIGN, $this->_signMsg);
         }
         $this->_validation = false;
         return false;
     }
     $this->_validation = true;
     return true;
 }
Пример #8
0
 /**
  * @param $appSecret
  * @param bool $throws whether throws exception when fails
  * @return bool whether is valid request
  * @throws PilipayError
  */
 public function verify($appSecret, $throws = false)
 {
     $calcedSignMsg = md5($this->_merchantNo . $this->_orderNo . $this->_orderAmount . $this->_signType . $this->_dealId . $this->_fee . $this->_orderTime . $this->_customerMail . $appSecret);
     if (strcasecmp($calcedSignMsg, $this->_signMsg) !== 0) {
         PilipayLogger::instance()->log("error", "Invalid signMsg: " . $this->_signMsg . " with secret: " . $appSecret . " with data: " . json_encode(get_object_vars($this)));
         if ($throws) {
             throw new PilipayError(PilipayError::INVALID_SIGN, $this->_signMsg);
         }
         return false;
     }
     return true;
 }
 /**
  * Make a $method request
  * @param string $method            - GET/POST/...
  * @param string $url               - the URL
  * @param array|string|null $params - if it's a string, it will passed as it is; if it's an array, http_build_query will be used to convert it to a string
  * @param int $timeout              - request timeout in seconds
  * @return string                   - the response content (without headers)
  */
 public function request($method, $url, $params, $timeout = 30)
 {
     $options = array(CURLOPT_HTTPGET => false, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => false, CURLOPT_MAXREDIRS => 0, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_USERAGENT => 'curl', CURLOPT_CONNECTTIMEOUT => $timeout, CURLOPT_TIMEOUT => $timeout);
     switch (Tools::strtoupper($method)) {
         case 'GET':
             if (!empty($params)) {
                 $url .= '?' . (is_array($params) ? http_build_query($params) : (string) $params);
             }
             $ch = curl_init($url);
             $options[CURLOPT_HTTPGET] = true;
             break;
         default:
             // post...
             $ch = curl_init($url);
             $options[CURLOPT_CUSTOMREQUEST] = $method;
             if (!empty($params)) {
                 $options[CURLOPT_POSTFIELDS] = is_array($params) ? http_build_query($params) : (string) $params;
                 $this->additionalHeaders['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
             }
             break;
     }
     $headers = array();
     if (!empty($this->additionalHeaders)) {
         foreach ($this->additionalHeaders as $key => $value) {
             $headers[] = $key . ': ' . $value;
         }
         $options[CURLOPT_HTTPHEADER] = $headers;
     }
     foreach ($options as $optKey => $optVal) {
         curl_setopt($ch, $optKey, $optVal);
     }
     $response = curl_exec($ch);
     $curlInfo = curl_getinfo($ch);
     $errCode = curl_errno($ch);
     $errMsg = curl_error($ch);
     curl_close($ch);
     PilipayLogger::instance()->log('debug', "CURL: " . print_r(array('request' => array('method' => $method, 'url' => $url, 'params' => $params, 'headers' => $headers), 'response' => array('errno' => $errCode, 'error' => $errMsg, 'content' => $response)), true));
     $headerSize = $curlInfo['header_size'];
     $this->responseHeaders = self::parseResponseHeader(Tools::substr($response, 0, $headerSize));
     $this->responseHeaders['redirect_url'] = $curlInfo['redirect_url'];
     $this->responseContent = Tools::substr($response, $headerSize);
     return $this->responseContent;
 }