From ad583f130e316dd79de7ce9c125f4f50f2fecfe0 Mon Sep 17 00:00:00 2001 From: lyt Date: Mon, 1 Jul 2019 09:42:43 +0800 Subject: [PATCH] =?UTF-8?q?Alipay=E4=B8=8B=E8=BD=BD=E5=AF=B9=E8=B4=A6?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/controllers/AlipayTradeService.php | 150 ++++++++++++++++-- .../pay/controllers/WxpayService.php | 1 + .../pay/helpers/payment_helper.php | 24 ++- 3 files changed, 162 insertions(+), 13 deletions(-) diff --git a/webht/third_party/pay/controllers/AlipayTradeService.php b/webht/third_party/pay/controllers/AlipayTradeService.php index 712309ea..ebe92174 100644 --- a/webht/third_party/pay/controllers/AlipayTradeService.php +++ b/webht/third_party/pay/controllers/AlipayTradeService.php @@ -130,7 +130,7 @@ class AlipayTradeService extends CI_Controller ,NULL ,strval($asyns_resp->data->gmt_create) ,strval($asyns_resp->data->gmt_payment) - ,json_encode($asyns_resp->data) + ,json_encode($asyns_resp->data,JSON_UNESCAPED_UNICODE) ,$notify_type ,$code ,strval($asyns_resp->data->trade_status) @@ -147,7 +147,7 @@ class AlipayTradeService extends CI_Controller ,NULL ,strval($asyns_resp->data->gmt_refund) ,strval($asyns_resp->data->notify_time) - ,json_encode($asyns_resp->data) + ,json_encode($asyns_resp->data,JSON_UNESCAPED_UNICODE) ,$notify_type ,$code ,strval($asyns_resp->data->trade_status) @@ -783,19 +783,151 @@ class AlipayTradeService extends CI_Controller */ public function get_billfile($date=NULL) { + ignore_user_abort(true); $request = new AlipayDataDataserviceBillDownloadurlQueryRequest(); - $date = $date===NULL ? "2019-05-16" : $date; + $date = $date===NULL ? date("Y-m-d", strtotime("-1 day")) : $date; $request->setBizContent("{" . "\"bill_type\":\"signcustomer\"," . "\"bill_date\":\"$date\"" . "}"); $response = $this->aopclientRequestExecute ($request); -// var_dump($response); $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; -var_dump($response->$responseNode); - // $resultCode = $result->$responseNode->code; - // if(!empty($resultCode)&&$resultCode == 10000){ - // } + $resultCode = $response->$responseNode->code; + if(empty($resultCode) || strval($resultCode) !== "10000"){ + return false; + } + set_time_limit(0); + $file = file_get_contents($response->$responseNode->bill_download_url); + $file_name = FCPATH.'download_statement\settlement_files\alipay\\' . $date . ".zip"; + $target = FCPATH.'download_statement\settlement_files\alipay\\' . $date ; + file_put_contents($file_name, $file); + $biz_detail_table = array(); + $biz_data_title = $this->bill_data_title(); + $finance_data_title = $this->finance_data_title(); + $zip = zip_open($file_name); + if ($zip) { + while ($zip_entry = zip_read($zip)) { + if (zip_entry_open($zip, $zip_entry, "r")) { + $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); + $content_convert = mb_convert_encoding($buf, 'UTF-8', 'GB2312'); + $content_row = explode("\n", $content_convert); + if (stripos($content_row[0], "业务明细")) { + $data_title = $biz_data_title; + } elseif (stripos($content_row[0], "账务明细")) { + $data_title = $finance_data_title; + } else { + zip_entry_close($zip_entry); + continue; + } + for ($i=5; $i < count($content_row); $i++) { + if (stripos($content_row[$i], "结束")) { + break; + } + $row = explode(',',$content_row[$i]); + $row_arr = array(); + foreach ($data_title as $key => $title) { + if ( ! isset($row[$key])) { + continue; + } + $row_arr[$title] = trim($row[$key]); + } + $biz_detail_table[] = $row_arr; + } + + zip_entry_close($zip_entry); + } + } + zip_close($zip); + } + if (empty($biz_detail_table)) { + return false; + } + foreach ($biz_detail_table as $key => $row) { + $total_amount = 0;$trade_type='';$trade_no = '';$has_charge=null; + if (isset($row['finance_id'])) { + if ($row['biz_type'] !== '其它') { + continue; + } + $total_amount = floatval($row['income'])>0 ? $row['income'] : $row['expenditure']; + $trade_type = floatval($total_amount)>0 ? 'pay' : 'refund'; + $trade_no = $row['trade_no']; + } else if (!empty($row['refund_id'])) { + $total_amount = bcadd($row['total_amount'],$row['charge_fee']); + $trade_type = 'refund'; + $trade_no = $row['refund_id']; + } else { + $total_amount = ($row['total_amount']); + $trade_type = 'pay'; + $trade_no = $row['trade_no']; + $has_charge = floatval($row['charge_fee'])==0 ? 1 : null; + } + $this->Alipay_note_model->save_alipay( + $trade_no + ,strval($row['out_trade_no']) + ,"CNY" + ,strval($total_amount) + ,NULL + ,$has_charge + ,strval($row['complete_time']) + ,strval($row['complete_time']) + ,json_encode($row,JSON_UNESCAPED_UNICODE) + ,$trade_type + ,null + ,'TRADE_SUCCESS' + ,NULL + ,$row['buyer_user_name'] + ); + } + return; + } + + public function bill_data_title() + { + return array( + "trade_no", // 支付宝交易号 + "out_trade_no", // 商户订单号 + "trade_type", // 业务类型 + "subject", // 商品名称 + "create_time", // 创建时间 + "complete_time", // 完成时间 + "store_id", // 门店编号 + "store_name", // 门店名称 + "operator", // 操作员 + "terminal_id", // 终端号 + "buyer_user_name", // 对方账户 + "total_amount", // 订单金额(元) + "receipt_amount", // 商家实收(元) + "coupon_amount", // 支付宝红包(元) + "point_amount", // 集分宝(元) + "alipayaccount_amount", // 支付宝优惠(元) + "mdiscount_amount", // 商家优惠(元) + "discount_amount", // 券核销金额(元) + "discount_name", // 券名称 + "mcoupon_amount", // 商家红包消费金额(元) + "pcard_amount", // 卡消费金额(元) + "refund_id", // 退款批次号/请求号 + "charge_fee", // 服务费(元) + "royalty_fee", // 分润(元) + "body" // 备注 + ); + } + + public function finance_data_title() + { + return array( + "finance_id",// 账务流水号 + "trade_no",// 业务流水号 + "out_trade_no",// 商户订单号 + "subject",// 商品名称 + "complete_time",// 发生时间 + "buyer_user_name",// 对方账号 + "income",// 收入金额(+元) + "expenditure",// 支出金额(-元) + "balance",// 账户余额(元) + "trading_channel",// 交易渠道 + "biz_type",// 业务类型:交易退款,在线支付,收费,其它 + "remark"// 备注 + ); } /** @@ -876,7 +1008,7 @@ var_dump($response->$responseNode); ,1 // 此处1表示通过收款码进来的, 不收手续费, 后续录入时分别处理 ,strval($query_pay->send_pay_date) ,strval($query_pay->send_pay_date) - ,json_encode($query_pay) + ,json_encode($query_pay,JSON_UNESCAPED_UNICODE) ,$pay_type ,NULL ,strval($query_pay->trade_status) diff --git a/webht/third_party/pay/controllers/WxpayService.php b/webht/third_party/pay/controllers/WxpayService.php index b7268920..2ea28dae 100644 --- a/webht/third_party/pay/controllers/WxpayService.php +++ b/webht/third_party/pay/controllers/WxpayService.php @@ -92,6 +92,7 @@ class WxpayService extends CI_Controller { $this->download_bill($account); } async_curl($this->config->item('send_notify_url', 'wxpay')); + async_curl('https://www.mycht.cn/webht.php/apps/pay/alipaytradeservice/get_billfile'); return; } diff --git a/webht/third_party/pay/helpers/payment_helper.php b/webht/third_party/pay/helpers/payment_helper.php index 8a5cd3e4..3d7ce814 100644 --- a/webht/third_party/pay/helpers/payment_helper.php +++ b/webht/third_party/pay/helpers/payment_helper.php @@ -78,12 +78,12 @@ function async_curl($url, $second = 1) curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch,CURLOPT_URL, $url); if(stripos($url,"https://")!==FALSE){ - curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); + // curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); } else { - curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); - curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验 + // curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); + // curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验 } curl_setopt($ch,CURLOPT_USERAGENT, $ua); //设置header @@ -270,3 +270,19 @@ function from_xml($xml) return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); } +/*! + * 转换字符集编码 + * @param $data + * @param $targetCharset + * @return string + */ +function characet($data, $targetCharset) { + if (!empty($data)) { + $fileType = "UTF-8"; + if (strcasecmp($fileType, $targetCharset) != 0) { + $data = mb_convert_encoding($data, $targetCharset, $fileType); + // $data = iconv($fileType, $targetCharset.'//IGNORE', $data); + } + } + return $data; +}