|
|
<?php
|
|
|
|
|
|
if (!defined('BASEPATH'))
|
|
|
exit('No direct script access allowed');
|
|
|
|
|
|
require_once __DIR__ ."/Mobile_Detect.php";
|
|
|
|
|
|
class AlipayTradeService extends CI_Controller
|
|
|
{
|
|
|
//支付宝网关地址
|
|
|
public $gateway_url = "https://openapi.alipay.com/gateway.do";
|
|
|
|
|
|
//支付宝公钥
|
|
|
public $alipay_public_key;
|
|
|
|
|
|
//商户私钥
|
|
|
public $private_key;
|
|
|
|
|
|
//应用id
|
|
|
public $appid;
|
|
|
|
|
|
//UID
|
|
|
public $seller_id;
|
|
|
|
|
|
//编码格式
|
|
|
public $charset = "UTF-8";
|
|
|
|
|
|
public $token = NULL;
|
|
|
|
|
|
//返回数据格式
|
|
|
public $format = "json";
|
|
|
|
|
|
//签名方式
|
|
|
public $signtype = "RSA2";
|
|
|
|
|
|
public $timeout_express = "1m";
|
|
|
|
|
|
// cht
|
|
|
public function __construct(){
|
|
|
parent::__construct();
|
|
|
$this->load->library('alipay/AopSdk');
|
|
|
// real URL
|
|
|
$this->config->load('alipay');
|
|
|
$this->config->load('wxpay', true);
|
|
|
// test URL
|
|
|
// $this->config->load('dev_alipay');
|
|
|
|
|
|
$this->load->model('AlipayTradePagePayContentBuilder');
|
|
|
$this->load->model('AlipayTradeWapPayContentBuilder');
|
|
|
$this->load->model('AlipayTradeQueryContentBuilder');
|
|
|
$this->load->model('Alipay_note_model');
|
|
|
$this->load->model('Alipay_model');
|
|
|
$this->load->helper('payment');
|
|
|
$this->load->model('Online_payment_account_model', 'payment_model');
|
|
|
|
|
|
$this->gateway_url = $this->config->item('gatewayUrl');
|
|
|
$this->appid = $this->config->item('app_id');
|
|
|
$this->seller_id = $this->config->item('seller_id');
|
|
|
$this->private_key = $this->config->item('merchant_private_key');
|
|
|
$this->alipay_public_key = $this->config->item('alipay_public_key');
|
|
|
$this->charset = $this->config->item('charset');
|
|
|
$this->signtype = $this->config->item('sign_type');
|
|
|
$this->timeout_express = $this->config->item('timeout_express');
|
|
|
|
|
|
$this->return_url = $this->config->item('return_url');
|
|
|
$this->notify_url = $this->config->item('notify_url');
|
|
|
|
|
|
if(empty($this->appid)||trim($this->appid)==""){
|
|
|
log_message('error','Alipay ERROR appid should not be NULL!');
|
|
|
}
|
|
|
if(empty($this->private_key)||trim($this->private_key)==""){
|
|
|
log_message('error','Alipay ERROR private_key should not be NULL!');
|
|
|
}
|
|
|
if(empty($this->alipay_public_key)||trim($this->alipay_public_key)==""){
|
|
|
log_message('error','Alipay ERROR alipay_public_key should not be NULL!');
|
|
|
}
|
|
|
if(empty($this->charset)||trim($this->charset)==""){
|
|
|
log_message('error','Alipay ERROR charset should not be NULL!');
|
|
|
}
|
|
|
if(empty($this->gateway_url)||trim($this->gateway_url)==""){
|
|
|
log_message('error','Alipay ERROR gateway_url should not be NULL!');
|
|
|
}
|
|
|
header('Access-Control-Allow-Origin:*');
|
|
|
header('Access-Control-Allow-Methods:POST, GET');
|
|
|
header('Access-Control-Max-Age:0');
|
|
|
header('Access-Control-Allow-Headers:x-requested-with, Content-Type');
|
|
|
}
|
|
|
public function index()
|
|
|
{
|
|
|
$this->note_list();
|
|
|
}
|
|
|
/*!
|
|
|
* 异步通知
|
|
|
* 必须返回"success"给支付系统
|
|
|
* @author LYT <lyt@hainatravel.com>
|
|
|
* @date 2017-09-13
|
|
|
* @return [type] [description]
|
|
|
*/
|
|
|
public function alipay_notice()
|
|
|
{
|
|
|
error_reporting(0);
|
|
|
$resp_arr = $this->input->post();
|
|
|
$asyns_resp = $this->check($resp_arr);
|
|
|
// 未得到结果
|
|
|
if (empty($asyns_resp->data->out_trade_no)) {
|
|
|
echo "failed";
|
|
|
return;
|
|
|
}
|
|
|
if ( isset($asyns_resp->data->refund_fee) && true === $this->if_note_exists($asyns_resp->data->out_biz_no) ) {
|
|
|
echo "success";
|
|
|
return;
|
|
|
} else if ( ! isset($asyns_resp->data->refund_fee) && true === $this->if_note_exists($asyns_resp->data->trade_no)) {
|
|
|
echo "success";
|
|
|
return;
|
|
|
}
|
|
|
$notify_type = "pay";
|
|
|
if (isset($asyns_resp->data->refund_fee)) {
|
|
|
$notify_type = "refund";
|
|
|
}
|
|
|
$code = isset($asyns_resp->data->code) ? strval($asyns_resp->data->code) : NULL ;
|
|
|
$buyer = isset($asyns_resp->data->buyer_logon_id) ? strval($asyns_resp->data->buyer_logon_id) : NULL ;
|
|
|
if (strcmp(trim(strval($asyns_resp->data->trade_status)), "TRADE_SUCCESS") == 0
|
|
|
|| strcmp(trim(strval($asyns_resp->data->trade_status)), "TRADE_FINISHED") == 0) {
|
|
|
if ($notify_type === "pay") {
|
|
|
$this->Alipay_note_model->save_alipay(
|
|
|
strval($asyns_resp->data->trade_no)
|
|
|
,strval($asyns_resp->data->out_trade_no)
|
|
|
,"CNY"
|
|
|
,strval($asyns_resp->data->total_amount)
|
|
|
,NULL
|
|
|
,NULL
|
|
|
,strval($asyns_resp->data->gmt_create)
|
|
|
,strval($asyns_resp->data->gmt_payment)
|
|
|
,json_encode($asyns_resp->data,JSON_UNESCAPED_UNICODE)
|
|
|
,$notify_type
|
|
|
,$code
|
|
|
,strval($asyns_resp->data->trade_status)
|
|
|
,NULL
|
|
|
,$buyer
|
|
|
);
|
|
|
} else if ($notify_type == "refund") {
|
|
|
$this->Alipay_note_model->save_alipay(
|
|
|
strval($asyns_resp->data->out_biz_no)
|
|
|
,strval($asyns_resp->data->out_trade_no)
|
|
|
,"CNY"
|
|
|
,"-" . strval($asyns_resp->data->refund_fee)
|
|
|
,NULL
|
|
|
,NULL
|
|
|
,strval($asyns_resp->data->gmt_refund)
|
|
|
,strval($asyns_resp->data->notify_time)
|
|
|
,json_encode($asyns_resp->data,JSON_UNESCAPED_UNICODE)
|
|
|
,$notify_type
|
|
|
,$code
|
|
|
,strval($asyns_resp->data->trade_status)
|
|
|
,NULL
|
|
|
,$buyer
|
|
|
);
|
|
|
}
|
|
|
// 查询payer
|
|
|
// $this->AlipayTradeQueryContentBuilder->setTradeNo($asyns_resp->data->trade_no);
|
|
|
// if ($asyns_resp->data->out_trade_no) {
|
|
|
// $this->AlipayTradeQueryContentBuilder->setOutTradeNo($asyns_resp->data->out_trade_no);
|
|
|
// }
|
|
|
// $response = $this->Query($this->AlipayTradeQueryContentBuilder);
|
|
|
// $resp_arr = (Array) $response;
|
|
|
// $query_resp = $this->check($resp_arr);
|
|
|
// if (strcmp(strval($response->trade_status), "TRADE_SUCCESS") == 0) {
|
|
|
// $this->Alipay_note_model->update_query($response->trade_no,$response->buyer_logon_id);
|
|
|
// }
|
|
|
}
|
|
|
// 返回状态码200
|
|
|
echo "success";
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
public function if_note_exists($dealId)
|
|
|
{
|
|
|
return $this->Alipay_note_model->note_exists($dealId) ? true : false;
|
|
|
}
|
|
|
|
|
|
public function request_faild()
|
|
|
{
|
|
|
$data["code"] = 1;
|
|
|
$data["msg"] = "Unknow request";
|
|
|
$data["error_code"] = "Unknow";
|
|
|
$url_query = "";
|
|
|
foreach ($data as $key => $v) {
|
|
|
$url_query .= $key . "=" . urlencode($v) ."&";
|
|
|
}
|
|
|
redirect("https://www.chinahighlights.com/secureipay/load_return" . "?" . substr($url_query, 0, -1));
|
|
|
}
|
|
|
|
|
|
public function get_url_string()
|
|
|
{
|
|
|
$param = ($this->input->get_post('param'));
|
|
|
return base64_decode($param);
|
|
|
}
|
|
|
|
|
|
// cht
|
|
|
public function pay_fun()
|
|
|
{
|
|
|
$param_string = $this->get_url_string();
|
|
|
@parse_str($param_string, $param_arr);
|
|
|
if ( ! $param_string || empty($param_arr['order_id'])) {
|
|
|
$this->request_faild();
|
|
|
return false;
|
|
|
}
|
|
|
//商户订单号,商户网站订单系统中唯一订单号,必填
|
|
|
$out_trade_no = trim($param_arr['order_id']);
|
|
|
//订单名称,必填
|
|
|
$subject = trim($param_arr['subject']);
|
|
|
//付款金额,必填
|
|
|
$total_amount = trim($param_arr['total_amount']);
|
|
|
//商品描述,可空
|
|
|
$body = str_replace("\n", "<br>", trim($param_arr['body']));
|
|
|
|
|
|
$post_return = $param_arr["return_url"] ? trim($param_arr["return_url"]) : false;
|
|
|
$post_notify = $param_arr["notify_url"] ? trim($param_arr["notify_url"]) : false;
|
|
|
|
|
|
$this->return_url = !empty($post_return) ? ($post_return) : $this->return_url;
|
|
|
$this->notify_url = !empty($post_notify) ? ($post_notify) : $this->notify_url;
|
|
|
|
|
|
$detect = new Mobile_Detect;
|
|
|
if ($detect->isMobile() && !$detect->isTablet()) {
|
|
|
// mobile page
|
|
|
$this->AlipayTradeWapPayContentBuilder->setBody($body);
|
|
|
$this->AlipayTradeWapPayContentBuilder->setSubject($subject);
|
|
|
$this->AlipayTradeWapPayContentBuilder->setOutTradeNo($out_trade_no);
|
|
|
$this->AlipayTradeWapPayContentBuilder->setTotalAmount($total_amount);
|
|
|
$this->AlipayTradeWapPayContentBuilder->setTimeExpress($timeout_express);
|
|
|
$result = $this->wapPay(
|
|
|
$this->AlipayTradeWapPayContentBuilder,
|
|
|
$this->return_url,
|
|
|
$this->notify_url
|
|
|
);
|
|
|
} else {
|
|
|
// PC page
|
|
|
$this->AlipayTradePagePayContentBuilder->setBody($body);
|
|
|
$this->AlipayTradePagePayContentBuilder->setSubject($subject);
|
|
|
$this->AlipayTradePagePayContentBuilder->setTotalAmount($total_amount);
|
|
|
$this->AlipayTradePagePayContentBuilder->setOutTradeNo($out_trade_no);
|
|
|
$url = $this->pagePay(
|
|
|
$this->AlipayTradePagePayContentBuilder,
|
|
|
$this->return_url,
|
|
|
$this->notify_url
|
|
|
);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* alipay.trade.page.pay
|
|
|
* @param $builder 业务参数,使用buildmodel中的对象生成。
|
|
|
* @param $return_url 同步跳转地址,公网可以访问
|
|
|
* @param $notify_url 异步通知地址,公网可以访问
|
|
|
* @return $response 支付宝返回的信息
|
|
|
*/
|
|
|
function pagePay($builder,$return_url,$notify_url) {
|
|
|
|
|
|
$biz_content=$builder->getBizContent();
|
|
|
|
|
|
$request = new AlipayTradePagePayRequest();
|
|
|
|
|
|
$request->setNotifyUrl($notify_url);
|
|
|
$request->setReturnUrl($return_url);
|
|
|
$request->setBizContent ( $biz_content );
|
|
|
|
|
|
// 首先调用支付api
|
|
|
$response = $this->aopclientRequestExecute ($request,true);
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* alipay.trade.wap.pay
|
|
|
* @param $builder 业务参数,使用buildmodel中的对象生成。
|
|
|
* @param $return_url 同步跳转地址,公网可访问
|
|
|
* @param $notify_url 异步通知地址,公网可以访问
|
|
|
* @return $response 支付宝返回的信息
|
|
|
*/
|
|
|
function wapPay($builder,$return_url,$notify_url) {
|
|
|
|
|
|
$biz_content=$builder->getBizContent();
|
|
|
|
|
|
$request = new AlipayTradeWapPayRequest();
|
|
|
|
|
|
$request->setNotifyUrl($notify_url);
|
|
|
$request->setReturnUrl($return_url);
|
|
|
$request->setBizContent ( $biz_content );
|
|
|
|
|
|
// 首先调用支付api
|
|
|
$response = $this->aopclientRequestExecute ($request,true);
|
|
|
// $response = $response->alipay_trade_wap_pay_response;
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* sdkClient
|
|
|
* @param $request 接口请求参数对象。
|
|
|
* @param $ispage 是否是页面接口,电脑网站支付是页面表单接口。
|
|
|
* @return $response 支付宝返回的信息
|
|
|
*/
|
|
|
function aopclientRequestExecute($request,$ispage=false) {
|
|
|
|
|
|
$aop = new AopClient ();
|
|
|
$aop->gatewayUrl = $this->gateway_url;
|
|
|
$aop->appId = $this->appid;
|
|
|
$aop->rsaPrivateKey = $this->private_key;
|
|
|
$aop->alipayrsaPublicKey = $this->alipay_public_key;
|
|
|
$aop->apiVersion = "1.0";
|
|
|
$aop->postCharset = $this->charset;
|
|
|
$aop->format = $this->format;
|
|
|
$aop->signType = $this->signtype;
|
|
|
// 开启页面信息输出
|
|
|
$aop->debugInfo = true;
|
|
|
$result = null;
|
|
|
if($ispage)
|
|
|
{
|
|
|
$result = $aop->pageExecute($request,"post");
|
|
|
echo $result;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
$result = $aop->Execute($request);
|
|
|
}
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
/*!
|
|
|
* 定时执行
|
|
|
* 处理异步通知: 解析订单号, 邮件外联/客人; 插入付款记录
|
|
|
* @author LYT <lyt@hainatravel.com>
|
|
|
*/
|
|
|
public function send_alipay($pn_txn_id = false, $old_ssje=null)
|
|
|
{
|
|
|
$data = array();
|
|
|
$int = 0;
|
|
|
|
|
|
//优先处理指定的交易号,用于修正交易号直接发送通知
|
|
|
if ( ! empty($pn_txn_id)) {
|
|
|
$data['unsend_list'] = array($this->Alipay_note_model->note($pn_txn_id));
|
|
|
}
|
|
|
|
|
|
// 待处理的
|
|
|
if (empty($data['unsend_list'])) {
|
|
|
$data['unsend_list'] = $this->Alipay_note_model->unsend(10);
|
|
|
}
|
|
|
//没有未处理的数据再查找处理失败的数据
|
|
|
if (empty($data['unsend_list'])) {
|
|
|
$data['unsend_list'] = $this->Alipay_note_model->failnote(20);
|
|
|
}
|
|
|
$handpick = empty($pn_txn_id) ? false : true;
|
|
|
$show_index = 0;
|
|
|
foreach ($data['unsend_list'] as $item) {
|
|
|
|
|
|
//已经发送的不处理,防止重复发送
|
|
|
if ($item->ALI_sent == 'send' && empty($pn_txn_id)) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//检测是否是APP订单
|
|
|
if ((strpos($item->ALI_memo, 'China Train Booking') !== false) || (strpos($item->ALI_memo, 'ChinaTrainBooking') !== false)) {
|
|
|
$item->ALI_orderId .= "_A";
|
|
|
// $this->Alipay_note_model->update_send($item->ALI_dealId, 'send');
|
|
|
}
|
|
|
|
|
|
//退款状态默认为已经处理,陆燕在退款前手动通知外联了,系统跳过处理
|
|
|
if ($item->ALI_payType == 'refund') {
|
|
|
$this->send_refund($item, $old_ssje, $handpick);
|
|
|
// $this->Alipay_note_model->update_send($item->ALI_dealId, 'send');
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//只处理完成状态,其他状态由陆燕处理
|
|
|
if (strcmp(trim($item->ALI_resultMsg), "TRADE_SUCCESS")!==0 && strcmp(trim($item->ALI_resultMsg), "TRADE_FINISHED")!==0 ) {
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'send');
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//根据note信息找到订单号
|
|
|
$orderid_info = analysis_orderid($item->ALI_orderId);
|
|
|
|
|
|
//找不到订单号,设置为发送失败标示
|
|
|
if (empty($orderid_info)) {
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail');
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//根据订单号查找外联信息
|
|
|
$orderid_info = json_decode($orderid_info);
|
|
|
$advisor_info = $this->Alipay_model->get_order($orderid_info->orderid, false, $orderid_info->ordertype, $handpick);
|
|
|
|
|
|
//查不到订单信息
|
|
|
if (empty($advisor_info)) {
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail');
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//更新正确的订单信息到记录中,以这个为主
|
|
|
$this->Alipay_note_model->set_invoice($item->ALI_dealId, $orderid_info->orderid . '_' . $orderid_info->ordertype);
|
|
|
|
|
|
//检测是否是APP订单,默认不处理
|
|
|
if ($orderid_info->ordertype == 'A' && $item->ALI_payType == 'pay') { //APP自动出票的订单不需要处理
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'send');
|
|
|
continue;
|
|
|
}
|
|
|
//添加支付信息入库
|
|
|
//没有分配订单之前先添加付款记录,这个过程可能会执行多次,必须在添加记录前查找是否有数据
|
|
|
if (!empty($orderid_info)) {
|
|
|
$currencyCode = str_replace("CNY", "RMB", trim(mb_strtoupper($item->ALI_currencyCode)));
|
|
|
$ssje = $item->ALI_orderAmount;
|
|
|
if (intval($item->ALI_stateCode)!==1) {
|
|
|
$ssje = $this->Alipay_model->get_ssje($item->ALI_orderAmount, $currencyCode);
|
|
|
}
|
|
|
$ssje = $old_ssje===null ? $ssje : $old_ssje;
|
|
|
$USD_amount = $this->Alipay_model->get_USD($item->ALI_orderAmount, $currencyCode);
|
|
|
//更新还没有填的客邮和交易号de收款记录(商务订单)
|
|
|
if (isset($advisor_info->order_type) && $advisor_info->order_type == 0) {
|
|
|
$ht_memo = '交易号(自动录入):' . $item->ALI_dealId;
|
|
|
$GAI_COLI_SN = isset($advisor_info->COLI_SN) ? $advisor_info->COLI_SN : 0;
|
|
|
//CHTAPP订单添加记录前判断是否有记录,以前的APP版本没有交易号,只能拿金额来判断
|
|
|
if (substr($advisor_info->COLI_WebCode, 0, 6) == 'CHTAPP' && strstr($advisor_info->COLI_WebCode, "-") != '-biz') {
|
|
|
//只判断前6位字符,CHTAPP-fr CHTAPP-jp等各语种都属于APP订单
|
|
|
$this->Alipay_model->add_account_info_forAPP(
|
|
|
$GAI_COLI_SN,
|
|
|
$advisor_info->COLI_ID,
|
|
|
$item->ALI_orderAmount,
|
|
|
$item->ALI_completeTime,
|
|
|
$currencyCode,
|
|
|
$ssje,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_payerName,
|
|
|
$item->ALI_payerEmail,
|
|
|
$item->ALI_dealId,
|
|
|
$ht_memo);
|
|
|
if ($advisor_info->COLI_WebCode == 'CHTAPP' && $advisor_info->COLI_State == 11) { //只修改APP组的订单状态,并且订单进度是我的订单
|
|
|
$this->Alipay_model->update_biz_coli_state($GAI_COLI_SN, 8); //把订单状态改为已付款
|
|
|
$this->Alipay_model->insert_biz_order_log($GAI_COLI_SN, 'BS8');
|
|
|
}
|
|
|
} else {
|
|
|
// 把订单状态设置为13-新订单已支付
|
|
|
if (false == $this->Alipay_model->if_biz_gai_exists($item->ALI_dealId) ) {
|
|
|
$this->Alipay_model->update_biz_coli_state($GAI_COLI_SN, 13);
|
|
|
$this->Alipay_model->insert_biz_order_log($GAI_COLI_SN, 'BS13');
|
|
|
}
|
|
|
$this->Alipay_model->add_account_info(
|
|
|
$GAI_COLI_SN,
|
|
|
$advisor_info->COLI_ID,
|
|
|
$item->ALI_orderAmount,
|
|
|
$item->ALI_completeTime,
|
|
|
$currencyCode,
|
|
|
$USD_amount,
|
|
|
$ssje,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_acquiringTime,
|
|
|
$item->ALI_payerName,
|
|
|
$item->ALI_payerEmail,
|
|
|
$item->ALI_dealId,
|
|
|
$ht_memo
|
|
|
);
|
|
|
// 更新订单主表付款方式,防止没访问thankyou-train.asp
|
|
|
$this->Alipay_model->update_paymanner($GAI_COLI_SN);
|
|
|
}
|
|
|
}
|
|
|
//更新还没有填的客邮和交易号de收款记录(传统订单)
|
|
|
elseif (isset($advisor_info->order_type) && $advisor_info->order_type == 1) {
|
|
|
$ht_memo = '交易号(自动录入):' . $item->ALI_dealId;
|
|
|
$GAI_COLI_SN = isset($advisor_info->COLI_SN) ? $advisor_info->COLI_SN : 0;
|
|
|
$gai_sn = $this->Alipay_model->add_tour_account_info(
|
|
|
$GAI_COLI_SN,
|
|
|
$item->ALI_orderAmount,
|
|
|
$item->ALI_acquiringTime,
|
|
|
$currencyCode,
|
|
|
$ssje,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_acquiringTime,
|
|
|
$item->ALI_payerName,
|
|
|
$item->ALI_payerEmail,
|
|
|
$item->ALI_dealId,
|
|
|
$ht_memo
|
|
|
);
|
|
|
//添加汉特的订单提醒
|
|
|
$this->Alipay_model->update_coli_introduction($GAI_COLI_SN, '已支付 ' . mb_strtoupper($item->ALI_currencyCode) . $item->ALI_orderAmount);
|
|
|
// 添加HT任务
|
|
|
$this->Alipay_model->exec_addToTask($gai_sn);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
$opi_email = !empty($advisor_info->OPI_Email) ? $advisor_info->OPI_Email : ''; //lussie@chinahighlights.net
|
|
|
$opi_firstname = !empty($advisor_info->OPI_FirstName) ? $advisor_info->OPI_FirstName : !empty($advisor_info->OPI_Name) ? $advisor_info->OPI_Name : ''; //lussie
|
|
|
if ($opi_email===''
|
|
|
&& in_array(strtoupper($advisor_info->COLI_WebCode), array('CHTAPP-JA', 'JP'))
|
|
|
) {
|
|
|
$opi_email = 'contact@arachina.com';
|
|
|
$opi_firstname = 'contact@arachina.com';
|
|
|
}
|
|
|
//没有外联信息表示订单未分配
|
|
|
if (empty($opi_email) || empty($opi_firstname)) {
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail');
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
//添加邮件发送记录
|
|
|
//给外联发送通知邮件
|
|
|
if ($item->ALI_sent !== 'send') {
|
|
|
$fromName = 'Alipay';
|
|
|
$fromEmail = '';
|
|
|
$toName = !empty($opi_firstname) ? $opi_firstname : '';
|
|
|
$toEmail = !empty($opi_email) ? $opi_email : '';
|
|
|
$subject = $orderid_info->orderid . '_' . $orderid_info->ordertype . ' / ' . $item->ALI_orderAmount . $item->ALI_currencyCode . ' / ' . $fromName;
|
|
|
$body = $this->load->view('alipay_receipt_mail', $item, true);
|
|
|
$M_RelatedInfo = $item->ALI_sn;
|
|
|
$M_AddTime = $item->ALI_completeTime;
|
|
|
$M_State = 0;
|
|
|
$this->Alipay_model->save_automail($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_RelatedInfo, $M_State, $M_AddTime, 'Alipay note');
|
|
|
//添加邮件发送记录 end
|
|
|
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'send');
|
|
|
}
|
|
|
$int++;
|
|
|
}
|
|
|
// 批量结果
|
|
|
if (empty($pn_txn_id)) {
|
|
|
echo "done. recorde count:".$int;
|
|
|
}
|
|
|
async_curl($this->config->item('send_notify_url', 'wxpay'));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
/*!
|
|
|
* 退款处理
|
|
|
* @date 2019-05-09
|
|
|
* * TODO 线下收款之后产生的退款没有通知
|
|
|
*/
|
|
|
public function send_refund($item, $old_ssje=null, $handpick)
|
|
|
{
|
|
|
// raw
|
|
|
$raw = json_decode($item->ALI_memo);
|
|
|
$parent_order = $raw->out_trade_no;
|
|
|
$parent_trade_no = $raw->trade_no;
|
|
|
$parent_note = $this->Alipay_note_model->note($parent_trade_no);
|
|
|
// APP 组的退款查不到原始收款记录 todo
|
|
|
if (empty($parent_note) ) { # && true === $this->Alipay_note_model->if_APP_order($parent_order) ) {
|
|
|
$parent_note = $item;
|
|
|
// 补充字段
|
|
|
// $parent_note->IPL_orderId = $parent_order . '_B';
|
|
|
// $parent_note->IPL_currencyCode = $parent_payment->currencyCode;
|
|
|
// $parent_note->IPL_payerName = strval("''");
|
|
|
// $parent_note->IPL_payerEmail = strval("''");
|
|
|
|
|
|
// $this->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail');
|
|
|
// return false;
|
|
|
}
|
|
|
//订单号
|
|
|
$orderid_info = analysis_orderid($parent_note->ALI_orderId);
|
|
|
$orderid_info = json_decode($orderid_info);
|
|
|
//根据订单号查找外联信息
|
|
|
$advisor_info = $this->Alipay_model->get_order($orderid_info->orderid, false, $orderid_info->ordertype, $handpick);
|
|
|
//查不到订单信息
|
|
|
if (empty($advisor_info)) {
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail');
|
|
|
return false;
|
|
|
}
|
|
|
//更新正确的订单信息到记录中,以这个为主
|
|
|
$this->Alipay_note_model->set_invoice($item->ALI_dealId, $orderid_info->orderid . '_' . $orderid_info->ordertype);
|
|
|
|
|
|
//添加支付信息入库
|
|
|
//没有分配订单之前先添加付款记录,这个过程可能会执行多次,必须在添加记录前查找是否有数据
|
|
|
if ( ! empty($orderid_info)) {
|
|
|
$currencyCode = str_replace("CNY", "RMB", trim(mb_strtoupper($item->ALI_currencyCode)));
|
|
|
$currencyCode = mb_strtoupper(trim($currencyCode));
|
|
|
$ssje = $this->Alipay_model->get_ssje($item->ALI_orderAmount, $currencyCode, '15015');
|
|
|
$ssje = $old_ssje===null ? $ssje : $old_ssje;
|
|
|
$USD_amount = $this->Alipay_model->get_USD($item->ALI_orderAmount, $currencyCode);
|
|
|
//更新还没有填的客邮和交易号de收款记录(商务订单)
|
|
|
if (isset($advisor_info->order_type) && $advisor_info->order_type == 0) {
|
|
|
$ht_memo = '(自动)退款号:' . $item->ALI_dealId . "\n. ";
|
|
|
$ht_memo .= '原收款号:' . $parent_trade_no;
|
|
|
$GAI_COLI_SN = isset($advisor_info->COLI_SN) ? $advisor_info->COLI_SN : 0;
|
|
|
//CHTAPP订单添加记录前判断是否有记录,以前的APP版本没有交易号,只能拿金额来判断
|
|
|
if (substr($advisor_info->COLI_WebCode, 0, 6) == 'CHTAPP' && strstr($advisor_info->COLI_WebCode, "-") != '-biz') {//只判断前6位字符,CHTAPP-fr CHTAPP-jp等各语种都属于APP订单
|
|
|
$this->Alipay_model->add_account_info_forAPP(
|
|
|
$GAI_COLI_SN,
|
|
|
$advisor_info->COLI_ID,
|
|
|
$item->ALI_orderAmount,
|
|
|
$item->ALI_completeTime,
|
|
|
$currencyCode,
|
|
|
$ssje,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_acquiringTime,
|
|
|
NULL,
|
|
|
NULL,
|
|
|
$item->ALI_dealId,
|
|
|
$ht_memo);
|
|
|
$this->Alipay_model->insert_biz_order_log($GAI_COLI_SN, 'Refunded');
|
|
|
} else {
|
|
|
if (false == $this->Alipay_model->if_biz_gai_exists($item->ALI_dealId) ) {
|
|
|
$this->Alipay_model->insert_biz_order_log($GAI_COLI_SN, 'Refunded');
|
|
|
}
|
|
|
$this->Alipay_model->add_account_info(
|
|
|
$GAI_COLI_SN,
|
|
|
$advisor_info->COLI_ID,
|
|
|
$item->ALI_orderAmount,
|
|
|
$item->ALI_completeTime,
|
|
|
$currencyCode,
|
|
|
$USD_amount,
|
|
|
$ssje,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_acquiringTime,
|
|
|
NULL,
|
|
|
NULL,
|
|
|
$item->ALI_dealId,
|
|
|
$ht_memo);
|
|
|
}
|
|
|
}
|
|
|
//更新还没有填的客邮和交易号的收款记录(传统订单)
|
|
|
elseif (isset($advisor_info->order_type) && $advisor_info->order_type == 1) {
|
|
|
$ht_memo = '(自动)退款号:' . $item->ALI_dealId . "\n. ";
|
|
|
$ht_memo .= '原交易号:' . $parent_trade_no;
|
|
|
$GAI_COLI_SN = isset($advisor_info->COLI_SN) ? $advisor_info->COLI_SN : 0;
|
|
|
$gai_sn = $this->Alipay_model->add_tour_account_info(
|
|
|
$GAI_COLI_SN,
|
|
|
$item->ALI_orderAmount,
|
|
|
$item->ALI_completeTime,
|
|
|
$currencyCode,
|
|
|
$ssje,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_completeTime,
|
|
|
$item->ALI_acquiringTime,
|
|
|
NULL,
|
|
|
NULL,
|
|
|
$item->ALI_dealId,
|
|
|
$ht_memo);
|
|
|
//添加汉特的订单提醒
|
|
|
$this->Alipay_model->update_coli_introduction($GAI_COLI_SN, '已退款 ' . ($currencyCode) . $item->ALI_orderAmount);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$opi_email = !empty($advisor_info->OPI_Email) ? $advisor_info->OPI_Email : ''; //lussie@chinahighlights.net
|
|
|
$opi_firstname = !empty($advisor_info->OPI_FirstName) ? $advisor_info->OPI_FirstName : !empty($advisor_info->OPI_Name) ? $advisor_info->OPI_Name : ''; //lussie
|
|
|
|
|
|
//没有外联信息表示订单未分配
|
|
|
if (empty($opi_email) || empty($opi_firstname)) {
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'sendfail');
|
|
|
return false;
|
|
|
}
|
|
|
$send_email = ($item->ALI_completeTime>date('Y-m-d', strtotime("-1 day")));
|
|
|
//添加邮件发送记录
|
|
|
// if (true) { // test
|
|
|
if ($send_email===true && $item->ALI_sent !== 'send' && substr($item->ALI_sent, 0, 5) !== 'send-') {
|
|
|
// 客人邮件中的外联落款
|
|
|
// $web_code = 'cht'; // 默认cht
|
|
|
$web_lgc = 1;
|
|
|
$web_code = strtolower($advisor_info->COLI_WebCode);
|
|
|
$site_info = $this->config->item('site');
|
|
|
if (isset($site_info[$web_code])) {
|
|
|
$site_info = $site_info[$web_code];
|
|
|
$item->site = $site_info['site_url'];
|
|
|
$web_lgc = $site_info['site_lgc'];
|
|
|
}
|
|
|
$advisor_detail = $this->Alipay_model->get_advisor_detail($advisor_info->COLI_SN, $advisor_info->OPI_SN, $web_lgc);
|
|
|
$item->advisor_detail = $advisor_detail;
|
|
|
|
|
|
//给外联发送通知邮件
|
|
|
$fromName = !empty($parent_note->ALI_payerName) ? $parent_note->ALI_payerName : 'Alipay';
|
|
|
$fromEmail = !empty($parent_note->ALI_payerEmail) ? $parent_note->ALI_payerEmail : '';
|
|
|
$toName = !empty($opi_firstname) ? $opi_firstname : '';
|
|
|
$toEmail = !empty($opi_email) ? $opi_email : '';
|
|
|
$subject = $orderid_info->orderid . '_' . $orderid_info->ordertype . ' / ' . $item->ALI_orderAmount . $currencyCode . ' / ' . $fromName;
|
|
|
$body = $this->load->view('alipay_receipt_mail', $item, true);
|
|
|
$M_RelatedInfo = $item->ALI_sn;
|
|
|
$M_AddTime = $item->ALI_completeTime;
|
|
|
$M_State = 0;
|
|
|
$this->Alipay_model->save_automail($fromName, $fromEmail, $toName, $toEmail, $subject, $body, $M_RelatedInfo, $M_State, $M_AddTime, 'Alipay note');
|
|
|
// 通知客人, 客人邮箱
|
|
|
$customer_detail = $this->Alipay_model->get_customer_detail($advisor_info->COLI_SN, $orderid_info->ordertype);
|
|
|
$c_fromName = $advisor_detail->fullname;
|
|
|
$opi_email_list = explode(";", $advisor_detail->email); // 解析外联的邮件
|
|
|
$c_fromEmail = trim($opi_email_list[0]);
|
|
|
$c_toName = $customer_detail->fullname;
|
|
|
$c_toEmail = $customer_detail->email;
|
|
|
$c_subject = $currencyCode . " " . str_replace('-', '', $item->ALI_orderAmount) . " Refunded to your account, booking number " . $item->ALI_orderId;
|
|
|
// 修改一些字段名, 为了和iPaylinks等用同一个邮件模板
|
|
|
$item->payer = $customer_detail->fullname;
|
|
|
$item->payer_email = $customer_detail->email;
|
|
|
$item->total_amount = $item->ALI_orderAmount;
|
|
|
$item->payment_date = $item->ALI_completeTime;
|
|
|
$item->payment_status = "Refunfed";
|
|
|
$item->currency = $currencyCode;
|
|
|
$item->invoice = $item->ALI_orderId;
|
|
|
$c_body = $this->load->view('refund_buyer', $item, true);
|
|
|
$c_M_RelatedInfo = $item->ALI_sn;
|
|
|
$c_M_AddTime = $item->ALI_completeTime;
|
|
|
$c_M_State = 0;
|
|
|
$this->Alipay_model->save_automail(
|
|
|
$c_fromName,
|
|
|
$c_fromEmail,
|
|
|
$c_toName,
|
|
|
$c_toEmail,
|
|
|
$c_subject,
|
|
|
$c_body,
|
|
|
$c_M_RelatedInfo,
|
|
|
$c_M_State,
|
|
|
$c_M_AddTime,
|
|
|
'ChinaHighlights refund receipt');
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'send-customer');
|
|
|
} else {
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'send');
|
|
|
}
|
|
|
//添加邮件发送记录 end
|
|
|
// 如果已做账, 标记需要通知财务send-to-finance, 通知时间用订单日志记录
|
|
|
// 更新是否需要发送财务
|
|
|
$refund_finance_day = 20;
|
|
|
$now_day = date('d');
|
|
|
$late_entry_date_set = $now_day<$refund_finance_day ? date('Y-m-01', strtotime("-1 month")) : date('Y-m-01');
|
|
|
if ($this->payment_model->if_finance_done($advisor_info->COLI_GRI_SN, $late_entry_date_set) === true) {
|
|
|
$this->Alipay_note_model->update_send($item->ALI_dealId, 'send-to-finance');
|
|
|
}
|
|
|
return ;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* alipay.trade.query (统一收单线下交易查询)
|
|
|
* @param $builder 业务参数,使用buildmodel中的对象生成。
|
|
|
* @return Array object $response 支付宝返回的信息
|
|
|
*/
|
|
|
function Query($builder){
|
|
|
$biz_content=$builder->getBizContent();
|
|
|
|
|
|
$request = new AlipayTradeQueryRequest();
|
|
|
$request->setBizContent ( $biz_content );
|
|
|
|
|
|
$response = $this->aopclientRequestExecute ($request);
|
|
|
$response = $response->alipay_trade_query_response;
|
|
|
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
|
public function query_pay($dealId=NULL,$orderId=NULL, $debug=false)
|
|
|
{
|
|
|
if ($dealId === NULL) {
|
|
|
$dealId = $this->input->get_post('dealid');
|
|
|
$dealId = trim($dealId);
|
|
|
}
|
|
|
$this->AlipayTradeQueryContentBuilder->setTradeNo($dealId);
|
|
|
if ($orderId) {
|
|
|
$this->AlipayTradeQueryContentBuilder->setOutTradeNo($orderId);
|
|
|
}
|
|
|
$response = $this->Query($this->AlipayTradeQueryContentBuilder);
|
|
|
if ($debug != false) {
|
|
|
return $this->output->set_content_type('application/json')->set_output(json_encode($response)); // test
|
|
|
}
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
|
/*!
|
|
|
* 查询退款
|
|
|
* @date 2019-04-24
|
|
|
* @param [type] $dealId 必须, 退款请求号.out_biz_no, 或原始交易订单号
|
|
|
* @param [type] $trade_no 必须, 原收款交易号, 和order_id不能同时为空
|
|
|
* @param [type] $order_id 必须, 原收款订单号, 和trade_no不能同时为空
|
|
|
*/
|
|
|
public function query_refund($dealId=NULL,$trade_no=NULL, $order_id=NULL)
|
|
|
{
|
|
|
$this->AlipayTradeQueryContentBuilder->setOutRequestNo($dealId);
|
|
|
$this->AlipayTradeQueryContentBuilder->setTradeNo($trade_no);
|
|
|
$this->AlipayTradeQueryContentBuilder->setOutTradeNo($order_id);
|
|
|
|
|
|
$biz_content=$this->AlipayTradeQueryContentBuilder->getBizContent();
|
|
|
$request = new AlipayTradeFastpayRefundQueryRequest();
|
|
|
$request->setBizContent ( $biz_content );
|
|
|
|
|
|
$response = $this->aopclientRequestExecute ($request);
|
|
|
// $response = $response->alipay_trade_fastpay_refund_query_response;
|
|
|
return $this->output->set_content_type('application/json')->set_output(json_encode($response));
|
|
|
}
|
|
|
|
|
|
/*!
|
|
|
* 对账单
|
|
|
* 流程:
|
|
|
* * 获取对账单下载地址
|
|
|
* * 下载对账单
|
|
|
* * 解压,获取明细表
|
|
|
* * 分析明细表,入库
|
|
|
* @author LYT <lyt@hainatravel.com>
|
|
|
* @date 2017-10-10
|
|
|
* @param [type] $date 按天yyyy-MM-dd;按月yyyy-MM
|
|
|
*/
|
|
|
public function get_billfile($date=NULL)
|
|
|
{
|
|
|
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\\' . $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']
|
|
|
,null,true
|
|
|
);
|
|
|
}
|
|
|
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"// 备注
|
|
|
);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 验签方法
|
|
|
* @param $arr 验签支付宝返回的信息,使用支付宝公钥。
|
|
|
* @return boolean
|
|
|
*/
|
|
|
function check($arr){
|
|
|
$ret = new ArrayObject();
|
|
|
$ret->check = false;
|
|
|
$ret->data = NULL;
|
|
|
|
|
|
$ret->data = $arr_obj = (object) $arr;
|
|
|
|
|
|
$aop = new AopClient();
|
|
|
$aop->alipayrsaPublicKey = $this->alipay_public_key;
|
|
|
$ret->check = $result = $aop->rsaCheckV1($arr, $this->alipay_public_key, $this->signtype);
|
|
|
if ($result === false) {
|
|
|
log_message('error','Alipay sign ERROR ! orderId:'.$arr_obj->out_trade_no.'; dealId:'.$arr_obj->trade_no . "; Original return:".json_encode($arr)."; ");
|
|
|
return $ret;
|
|
|
}
|
|
|
|
|
|
// cht
|
|
|
// 1.app_id
|
|
|
if ($arr['app_id'] !== $this->appid) {
|
|
|
log_message('error','Alipay ERROR APPID verify failed '.$arr_obj->app_id." !== ".$this->appid."; orderId:".$arr_obj->out_trade_no."; dealId:".$arr_obj->trade_no);
|
|
|
return $ret;
|
|
|
}
|
|
|
// 2.seller_id
|
|
|
if ($arr['seller_id'] !== $this->seller_id) {
|
|
|
log_message('error','Alipay ERROR SELLERID verify failed '.$arr_obj->seller_id." !== ".$this->seller_id."; orderId:".$arr_obj->out_trade_no."; dealId:".$arr_obj->trade_no);
|
|
|
return $ret;
|
|
|
}
|
|
|
// 3.pay status
|
|
|
if (strcmp(strval($arr_obj->trade_status), "TRADE_SUCCESS")) {
|
|
|
log_message('error',"Alipay payment failed! error code:".$arr_obj->trade_status."; result Msg: ".$arr_obj->trade_status.'; orderId:'.$arr_obj->out_trade_no.'; dealId:'.$arr_obj->trade_no."; ");
|
|
|
return $ret;
|
|
|
}
|
|
|
$ret->check = true;
|
|
|
return $ret;
|
|
|
}
|
|
|
|
|
|
/** *利用google api生成二维码图片
|
|
|
* $content:二维码内容参数
|
|
|
* $size:生成二维码的尺寸,宽度和高度的值
|
|
|
* $lev:可选参数,纠错等级
|
|
|
* $margin:生成的二维码离边框的距离
|
|
|
*/
|
|
|
function create_erweima($content, $size = '200', $lev = 'L', $margin= '0') {
|
|
|
$content = urlencode($content);
|
|
|
$image = '<img src="http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$lev.'|'.$margin.'&chl='.$content.'" widht="'.$size.'" height="'.$size.'" />';
|
|
|
// http://chart.apis.google.com/chart?chs=200x200&cht=qr&chl=http://www.chinahighlights.com/
|
|
|
return $image;
|
|
|
}
|
|
|
|
|
|
public function note_list()
|
|
|
{
|
|
|
$this->permission->is_admin(true);
|
|
|
$data = array();
|
|
|
$data["paytext"] = $this->payment_status();
|
|
|
$data["keywords"] = $this->input->get_post("keywords");
|
|
|
$data["date"] = $this->input->get_post("date");
|
|
|
empty($data['date']) ? $data['date'] = date('Y-m-d') : false;
|
|
|
if (!empty($data['keywords'])) {
|
|
|
$data['notelist'] = $this->Alipay_note_model->search_key($data['keywords']);
|
|
|
/** 手动查询通过收钱码的收款, 必须输入交易号 */
|
|
|
if (empty($data['notelist'])) {
|
|
|
$query_pay = $this->query_pay($data["keywords"]);
|
|
|
if ( ! empty($query_pay) && strval($query_pay->code)==="10000"
|
|
|
&& in_array(strval($query_pay->trade_status), array("TRADE_SUCCESS", "TRADE_FINISHED")) ) {
|
|
|
$pay_type = $query_pay->total_amount>0 ? "pay" : "refund";
|
|
|
$new_record = $this->Alipay_note_model->save_alipay(
|
|
|
strval($query_pay->trade_no)
|
|
|
,strval($query_pay->out_trade_no)
|
|
|
,"CNY"
|
|
|
,strval($query_pay->total_amount)
|
|
|
,NULL
|
|
|
,1 // 此处1表示通过收款码进来的, 不收手续费, 后续录入时分别处理
|
|
|
,strval($query_pay->send_pay_date)
|
|
|
,strval($query_pay->send_pay_date)
|
|
|
,json_encode($query_pay,JSON_UNESCAPED_UNICODE)
|
|
|
,$pay_type
|
|
|
,NULL
|
|
|
,strval($query_pay->trade_status)
|
|
|
,NULL
|
|
|
,strval($query_pay->buyer_logon_id)
|
|
|
);
|
|
|
$data['notelist'][] = $new_record;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
$data['notelist'] = $this->Alipay_note_model->search_date($data['date']);
|
|
|
}
|
|
|
$this->load->view("alipay_list",$data);
|
|
|
return;
|
|
|
}
|
|
|
//失败记录列表
|
|
|
public function note_faillist() {
|
|
|
$this->permission->is_admin(true);
|
|
|
$data = array();
|
|
|
$data["paytext"] = $this->payment_status();
|
|
|
//有关键词则不限制日期
|
|
|
$data['search_key'] = $this->input->post('keywords');
|
|
|
$data['date'] = $this->input->get_post('date');
|
|
|
empty($data['date']) ? $data['date'] = date('Y-m-d') : false;
|
|
|
$data['notelist'] = $this->Alipay_note_model->failnote(100);
|
|
|
$this->load->view("alipay_list",$data);
|
|
|
}
|
|
|
//用于HT--交易详情
|
|
|
public function receipt($pm_transaction_id) {
|
|
|
if ( ! $pm_transaction_id) {
|
|
|
return false;
|
|
|
}
|
|
|
$data = array();
|
|
|
$data = $this->Alipay_note_model->note($pm_transaction_id);
|
|
|
$this->load->view('alipay_receipt_mail', $data);
|
|
|
}
|
|
|
|
|
|
//获取note详情,以便后续修改各项数据
|
|
|
public function note_modal($pn_txn_id = false, $pn_invoice = false ,$notice_time = false) {
|
|
|
// $this->permission->is_admin(true);
|
|
|
// $data = array();
|
|
|
// $data['IPL_orderId'] = $pn_invoice;
|
|
|
// if (!empty($pn_txn_id)) {
|
|
|
// $data['note'] = $this->Alipay_note_model->note($pn_txn_id);
|
|
|
// if (!empty($data['note'])) {
|
|
|
// if (!empty($pn_invoice)) {
|
|
|
// $orderid_info = analysis_orderid($pn_invoice);
|
|
|
// } else {
|
|
|
// $orderid_info = analysis_orderid($data['note']->IPL_orderId);
|
|
|
// }
|
|
|
// if (!empty($orderid_info)) {
|
|
|
// $orderid_info = json_decode($orderid_info);
|
|
|
// $data['order_info'] = $this->Alipay_model->get_order($orderid_info->orderid, true, $orderid_info->ordertype);
|
|
|
// }
|
|
|
// $data["paytext"] = $this->payment_status();
|
|
|
// echo json_encode($this->load->view('alipay_note_setting', $data, true));
|
|
|
// return true;
|
|
|
// }
|
|
|
// }
|
|
|
// echo json_encode('没找到数据!');
|
|
|
// return;
|
|
|
|
|
|
$this->permission->is_admin(true);
|
|
|
$data = array();
|
|
|
$data['note'] = $this->Alipay_note_model->note($pn_txn_id);
|
|
|
$orderid_info = analysis_orderid($data['note']->ALI_orderId);
|
|
|
if (!empty($orderid_info)) {
|
|
|
$orderid_info = json_decode($orderid_info);
|
|
|
if ($orderid_info->ordertype === 'T') {
|
|
|
$data['gai_info'] = $this->Alipay_model->get_money_t($pn_txn_id);
|
|
|
} elseif ($orderid_info->ordertype === 'B') {
|
|
|
$data['gai_info'] = $this->Alipay_model->get_money_b($pn_txn_id);
|
|
|
}
|
|
|
}
|
|
|
$data['old_order'] = $data['note']->ALI_orderId;
|
|
|
$data['new_order'] = $pn_invoice;
|
|
|
$data['order_info'] = null;
|
|
|
if ($pn_invoice !== null) {
|
|
|
$neworder_id = analysis_orderid($pn_invoice);
|
|
|
$neworder_id = json_decode($neworder_id);
|
|
|
if ( ! empty($neworder_id)) {
|
|
|
$data['order_info'] = $this->Alipay_model->get_order($neworder_id->orderid, true, $neworder_id->ordertype, true);
|
|
|
}
|
|
|
}
|
|
|
$data["paytext"] = $this->payment_status();
|
|
|
echo json_encode($this->load->view('alipay_note_setting', $data, true));
|
|
|
}
|
|
|
|
|
|
public function note_order_modal($old_order,$pn_invoice = false ,$notice_time = false) {
|
|
|
$data = array();
|
|
|
$data['IPL_orderId'] = $pn_invoice;
|
|
|
if (!empty($pn_invoice)) {
|
|
|
$data['note'] = $this->Alipay_note_model->note_order($old_order,$notice_time);
|
|
|
if (!empty($data['note'])) {
|
|
|
if (!empty($pn_invoice)) {
|
|
|
$orderid_info = analysis_orderid($pn_invoice);
|
|
|
} else {
|
|
|
$orderid_info = analysis_orderid($data['note']->IPL_orderId);
|
|
|
}
|
|
|
if (!empty($orderid_info)) {
|
|
|
$orderid_info = json_decode($orderid_info);
|
|
|
$data['order_info'] = $this->Alipay_model->get_order($orderid_info->orderid, true, $orderid_info->ordertype, true);
|
|
|
}
|
|
|
$data["paytext"] = $this->payment_status();
|
|
|
echo json_encode($this->load->view('alipay_note_setting', $data, true));
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
echo json_encode('没找到数据!');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//关闭note通知,用于手动处理通知后
|
|
|
public function close_note($pn_txn_id) {
|
|
|
$data = array();
|
|
|
$data['note'] = $this->Alipay_note_model->note($pn_txn_id);
|
|
|
if (!empty($data['note'])) {
|
|
|
$this->Alipay_note_model->update_send($pn_txn_id, 'send');
|
|
|
echo json_encode('通知已经关闭!');
|
|
|
return true;
|
|
|
}
|
|
|
echo json_encode('没找到数据!');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//修改订单名
|
|
|
public function note_modal_save() {
|
|
|
$data = array();
|
|
|
$old_ssje = null;
|
|
|
|
|
|
$pn_txn_id = $this->input->post('pn_txn_id');
|
|
|
$pn_invoice = $this->input->post('pn_invoice');
|
|
|
|
|
|
$data['note'] = $this->Alipay_note_model->note($pn_txn_id);
|
|
|
$orderid_info = analysis_orderid($data['note']->ALI_orderId);
|
|
|
// if (empty($orderid_info) && $data['note']->ALI_payType == 'refund'
|
|
|
// && true === $this->Alipay_model->if_APP_order($data['note']->ALI_orderId)
|
|
|
// ) {
|
|
|
// // APP 组的退款订单是没有后缀的
|
|
|
// $orderid_info = analysis_orderid($data['note']->ALI_orderId . '_B');
|
|
|
// }
|
|
|
if (!empty($orderid_info)) {
|
|
|
$orderid_info = json_decode($orderid_info);
|
|
|
if ($orderid_info->ordertype === 'T') {
|
|
|
$data['gai_info'] = $this->Alipay_model->get_money_t(substr($pn_txn_id,0,30));
|
|
|
if ( ! empty($data['gai_info'])) {
|
|
|
$old_ssje = $data['gai_info'][0]->GAI_SSJE;
|
|
|
$this->Alipay_model->delete_money_t(substr($pn_txn_id,0,30));
|
|
|
}
|
|
|
} elseif ($orderid_info->ordertype === 'B' || $orderid_info->ordertype === 'A') {
|
|
|
$data['gai_info'] = $this->Alipay_model->get_money_b(substr($pn_txn_id,0,30));
|
|
|
if ( ! empty($data['gai_info'])) {
|
|
|
$old_ssje = $data['gai_info'][0]->GAI_SSJE;
|
|
|
$this->Alipay_model->delete_money_b(substr($pn_txn_id,0,30));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (!empty($pn_txn_id) && !empty($pn_invoice)) {
|
|
|
$orderid_info = analysis_orderid($pn_invoice);
|
|
|
if (!empty($orderid_info)) {
|
|
|
$orderid_info = json_decode($orderid_info);
|
|
|
$advisor_info = $this->Alipay_model->get_order($orderid_info->orderid, false, $orderid_info->ordertype, true);
|
|
|
if (!empty($advisor_info)) {
|
|
|
$this->Alipay_note_model->set_invoice($pn_txn_id, $pn_invoice);
|
|
|
$this->send_alipay($pn_txn_id, $old_ssje);
|
|
|
echo json_encode('修改成功!');
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
echo json_encode('没找到数据!');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
public function payment_status()
|
|
|
{
|
|
|
return array(
|
|
|
"WAIT_BUYER_PAY" => "Pending",
|
|
|
"TRADE_SUCCESS" => "Payment success",
|
|
|
"TRADE_CLOSED" => "Payment closed",
|
|
|
"TRADE_FINISHED" => "Payment success"
|
|
|
);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|