alipay.php (6533B)
1 <?php 2 class ModelExtensionPaymentAlipay extends Model { 3 private $apiMethodName="alipay.trade.page.pay"; 4 private $postCharset = "UTF-8"; 5 private $alipaySdkVersion = "alipay-sdk-php-20161101"; 6 private $apiVersion="1.0"; 7 private $logFileName = "alipay.log"; 8 private $gateway_url = "https://openapi.alipay.com/gateway.do"; 9 private $alipay_public_key; 10 private $private_key; 11 private $appid; 12 private $notifyUrl; 13 private $returnUrl; 14 private $format = "json"; 15 private $signtype = "RSA2"; 16 17 private $apiParas = array(); 18 19 public function getMethod($address, $total) { 20 $this->load->language('extension/payment/alipay'); 21 22 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_alipay_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')"); 23 24 if ($this->config->get('payment_alipay_total') > 0 && $this->config->get('payment_alipay_total') > $total) { 25 $status = false; 26 } elseif (!$this->config->get('payment_alipay_geo_zone_id')) { 27 $status = true; 28 } elseif ($query->num_rows) { 29 $status = true; 30 } else { 31 $status = false; 32 } 33 34 $method_data = array(); 35 36 if ($status) { 37 $method_data = array( 38 'code' => 'alipay', 39 'title' => $this->language->get('text_title'), 40 'terms' => '', 41 'sort_order' => $this->config->get('payment_alipay_sort_order') 42 ); 43 } 44 45 return $method_data; 46 } 47 48 private function setParams($alipay_config){ 49 $this->gateway_url = $alipay_config['gateway_url']; 50 $this->appid = $alipay_config['app_id']; 51 $this->private_key = $alipay_config['merchant_private_key']; 52 $this->alipay_public_key = $alipay_config['alipay_public_key']; 53 $this->postCharset = $alipay_config['charset']; 54 $this->signtype = $alipay_config['sign_type']; 55 $this->notifyUrl = $alipay_config['notify_url']; 56 $this->returnUrl = $alipay_config['return_url']; 57 58 if (empty($this->appid)||trim($this->appid)=="") { 59 throw new Exception("appid should not be NULL!"); 60 } 61 if (empty($this->private_key)||trim($this->private_key)=="") { 62 throw new Exception("private_key should not be NULL!"); 63 } 64 if (empty($this->alipay_public_key)||trim($this->alipay_public_key)=="") { 65 throw new Exception("alipay_public_key should not be NULL!"); 66 } 67 if (empty($this->postCharset)||trim($this->postCharset)=="") { 68 throw new Exception("charset should not be NULL!"); 69 } 70 if (empty($this->gateway_url)||trim($this->gateway_url)=="") { 71 throw new Exception("gateway_url should not be NULL!"); 72 } 73 } 74 75 function pagePay($builder,$config) { 76 $this->setParams($config); 77 $biz_content=null; 78 if(!empty($builder)){ 79 $biz_content = json_encode($builder,JSON_UNESCAPED_UNICODE); 80 } 81 82 $log = new Log($this->logFileName); 83 $log->write($biz_content); 84 85 $this->apiParas["biz_content"] = $biz_content; 86 87 $response = $this->pageExecute($this, "post"); 88 $log = new Log($this->logFileName); 89 $log->write("response: ".var_export($response,true)); 90 91 return $response; 92 } 93 94 function check($arr, $config){ 95 $this->setParams($config); 96 97 $result = $this->rsaCheckV1($arr, $this->signtype); 98 99 return $result; 100 } 101 102 public function pageExecute($request, $httpmethod = "POST") { 103 $iv=$this->apiVersion; 104 105 $sysParams["app_id"] = $this->appid; 106 $sysParams["version"] = $iv; 107 $sysParams["format"] = $this->format; 108 $sysParams["sign_type"] = $this->signtype; 109 $sysParams["method"] = $this->apiMethodName; 110 $sysParams["timestamp"] = date("Y-m-d H:i:s"); 111 $sysParams["alipay_sdk"] = $this->alipaySdkVersion; 112 $sysParams["notify_url"] = $this->notifyUrl; 113 $sysParams["return_url"] = $this->returnUrl; 114 $sysParams["charset"] = $this->postCharset; 115 $sysParams["gateway_url"] = $this->gateway_url; 116 117 $apiParams = $this->apiParas; 118 119 $totalParams = array_merge($apiParams, $sysParams); 120 121 $totalParams["sign"] = $this->generateSign($totalParams, $this->signtype); 122 123 if ("GET" == strtoupper($httpmethod)) { 124 $preString=$this->getSignContentUrlencode($totalParams); 125 $requestUrl = $this->gateway_url."?".$preString; 126 127 return $requestUrl; 128 } else { 129 foreach ($totalParams as $key => $value) { 130 if (false === $this->checkEmpty($value)) { 131 $value = str_replace("\"", """, $value); 132 $totalParams[$key] = $value; 133 } else { 134 unset($totalParams[$key]); 135 } 136 } 137 return $totalParams; 138 } 139 } 140 141 protected function checkEmpty($value) { 142 if (!isset($value)) 143 return true; 144 if ($value === null) 145 return true; 146 if (trim($value) === "") 147 return true; 148 149 return false; 150 } 151 152 public function rsaCheckV1($params, $signType='RSA') { 153 $sign = $params['sign']; 154 $params['sign_type'] = null; 155 $params['sign'] = null; 156 return $this->verify($this->getSignContent($params), $sign, $signType); 157 } 158 159 function verify($data, $sign, $signType = 'RSA') { 160 $pubKey= $this->alipay_public_key; 161 $res = "-----BEGIN PUBLIC KEY-----\n" . 162 wordwrap($pubKey, 64, "\n", true) . 163 "\n-----END PUBLIC KEY-----"; 164 165 (trim($pubKey)) or die('Alipay public key error!'); 166 167 if ("RSA2" == $signType) { 168 $result = (bool)openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256); 169 } else { 170 $result = (bool)openssl_verify($data, base64_decode($sign), $res); 171 } 172 173 return $result; 174 } 175 176 public function getSignContent($params) { 177 ksort($params); 178 179 $stringToBeSigned = ""; 180 $i = 0; 181 foreach ($params as $k => $v) { 182 if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) { 183 if ($i == 0) { 184 $stringToBeSigned .= "$k" . "=" . "$v"; 185 } else { 186 $stringToBeSigned .= "&" . "$k" . "=" . "$v"; 187 } 188 $i++; 189 } 190 } 191 192 unset ($k, $v); 193 return $stringToBeSigned; 194 } 195 196 public function generateSign($params, $signType = "RSA") { 197 return $this->sign($this->getSignContent($params), $signType); 198 } 199 200 protected function sign($data, $signType = "RSA") { 201 $priKey=$this->private_key; 202 $res = "-----BEGIN RSA PRIVATE KEY-----\n" . 203 wordwrap($priKey, 64, "\n", true) . 204 "\n-----END RSA PRIVATE KEY-----"; 205 206 if ("RSA2" == $signType) { 207 openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256); 208 } else { 209 openssl_sign($data, $sign, $res); 210 } 211 212 $sign = base64_encode($sign); 213 return $sign; 214 } 215 216 function getPostCharset(){ 217 return trim($this->postCharset); 218 } 219 }