diff --git a/webht/third_party/pay/controllers/AlipayTradeService.php b/webht/third_party/pay/controllers/AlipayTradeService.php index fca6bc4c..76058e55 100644 --- a/webht/third_party/pay/controllers/AlipayTradeService.php +++ b/webht/third_party/pay/controllers/AlipayTradeService.php @@ -54,6 +54,7 @@ class AlipayTradeService extends CI_Controller $this->load->model('Group_model'); $this->load->helper('payment'); $this->load->model('Online_payment_account_model', 'payment_model'); + $this->load->model('Online_payment_note_model', 'online_note'); $this->set_merchant('cht'); @@ -98,6 +99,15 @@ class AlipayTradeService extends CI_Controller } } + private function get_fundsource($app_id) + { + $code_fundsource = array( + "2021004129643221" => "trippest", + "2017092108849921" => "cht", // "CHT", + ); + return $app_id ? $code_fundsource[$app_id] : 'cht'; + } + public function index() { $this->note_list(); @@ -187,6 +197,38 @@ class AlipayTradeService extends CI_Controller // if (strcmp(strval($response->trade_status), "TRADE_SUCCESS") == 0) { // $this->Alipay_note_model->update_query($response->trade_no,$response->buyer_logon_id); // } + $fund_bill_list = json_decode($asyns_resp->data->fund_bill_list, true); + $to_online = [ + 'memo' => json_encode($asyns_resp->data,JSON_UNESCAPED_UNICODE), + 'transaction_id' => strval($asyns_resp->data->trade_no), + 'invoice_id' => strval($asyns_resp->data->out_trade_no), + 'subject' => strval($asyns_resp->data->subject), + 'remark' => strval($asyns_resp->data->body), + 'buyer_id' => $buyer, + // 'custom_id' => $pn_custom, + 'pay_amount' => strval($asyns_resp->data->total_amount), + 'pay_currency' => 'CNY', // $pn_mc_currency, + 'net_amount' => isset($asyns_resp->data->receipt_amount) ? strval($asyns_resp->data->receipt_amount) : null, + // 'pay_fee' => $post_data->mc_fee, + 'transaction_status' => 'completed', // $transaction_status, // $pn_payment_status, + 'payment_date' => strval($asyns_resp->data->gmt_create), + 'complate_date' => strval($asyns_resp->data->gmt_payment), + 'fund_source' => $this->get_fundsource($asyns_resp->data->app_id), + 'fund_type' => $notify_type, + 'event' => strval($asyns_resp->data->notify_type), + 'event_result' => strval($asyns_resp->data->trade_status), + 'payment_source' => isset($fund_bill_list[0]) ? $fund_bill_list[0]->fundChannel : null, + // 'referer_id' => isset($post_data->parent_txn_id) ? $post_data->parent_txn_id : '', + ]; + if ($notify_type == "refund") { + $to_online['transaction_id'] = strval($asyns_resp->data->out_biz_no); + $to_online['pay_amount'] = "-" . strval($asyns_resp->data->refund_fee); + $to_online['net_amount'] = $to_online['pay_amount']; + $to_online['payment_date'] = strval($asyns_resp->data->gmt_refund); + $to_online['complate_date'] = strval($asyns_resp->data->notify_time); + $to_online['referer_id'] = strval($asyns_resp->data->trade_no); + } + $this->online_note->add_note_alipay($to_online); } // 返回状态码200 echo "success"; @@ -868,24 +910,31 @@ class AlipayTradeService extends CI_Controller ignore_user_abort(true); $request = new AlipayDataDataserviceBillDownloadurlQueryRequest(); $date = $date===NULL ? date("Y-m-d", strtotime("-1 day")) : $date; - $request->setBizContent("{" . - "\"bill_type\":\"signcustomer\"," . - "\"bill_date\":\"$date\"" . - "}"); - $response = $this->aopclientRequestExecute ($request); - $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; - $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\\' . $this->merchant_account . $date . ".zip"; - $target = FCPATH.'download_statement\settlement_files\alipay\\' . $this->merchant_account . $date ; - file_put_contents($file_name, $file); + if (file_exists($file_name)) { + // 已经下载, 直接读取 + } else { + $request->setBizContent("{" . + "\"bill_type\":\"signcustomer\"," . + "\"bill_date\":\"$date\"" . + "}"); + $response = $this->aopclientRequestExecute ($request); + $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response"; + $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\\' . $this->merchant_account . $date . ".zip"; + $target = FCPATH.'download_statement\settlement_files\alipay\\' . $this->merchant_account . $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(); + $biz_data_rows = []; + $finance_data_rows = []; $zip = zip_open($file_name); if ($zip) { while ($zip_entry = zip_read($zip)) { @@ -895,8 +944,10 @@ class AlipayTradeService extends CI_Controller $content_row = explode("\n", $content_convert); if (stripos($content_row[0], "业务明细")) { $data_title = $biz_data_title; + $this_rows = &$biz_data_rows; } elseif (stripos($content_row[0], "账务明细")) { $data_title = $finance_data_title; + $this_rows = &$finance_data_rows; } else { zip_entry_close($zip_entry); continue; @@ -914,7 +965,8 @@ class AlipayTradeService extends CI_Controller $row_arr[$title] = trim($row[$key]); } $row_arr['app_id'] = $this->appid; - $biz_detail_table[] = $row_arr; + // $biz_detail_table[] = $row_arr; + array_push($this_rows, $row_arr); } zip_entry_close($zip_entry); @@ -922,11 +974,13 @@ class AlipayTradeService extends CI_Controller } zip_close($zip); } + $biz_detail_table = array_merge($biz_data_rows, $finance_data_rows); if (empty($biz_detail_table)) { return false; } + $to_online_arr = array(); foreach ($biz_detail_table as $key => $row) { - $total_amount = 0;$trade_type='';$trade_no = '';$has_charge=null; + $total_amount = 0;$trade_type='';$trade_no = '';$has_charge=null;$net_amount = ''; if (isset($row['finance_id'])) { if ($row['biz_type'] !== '其它' && $row['biz_type'] !== '在线支付' && $row['biz_type'] !== '转账') { continue; @@ -940,6 +994,7 @@ class AlipayTradeService extends CI_Controller $trade_no = $row['refund_id']; } else { $total_amount = ($row['total_amount']); + $net_amount = bcadd($row['total_amount'],$row['charge_fee']); $trade_type = 'pay'; $trade_no = $row['trade_no']; $has_charge = floatval($row['charge_fee'])==0 ? 1 : null; @@ -961,6 +1016,36 @@ class AlipayTradeService extends CI_Controller ,$row['buyer_user_name'] ,null,true ); + $to_online = [ + 'memo' => json_encode($row, JSON_UNESCAPED_UNICODE), + 'transaction_id' => strval($trade_no), + 'invoice_id' => strval($row['out_trade_no']), + 'subject' => strval($row['subject']), + 'remark' => isset($row['body']) ? strval($row['body']) : $row['remark'], + 'buyer_email' => $row['buyer_user_name'], + // 'buyer_id' => $buyer, + // 'custom_id' => $pn_custom, + 'pay_amount' => strval($total_amount), + 'pay_currency' => 'CNY', // $pn_mc_currency, + 'net_amount' => $net_amount === '' ? strval($total_amount) : strval($net_amount), + 'pay_fee' => isset($row['charge_fee']) ? bcsub(0, $row['charge_fee']) : null, + 'transaction_status' => 'completed', // $transaction_status, // $pn_payment_status, + 'payment_date' => $row['complete_time'], + 'complate_date' => $row['complete_time'], + 'fund_source' => $this->get_fundsource($row['app_id']), + 'fund_type' => $trade_type, + 'event' => isset($row['biz_type']) ? strval($row['biz_type']) : $row['trade_type'], + 'event_result' => 'TRADE_SUCCESS', + 'payment_source' => isset($row['trading_channel']) ? $row['trading_channel'] : null, + // 'referer_id' => isset($post_data->parent_txn_id) ? $post_data->parent_txn_id : '', + ]; + if ($trade_type == "refund") { + $to_online['referer_id'] = strval($row['trade_no']); + } + $to_online_arr[] = $to_online; + } + foreach ($to_online_arr as $key => $item) { + $this->online_note->add_note_alipay($item, true); } return; } diff --git a/webht/third_party/pay/models/Online_payment_note_model.php b/webht/third_party/pay/models/Online_payment_note_model.php index 303d2472..e156791f 100644 --- a/webht/third_party/pay/models/Online_payment_note_model.php +++ b/webht/third_party/pay/models/Online_payment_note_model.php @@ -150,7 +150,7 @@ class Online_payment_note_model extends CI_Model { $wx_app = $wx_app!=='0' ? $wx_app : (isset($raw->appid) ? $raw->appid : '0'); $ele->app_name = ""; - if ($this->wxpay_app[$wx_app]) { + if ($ele->OPN_accountMethod ===15016 && isset($this->wxpay_app[$wx_app])) { $ele->app_name = $this->wxpay_app[$wx_app]; } } @@ -534,5 +534,59 @@ class Online_payment_note_model extends CI_Model { return; } + /** + * @uses AlipayTradeService::alipay_notice + */ + public function add_note_alipay($result_data, $check_exists=false) + { + $this->load->model('Online_payment_account_model', 'account_model'); + if ($check_exists === true) { + $sql = "SELECT TOP 1 1 + FROM OnlinePaymentNote + WHERE OPN_accountMethod=15015 AND OPN_transactionId = '{$result_data['transaction_id']}'"; + $query = $this->info->query($sql); + if ($query->num_rows() > 0) { + // todo: update pay_fee, CNY ssje + return false; + } + } + $ssje = $this->account_model->get_ssje_exclude_fee($result_data['net_amount'], str_replace("CNY", "RMB", strtoupper($result_data['pay_currency']))); + + $payment_date = date('Y-m-d H:i:s', strtotime($result_data['payment_date']) + 3600 * 8); + $complate_date = date('Y-m-d H:i:s', strtotime($result_data['complate_date']) + 3600 * 8); + + $save_column = array(); + $save_column['OPN_transactionId'] = $result_data['transaction_id']; + $save_column['OPN_orderAmount'] = $result_data['pay_amount']; + $save_column['OPN_payAmount'] = $result_data['pay_amount']; + $save_column['OPN_payFee'] = isset($result_data['pay_fee']) ? bcsub(0,$result_data['pay_fee']) : null; + $save_column['OPN_netAmount'] = $result_data['net_amount']; + $save_column['OPN_resultCode'] = $result_data['event_result']; + $save_column['OPN_resultMsg'] = $result_data['event']; + $save_column['OPN_entryAmountCNY'] = floatval($ssje); + $save_column['OPN_noticeType'] = $result_data['fund_type']; + $save_column['OPN_relatedId'] = isset($result_data['referer_id']) ? $result_data['referer_id'] : null; + $save_column['OPN_acquiringTime'] = $result_data['payment_date']; + $save_column['OPN_completeTime'] = $result_data['complate_date']; + + $save_column['OPN_accountMethod'] = 15015; + $save_column['OPN_noticeSendStatus'] = 'closed'; + $save_column['OPN_transactionResult'] = $result_data['transaction_status']; + $save_column['OPN_orderId'] = $result_data['invoice_id']; + $save_column['OPN_rawOrderId'] = $result_data['invoice_id']; + $save_column['OPN_invoiceId'] = $result_data['invoice_id']; + $save_column['OPN_subject'] = isset($result_data['subject']) ? $result_data['subject'] : ''; + $save_column['OPN_currency'] = $result_data['pay_currency']; + $save_column['OPN_remark'] = empty($result_data['remark']) ? '' : ($result_data['remark']); + $save_column['OPN_payerLogId'] = isset( $result_data['buyer_id']) ? $result_data['buyer_id'] : ''; + $save_column['OPN_payerEmail'] = isset( $result_data['buyer_email']) ? $result_data['buyer_email'] : ''; + $save_column['OPN_fundSource'] = $result_data['fund_source']; + $save_column['OPN_paymentSource'] = $result_data['payment_source']; + $save_column['OPN_rawContent'] = $result_data['memo']; + $save_column['OPN_noticeTime'] = date('Y-m-d H:i:s'); + $this->insert_note($save_column) ; + return; + } + } diff --git a/webht/third_party/paypal/controllers/index.php b/webht/third_party/paypal/controllers/index.php index 128f1f39..2da5380f 100644 --- a/webht/third_party/paypal/controllers/index.php +++ b/webht/third_party/paypal/controllers/index.php @@ -801,7 +801,7 @@ class Index extends CI_Controller { ,$pn_receiver_account ,isset($post_data->parent_txn_id) ? $post_data->parent_txn_id : '' ); - log_message('error','paypal-webhook-succeed ' . $pn_txn_id . ' # ' . $pn_invoice); + log_message('error','paypal-webhook-succeed ' . $pn_payment_status . ' ' . $pn_txn_id . ' # ' . $pn_invoice); // echo 'ok'; $to_online = [ 'memo' => $pn_memo, diff --git a/webht/third_party/paypal/models/Online_payment_note_model.php b/webht/third_party/paypal/models/Online_payment_note_model.php index a3831a63..c99e63b2 100644 --- a/webht/third_party/paypal/models/Online_payment_note_model.php +++ b/webht/third_party/paypal/models/Online_payment_note_model.php @@ -537,19 +537,22 @@ class Online_payment_note_model extends CI_Model { * 'net_amount' => $net_amount, * 'pay_fee' => $post_data->mc_fee, * 'transaction_status' => 'completed', // $pn_payment_status, - * 'payment_date' => $pn_payment_date, + * 'payment_date' => $pn_payment_date, // UTC * 'fund_source' => $pn_receiver_account_name, * 'fund_type' => $fund_type, * 'event' => $post_data->event_type, * 'event_result' => $pn_payment_status, * 'referer_id' => isset($post_data->parent_txn_id) ? $post_data->parent_txn_id : '', * ]; + * @uses Index::paypal_webhook */ public function add_note_paypal($result_data) { $this->load->model('paypal_model', 'account_model'); $ssje = $this->account_model->get_ssje_exclude_fee($result_data['net_amount'],'', str_replace("CNY", "RMB", strtoupper($result_data['pay_currency']))); + $localSqlDateTime = date('Y-m-d H:i:s', strtotime($result_data['payment_date']) + 3600 * 8); + $save_column = array(); $save_column['OPN_transactionId'] = $result_data['transaction_id']; $save_column['OPN_orderAmount'] = $result_data['pay_amount']; @@ -561,8 +564,8 @@ class Online_payment_note_model extends CI_Model { $save_column['OPN_entryAmountCNY'] = floatval($ssje); $save_column['OPN_noticeType'] = $result_data['fund_type']; $save_column['OPN_relatedId'] = $result_data['referer_id']; - $save_column['OPN_acquiringTime'] = $result_data['payment_date']; - $save_column['OPN_completeTime'] = $result_data['payment_date']; + $save_column['OPN_acquiringTime'] = $localSqlDateTime; + $save_column['OPN_completeTime'] = $localSqlDateTime; $save_column['OPN_accountMethod'] = 15002; // $this->config->item('method_code', 'paypal'); $save_column['OPN_noticeSendStatus'] = 'closed';