From 155c137a975d72b60a91b54e495d6e06a8d3b989 Mon Sep 17 00:00:00 2001 From: Lei OT Date: Wed, 26 Mar 2025 13:48:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E4=B8=ADPayPal=E7=9A=84webhook?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=88=B0=E5=9C=A8=E7=BA=BF=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/models/Online_payment_note_model.php | 3 + webht/third_party/pay/views/payment_list.php | 6 +- .../third_party/paypal/controllers/index.php | 45 +- .../models/Online_payment_note_model.php | 585 ++++++++++++++++++ 4 files changed, 634 insertions(+), 5 deletions(-) create mode 100644 webht/third_party/paypal/models/Online_payment_note_model.php 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 f9cc83e7..0bf4c93c 100644 --- a/webht/third_party/pay/models/Online_payment_note_model.php +++ b/webht/third_party/pay/models/Online_payment_note_model.php @@ -465,6 +465,9 @@ class Online_payment_note_model extends CI_Model { return $result_object; } + /** + * @uses WxpayService::query + */ public function add_note_wxpay($result_data, $target_account, $type='pay') { $this->load->model('Online_payment_account_model', 'account_model'); diff --git a/webht/third_party/pay/views/payment_list.php b/webht/third_party/pay/views/payment_list.php index 94ed87ce..8a3a6843 100644 --- a/webht/third_party/pay/views/payment_list.php +++ b/webht/third_party/pay/views/payment_list.php @@ -158,9 +158,9 @@ class="text-muted" href="15016)) ?>">微信»   - + class="text-muted" + href="15002)) ?>">PayPal» +   class="text-muted" href="'15018,15035')) ?>">信用卡»   diff --git a/webht/third_party/paypal/controllers/index.php b/webht/third_party/paypal/controllers/index.php index 873b77bc..128f1f39 100644 --- a/webht/third_party/paypal/controllers/index.php +++ b/webht/third_party/paypal/controllers/index.php @@ -11,6 +11,8 @@ class Index extends CI_Controller { $this->load->model('Paypal_model'); $this->load->model('Note_model'); $this->load->model('Group_model'); + + $this->load->model('Online_payment_note_model', 'online_note'); bcscale(2); } @@ -655,10 +657,12 @@ class Index extends CI_Controller { $default_payee = new stdClass(); $default_payee->email_address = "pays@chinahighlights.com"; $default_payee->merchant_id = "JE4VX9SSVGTVN"; + $default_payee->merchant = "cht"; $tp_payee = new stdClass(); $tp_payee->email_address = "pay@trippest.com"; $tp_payee->merchant_id = "5FUTQ9SU5P7CJ"; + $tp_payee->merchant = "trippest"; $pn_txn_id = $post_data->resource->id; @@ -669,13 +673,20 @@ class Index extends CI_Controller { $pn_mc_fee = ''; $GAI_API = new stdClass(); $pn_receiver_account = ''; + $pn_receiver_account_name = ''; + $fund_type = 'pay'; + $net_amount = ''; + $transaction_status = ''; switch ($post_data->event_type) { case 'PAYMENT.SALE.COMPLETED': + $fund_type = 'pay'; $pn_invoice = $post_data->resource->invoice_number; $pn_mc_gross = $post_data->resource->amount->total; $pn_mc_currency = $post_data->resource->amount->currency; $pn_payment_status = $post_data->resource->state; + $transaction_status = 'completed'; $post_data->mc_fee = $post_data->resource->transaction_fee->value; + $net_amount = bcsub($pn_mc_gross, $post_data->mc_fee); $GAI_API->payee = new stdClass(); if (stripos($post_data->resource->soft_descriptor, 'CHINAHIG') !== false) { @@ -687,15 +698,19 @@ class Index extends CI_Controller { $GAI_API->res_links = $post_data->resource->links; $post_data->GAI_API = $GAI_API; $pn_receiver_account = $GAI_API->payee->email_address; + $pn_receiver_account_name = $GAI_API->payee->merchant; break; case 'PAYMENT.SALE.REFUNDED': $pn_invoice = $post_data->resource->invoice_number; $pn_mc_currency = $post_data->resource->amount->currency; + $fund_type = 'refund'; $pn_payment_status = 'Refunded'; + $transaction_status = 'completed'; $pn_mc_gross = '-' . $post_data->resource->amount->total; $post_data->parent_txn_id = $post_data->resource->sale_id; - $pn_mc_fee = '-' . $post_data->resource->refund_from_transaction_fee->value; + $pn_mc_fee = bcsub(0, $post_data->resource->refund_from_transaction_fee->value); $post_data->mc_fee = $pn_mc_fee; + $net_amount = isset($post_data->resource->seller_receivable_breakdown) ? '-'.$post_data->resource->seller_receivable_breakdown->refund_from_received_amount->value : $pn_mc_gross; $GAI_API->payee = new stdClass(); $GAI_API->res_links = $post_data->resource->links; @@ -704,38 +719,46 @@ class Index extends CI_Controller { case 'PAYMENT.CAPTURE.COMPLETED': case 'PAYMENT.CAPTURE.DECLINED': + $fund_type = 'pay'; $pn_invoice = $post_data->resource->invoice_id; $pn_custom = $post_data->resource->custom_id; $pn_mc_gross = $post_data->resource->amount->value; $pn_mc_currency = $post_data->resource->amount->currency_code; $pn_payment_status = $post_data->resource->status; + $transaction_status = strtolower($post_data->resource->status); $post_data->mc_fee = isset($post_data->resource->seller_receivable_breakdown) ? $post_data->resource->seller_receivable_breakdown->paypal_fee->value : 0; + $net_amount = isset($post_data->resource->seller_receivable_breakdown) ? $post_data->resource->seller_receivable_breakdown->net_amount->value : $pn_mc_gross; $GAI_API->payee = $post_data->resource->payee; $GAI_API->res_links = $post_data->resource->links; $post_data->GAI_API = $GAI_API; $pn_receiver_account = $GAI_API->payee->email_address; + $pn_receiver_account_name = $pn_receiver_account===$default_payee->email_address ? $default_payee->merchant : $tp_payee->merchant; break; case 'PAYMENT.CAPTURE.REFUNDED': + $fund_type = 'refund'; $pn_invoice = $post_data->resource->invoice_id; $pn_custom = $post_data->resource->custom_id; $pn_mc_currency = $post_data->resource->amount->currency_code; $pn_mc_gross = '-' . $post_data->resource->amount->value; $pn_payment_status = 'Refunded'; + $transaction_status = 'completed'; // $post_data->parent_txn_id = $post_data->resource->sale_id; $parent_link = $post_data->resource->links[1]->href; $parent_path = parse_url($parent_link, PHP_URL_PATH); // Get the path from the URL $post_data->parent_txn_id = basename($parent_path); - $pn_mc_fee = '-' . $post_data->resource->seller_payable_breakdown->paypal_fee->value; + $pn_mc_fee = bcsub(0, $post_data->resource->seller_payable_breakdown->paypal_fee->value); $post_data->mc_fee = $pn_mc_fee; + $net_amount = isset($post_data->resource->seller_payable_breakdown) ? '-'.$post_data->resource->seller_payable_breakdown->net_amount->value : $pn_mc_gross; $GAI_API->payee = $post_data->resource->payer; $GAI_API->res_links = $post_data->resource->links; $post_data->GAI_API = $GAI_API; $pn_receiver_account = $GAI_API->payee->email_address; + $pn_receiver_account_name = $pn_receiver_account===$default_payee->email_address ? $default_payee->merchant : $tp_payee->merchant; break; @@ -780,6 +803,24 @@ class Index extends CI_Controller { ); log_message('error','paypal-webhook-succeed ' . $pn_txn_id . ' # ' . $pn_invoice); // echo 'ok'; + $to_online = [ + 'memo' => $pn_memo, + 'transaction_id' => $pn_txn_id, + 'invoice_id' => $pn_invoice, + 'custom_id' => $pn_custom, + 'pay_amount' => $pn_mc_gross, + 'pay_currency' => $pn_mc_currency, + 'net_amount' => $net_amount, + 'pay_fee' => $post_data->mc_fee, + 'transaction_status' => $transaction_status, // $pn_payment_status, + 'payment_date' => $pn_payment_date, + '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 : '', + ]; + $this->online_note->add_note_paypal($to_online); } else { // echo 'no'; } diff --git a/webht/third_party/paypal/models/Online_payment_note_model.php b/webht/third_party/paypal/models/Online_payment_note_model.php new file mode 100644 index 00000000..a3831a63 --- /dev/null +++ b/webht/third_party/paypal/models/Online_payment_note_model.php @@ -0,0 +1,585 @@ +info = $this->load->database('INFO', TRUE); + } + + public function if_note_exists($transaction_id, $raw_id=null) + { + if ($transaction_id === null) { + return null; + } + $order_sql = $raw_id===null ? " " : " AND OPN_rawOrderId='" . $raw_id . "'"; + $ret = "SELECT TOP 1 * FROM OnlinePaymentNote WHERE OPN_transactionId=? $order_sql ORDER BY OPN_SN DESC "; + return $this->info->query($ret, array($transaction_id))->num_rows() > 0; + } + + public function if_refund_exists($refund_id) + { + if ($refund_id === null) { + return null; + } + $ret = "SELECT TOP 1 * FROM OnlinePaymentNote WHERE OPN_rawOrderId=? ORDER BY OPN_SN DESC "; + return $this->info->query($ret, array($refund_id))->num_rows() > 0; + } + + public function insert_note($column) + { + if ($column === null) { + return false; + } + $this->info->insert('OnlinePaymentNote', $column); + $ret = "SELECT TOP 1 * FROM OnlinePaymentNote WHERE OPN_transactionId=? ORDER BY OPN_SN DESC "; + return $this->info->query($ret, array($column['OPN_transactionId']))->row(); + } + + public function update_note($where, $column) + { + $update_str = $this->info->update_string('OnlinePaymentNote', $column, $where); + $this->info->query($update_str); + return TRUE; + } + + public function update_send($id, $transactionId, $send) + { + $column = array("OPN_noticeSendStatus" => $send, "OPN_noticeSendTime" => date('Y-m-d H:i:s')); + $where = " OPN_transactionId = '$transactionId' AND OPN_SN=" . $id; + return $this->update_note($where, $column); + } + + //设置订单号 + public function set_invoice($id, $pn_invoice) + { + $column = array("OPN_orderId" => $pn_invoice); + $where = " OPN_SN=" . $id; + return $this->update_note($where, $column); + } + + public function unsend_note($num=2, $method=null) + { + $this->init_query(); + $this->topnum = $num; + // $this->send = " AND (OPN_noticeSendStatus='unsend' OR OPN_noticeSendStatus='' OR OPN_noticeSendStatus IS NULL) "; + $this->search = $method===null ? "" : " AND OPN_accountMethod IN ($method) "; + + $this->send = " AND ISNULL(OPN_noticeSendStatus,'') in ('', 'unsend') "; + return $this->query_note(); + } + + public function closed_note($date, $num=2, $method=null) + { + $this->init_query(); + $this->topnum = $num; + $this->send = " AND (OPN_noticeSendStatus) = 'closed' "; + $this->search = $method===null ? "" : " AND OPN_accountMethod IN ($method) "; + $this->search .= " AND OPN_noticeTime BETWEEN '$date 00:00:00' AND '$date 23:59:59' "; + return $this->query_note(); + } + + public function sendfail_note($num=2, $method=null) + { + $this->init_query(); + $this->topnum = $num; + $this->search = $method===null ? "" : " AND OPN_accountMethod IN ($method) "; + // 1小时前 + $date = date("Y-m-d H:i:s", time() - 3600); + $this->search .= $num !== false ? " AND OPN_noticeSendTime < '$date' " : ''; + $this->send = " AND OPN_noticeSendStatus='sendfail' "; + return $this->query_note(); + } + + + public $topnum = false; + public $orderby = false; + public $send = false; + public $search = false; + public $transactionId = false; + public $payment_status = false; + // public $payment_method = false; + + public function init_query() { + $this->topnum = false; + $this->send = false; + $this->search = false; + $this->payment_status = false; + $this->transactionId = false; + // if ($GLOBALS['__PAYMENT_METHOD_CODE__']) { + // $this->payment_method = ' AND opn.OPN_accountMethod = ' . $GLOBALS['__PAYMENT_METHOD_CODE__'] . ' '; + // } + $this->orderby = ' ORDER BY OPN_SN DESC '; + } + + private $code_brandname = array( + "15002" => "PayPal", + "15010" => "PayPal", + "15015" => "Alipay", + "15016" => "WeChat", + "15018" => "Credit Card-iPaylinks", + "15035" => "Credit Card-Lianlian", + ); + private $wxpay_app = array( + "wx749246dd935ca07b" => "APP", + "wx5d01021a6d515098" => "HLY", // "花梨鹰小程序", // 交行收款码 + "wxd6c8dd69af5128cd" => "", // "NATIVE", + "wx7e605820faf98a05" => "Trippest-NATIVE", + "0" => "unknown", + ); + public function set_brandname(&$ele) + { + $ele->brand_name = "none"; + if ($this->code_brandname[$ele->OPN_accountMethod]) { + $ele->brand_name = $this->code_brandname[$ele->OPN_accountMethod]; + } + + $raw = json_decode($ele->OPN_rawContent); + $wx_app = isset($raw->app_id) ? $raw->app_id : '0'; + $wx_app = $wx_app!=='0' ? $wx_app : (isset($raw->appid) ? $raw->appid : '0'); + + $ele->app_name = ""; + if ($this->wxpay_app[$wx_app]) { + $ele->app_name = $this->wxpay_app[$wx_app]; + } + } + + public function query_note() + { + $top_sql = $this->topnum ? (" TOP " . $this->topnum) : ""; + $sql = "SELECT $top_sql opn.* + FROM [InfoManager].[dbo].[OnlinePaymentNote] opn + WHERE 1=1 "; + // $this->payment_method ? $sql.=$this->payment_method : false; + $this->send ? $sql.=$this->send : false; + $this->search ? $sql.=$this->search : false; + $this->transactionId ? $sql.=$this->transactionId : false; + $this->orderby ? $sql.=$this->orderby : false; + + // log_message('error', PHP_EOL . $sql); + + $query = $this->info->query($sql); + $result = $query->result(); + array_walk($result, 'Online_payment_note_model::set_brandname'); + return $result; + } + + public function get_note($opn_id) + { + $this->init_query(); + $this->topnum=1; + $this->search = " AND opn.OPN_SN=" . $this->info->escape($opn_id); + return $this->query_note(); + } + + /** + * @param mixed $where ['OPN_SN' => ['=', {val}] ] + */ + public function get_note_where($where) { + $this->init_query(); + $this->topnum=1; + $this->search = ''; + foreach ($where as $key => $value) { + $where_val = ''; + switch (strtolower($value[0])) { + case '=': + $where_val = " = {$this->info->escape($value[1])}"; + break; + case 'in': + $where_val = " IN ({$this->info->escape($value[1])})"; + break; + + default: + $where_val = " = {$this->info->escape($value[1])}"; + break; + } + $this->search .= " AND {$key} {$where_val} "; + } + return $this->query_note(); + } + + public function search_key($keyword) + { + $this->init_query(); + $this->topnum = 300; + $keyword = trim($keyword); + $search_sql = ""; + if ( ! empty($keyword)) { + $search_sql.=" AND ( OPN_transactionId = '$keyword' + OR OPN_relatedId like '%$keyword%' + OR OPN_orderId like '%$keyword%' + OR OPN_rawOrderId like '%$keyword%' )"; + } + $this->search = $search_sql; + return $this->query_note(); + } + + public function search_date($date, $method=null) + { + $this->init_query(); + $this->search = $method===null ? "" : " AND OPN_accountMethod IN ($method) "; + $this->search .= " AND OPN_noticeTime BETWEEN '$date 00:00:00' AND '$date 23:59:59' "; + $this->send = " AND isnull(OPN_noticeSendStatus,'') NOT IN ('', 'sendfail', 'unsend', 'closed' )"; + return $this->query_note(); + } + + /*! + * 导出记录 + * @date 2019-04-28 + * @param integer $sn [description] + */ + public function list_export_record($sn=0) + { + $search_sql = $sn===0 ? "" : " and TEL_SN=$sn "; + $sql = "SELECT TOP 10 * + FROM [InfoManager].[dbo].[Transaction_Export_Log] + WHERE 1=1 + $search_sql + order by TEL_SN desc"; + return $this->info->query($sql)->result(); + } + + public function query_ipalinkslog_memo($memo) + { + $sql = "SELECT * + from IPayLinksLog + where CHARINDEX(?,IPL_memo)>0"; + return $this->info->query($sql, array($memo))->row(); + } + + public function query_ipalinkslog_dealid($id) + { + $sql = "SELECT * + from IPayLinksLog + where IPL_dealId=?"; + return $this->info->query($sql, array($id))->row(); + } + + public function query_related($related_id) + { + $this->init_query(); + $this->search = " AND OPN_transactionId='$related_id' and OPN_accountType is not null "; + return $this->query_note(); + } + + /** + * + * @param [type] $pay_method + * + * 选择日期: + * @param [type] $from_date + * @param [type] $to_date + * + * 环比日期: + * @param [type] $from_date_fomer + * @param [type] $to_date_former + * + * 上年同期: + * @param [type] $from_date_year + * @param [type] $to_date_year + * + * 本年度开始日期: {Year}-01-01 + * @param [type] $at_year + */ + public function total_by_method($pay_method, + $from_date, $to_date, + $from_date_fomer, $to_date_former, + $from_date_year, $to_date_year, + $at_year + ) + { + $sql = " SET NOCOUNT ON; + DECLARE @type varchar(10)='$pay_method' + + declare @this_year date='$at_year' + declare @from_date date='$from_date' + declare @to_date date='$to_date' + + declare @from_date_former date='$from_date_fomer' + declare @to_date_former date='$to_date_former' + + declare @from_date_last_year date='$from_date_year' + declare @to_date_last_year date='$to_date_year' + + declare @unique_opn table ( + id bigint IDENTITY(1,1) primary key, + --opn_sn bigint, + OPN_transactionId varchar(512), + OPN_accountMethod varchar(100), + OPN_noticeType varchar(100), + OPN_currency varchar(100), + OPN_orderAmount varchar(512), + OPN_entryAmountCNY varchar(512), + OPN_completeTime date + ) + + insert into @unique_opn ( + --opn_sn , + OPN_transactionId , + OPN_accountMethod , + OPN_noticeType , + OPN_currency , + OPN_orderAmount , + OPN_entryAmountCNY , + OPN_completeTime + ) + select + --opn_sn , + OPN_transactionId , + OPN_accountMethod , + OPN_noticeType , + OPN_currency , + OPN_orderAmount , + OPN_entryAmountCNY , + OPN_completeTime + from InfoManager.dbo.OnlinePaymentNote + where OPN_completeTime>@from_date_last_year + group by + --opn_sn , + OPN_transactionId , + OPN_accountMethod , + OPN_noticeType , + OPN_currency , + OPN_orderAmount , + OPN_entryAmountCNY , + OPN_completeTime + + SELECT isnull(cal.select_date_method,ISNULL(cal.last_method,ISNULL(cal.year_method, cal.this_year_method ))) as OPN_accountMethod + ,method_total_income,method_total_income_cmp + ,isnull(year_method_total_income,0) year_method_total_income + ,this_year_method_total_income + --,REPLACE(CONVERT(VARCHAR, CONVERT(MONEY, method_total_income), 1), '.00', '') method_total_income + --,REPLACE(CONVERT(VARCHAR, CONVERT(MONEY, method_total_income_cmp), 1), '.00', '') method_total_income_cmp + ,case when ISNULL( method_total_income_cmp,0)=0 then 100 else CONVERT(decimal(10,2), + round( (method_total_income-method_total_income_cmp)/method_total_income_cmp*100 ,2 ) + ) end AS quarter_rate + --,REPLACE(CONVERT(VARCHAR, CONVERT(MONEY, isnull(year_method_total_income,0)), 1), '.00', '') year_method_total_income + + ,case when ISNULL( year_method_total_income,0)=0 then 100 else CONVERT(decimal(10,2), + round( (method_total_income-year_method_total_income)/year_method_total_income*100 ,2 ) + ) end AS year_rate + --,REPLACE(CONVERT(VARCHAR, CONVERT(MONEY, this_year_method_total_income), 1), '.00', '') this_year_method_total_income + FROM + ( + SELECT * + FROM + ( + SELECT OPN_accountMethod,OPN_accountMethod select_date_method + ,SUM( + ISNULL( CONVERT(decimal(10,2),OPN_entryAmountCNY), + Tourmanager.dbo.GetSSJEFromSQJE( + OPN_accountMethod, + REPLACE(OPN_currency, 'CNY', 'RMB'), + CONVERT(decimal(10,2),OPN_orderAmount) + ) + ) + ) AS method_total_income + FROM @unique_opn + WHERE OPN_completeTime BETWEEN @from_date AND @to_date + and OPN_noticeType=@type + GROUP BY OPN_accountMethod + ) AS method + full join ( + SELECT OPN_accountMethod last_method + ,SUM( + [Tourmanager].[dbo].ZeroToOne( + ISNULL( CONVERT(decimal(10,2),OPN_entryAmountCNY), + Tourmanager.dbo.GetSSJEFromSQJE( + OPN_accountMethod, + REPLACE(OPN_currency, 'CNY', 'RMB'), + CONVERT(decimal(10,2),OPN_orderAmount) + ) + + ) + ) + ) AS method_total_income_cmp + FROM @unique_opn + WHERE OPN_completeTime BETWEEN @from_date_former AND @to_date_former + and OPN_noticeType=@type + GROUP BY OPN_accountMethod + ) as cmp on method.OPN_accountMethod=cmp.last_method + full join ( + SELECT OPN_accountMethod year_method + ,SUM( + [Tourmanager].[dbo].ZeroToOne( + ISNULL( CONVERT(decimal(10,2),OPN_entryAmountCNY), + Tourmanager.dbo.GetSSJEFromSQJE( + OPN_accountMethod, + REPLACE(OPN_currency, 'CNY', 'RMB'), + CONVERT(decimal(10,2),OPN_orderAmount) + ) + + ) + ) + ) AS year_method_total_income + FROM @unique_opn + WHERE OPN_completeTime BETWEEN @from_date_last_year AND @to_date_last_year + and OPN_noticeType=@type + GROUP BY OPN_accountMethod + ) as year_cmp on isnull(method.OPN_accountMethod,cmp.last_method)=year_cmp.year_method + full join ( + SELECT OPN_accountMethod this_year_method + ,SUM( + [Tourmanager].[dbo].ZeroToOne( + ISNULL( CONVERT(decimal(10,2),OPN_entryAmountCNY), + Tourmanager.dbo.GetSSJEFromSQJE( + OPN_accountMethod, + REPLACE(OPN_currency, 'CNY', 'RMB'), + CONVERT(decimal(10,2),OPN_orderAmount) + ) + + ) + ) + ) AS this_year_method_total_income + FROM @unique_opn + WHERE OPN_completeTime BETWEEN @this_year AND @to_date + and OPN_noticeType=@type + GROUP BY OPN_accountMethod + ) as this_year_cmp on isnull(method.OPN_accountMethod,cmp.last_method)=this_year_cmp.this_year_method + ) AS cal + "; + // var_dump($this->info->query($sql)); // 无法输出结果集 + include('c:/database_conn.php'); + $connection = array( + 'UID' => $db['HT']['username'], + 'PWD' => $db['HT']['password'], + 'Database' => 'tourmanager', + 'ConnectionPooling' => 1, + 'CharacterSet' => 'utf-8', + 'ReturnDatesAsStrings' => 1 + ); + $conn = sqlsrv_connect($db['HT']['hostname'], $connection); + $stmt = sqlsrv_query($conn, $sql); + //每一个select都会产生一个结果集,取某个结果集就需要从第一个移动到需要的那个结果集 + //如果结果集为空就移到下一个 + while (sqlsrv_has_rows($stmt) !== TRUE) { + sqlsrv_next_result($stmt); + } + $result_object = array(); + while ($row = sqlsrv_fetch_object($stmt)) { + $result_object[] = $row; + } + sqlsrv_free_stmt($stmt); + sqlsrv_close($conn); + return $result_object; + } + + /** + * @uses WxpayService::query + */ + public function add_note_wxpay($result_data, $target_account, $type='pay') + { + $this->load->model('paypal_model', 'account_model'); + foreach ($result_data as $key => $row) { + $save_column = array(); + $save_column['OPN_accountMethod'] = $this->config->item('method_code', 'wxpay'); + $total_fee = bcdiv($row['total_fee'], $this->config->item('currency_unit', 'wxpay')); + $OPN_currency = isset($row['currency_type']) ? $row['currency_type'] : (isset($row['fee_type']) ? $row['fee_type'] : 'CNY'); + if ($type != 'pay') { + // 退款 + $refund_fee = bcdiv($row['refund_fee'], $this->config->item('currency_unit', 'wxpay')); + $ssje = $this->account_model->get_ssje($refund_fee, str_replace("CNY", "RMB", strtoupper($OPN_currency)), $save_column['OPN_accountMethod']); + $save_column['OPN_transactionId'] = $row['refund_id']; + $save_column['OPN_orderAmount'] = "-" . $refund_fee; + $save_column['OPN_payAmount'] = "-" . $refund_fee; + $save_column['OPN_resultCode'] = $row['refund_status']; + $save_column['OPN_resultMsg'] = $row['refund_status']; + $save_column['OPN_entryAmountCNY'] = floatval("-" . $ssje); + $save_column['OPN_noticeType'] = $type; + $save_column['OPN_relatedId'] = $row['transaction_id']; + $save_column['OPN_acquiringTime'] = date('Y-m-d H:i:s', strtotime($row['refund_success_time'])); + $save_column['OPN_completeTime'] = date('Y-m-d H:i:s', strtotime($row['refund_success_time'])); + } else { + // 收款 + $total_fee = bcdiv($row['total_fee'], $this->config->item('currency_unit', 'wxpay')); + $ssje = $this->account_model->get_ssje($total_fee, str_replace("CNY", "RMB", strtoupper($OPN_currency)), $save_column['OPN_accountMethod']); + $save_column['OPN_transactionId'] = $row['transaction_id']; + $save_column['OPN_orderAmount'] = $total_fee; + $save_column['OPN_payAmount'] = $total_fee; + $save_column['OPN_resultCode'] = $row['trade_state']; + $save_column['OPN_resultMsg'] = $row['trade_state']; + $save_column['OPN_entryAmountCNY'] = floatval($ssje); + $save_column['OPN_noticeType'] = 'pay'; + $save_column['OPN_relatedId'] = ''; + $save_column['OPN_acquiringTime'] = date('Y-m-d H:i:s', strtotime($row['time_end'])); + $save_column['OPN_completeTime'] = date('Y-m-d H:i:s', strtotime($row['time_end'])); + } + $save_column['OPN_noticeSendStatus'] = 'unsend'; + $save_column['OPN_transactionResult'] = 'completed'; + $save_column['OPN_orderId'] = $row['out_trade_no']; + $save_column['OPN_rawOrderId'] = $row['out_trade_no']; + $save_column['OPN_invoiceId'] = $row['out_trade_no']; + $save_column['OPN_subject'] = isset($row['item_name']) ? $row['item_name'] : ''; + $save_column['OPN_currency'] = $OPN_currency; + $save_column['OPN_remark'] = empty($row['attach']) ? '' : json_encode($row['attach']); + $save_column['OPN_payerLogId'] = isset( $row['openid']) ? $row['openid'] : ''; + $save_column['OPN_fundSource'] = $target_account; + $save_column['OPN_rawContent'] = json_encode($row); + $save_column['OPN_noticeTime'] = date('Y-m-d H:i:s'); + + // $this->insert_note($save_column) ; // todo: + // log_message('error', 'test: ' . __CLASS__ . PHP_EOL . var_export($save_column, 1)); + } + return; + } + + /** + * + * $to_online = [ + * 'memo' => $pn_memo, + * 'transaction_id' => $pn_txn_id, + * 'invoice_id' => $pn_invoice, + * 'custom_id' => $pn_custom, + * 'pay_amount' => $pn_mc_gross, + * 'pay_currency' => $pn_mc_currency, + * 'net_amount' => $net_amount, + * 'pay_fee' => $post_data->mc_fee, + * 'transaction_status' => 'completed', // $pn_payment_status, + * 'payment_date' => $pn_payment_date, + * '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 : '', + * ]; + */ + 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']))); + + $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'] = bcsub(0,$result_data['pay_fee']); + $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'] = $result_data['referer_id']; + $save_column['OPN_acquiringTime'] = $result_data['payment_date']; + $save_column['OPN_completeTime'] = $result_data['payment_date']; + + $save_column['OPN_accountMethod'] = 15002; // $this->config->item('method_code', 'paypal'); + $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['custom_id']) ? $result_data['custom_id'] : ''; + $save_column['OPN_currency'] = $result_data['pay_currency']; + // $save_column['OPN_remark'] = empty($row['attach']) ? '' : json_encode($row['attach']); + // $save_column['OPN_payerLogId'] = isset( $row['openid']) ? $row['openid'] : ''; + $save_column['OPN_fundSource'] = $result_data['fund_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; + } + +} +