feat: 支付宝退款: 异步更新状态到财务系统

webht/payment
Lei OT 7 months ago
parent def549e8ac
commit c3b265a58f

@ -53,7 +53,9 @@ class AlipayTradeService extends CI_Controller
$this->load->model('Alipay_note_model');
$this->load->model('Alipay_model');
$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');
@ -623,7 +625,8 @@ class AlipayTradeService extends CI_Controller
return;
}
if (($item->ALI_resultMsg) === 'pending') {
// todo: 查询退款结果
// 查询退款结果
$this->query_refund_result($item);
return;
}
// raw
@ -1453,7 +1456,7 @@ class AlipayTradeService extends CI_Controller
}
// refund_reason len 256
$this->AlipayTradeRefundContentBuilder->setRefundReason($refund_payload['reason']);
// * out_request_no 退款请求号。 标识一次退款请求
// * out_request_no 退款请求号。 标识一次退款请求 *refund_id*
$generateId = $refundRequestId ? $refundRequestId : str_replace('-', '', getGuid());
$this->AlipayTradeRefundContentBuilder->setOutRequestNo($generateId);
// [-] query_options 同步需要额外返回的信息字段 ["refund_detail_item_list"]
@ -1463,6 +1466,9 @@ class AlipayTradeService extends CI_Controller
$refund_res = $response;
$refund_res->id = $generateId;
$refund_res->custom_id = $refund_payload['custom_id'];
$refund_res->trade_no = $dealId;
$refund_res->out_trade_no = $paymentOrder;
$refund_res->merchant_account = $dealAccount ? $dealAccount : 'cht';
// $ret_status_mapped = array(
// "Y" => "1", // COMPLETED
// "N" => "3", // FAILED
@ -1470,7 +1476,7 @@ class AlipayTradeService extends CI_Controller
// // "CANCELLED" => "2", // todo:
// );
// $refund_res->status = $response->code === '10000' ? '1' : '3';
$refund_res->status = $response->code === '10000' && $response->fund_change === 'Y' ? '1' : $response->code === '10000' ? '4' : '3';
$refund_status = $response->code === '10000' && $response->fund_change === 'Y' ? '1' : $response->code === '10000' ? '4' : '3';
$refund_res->refund_payload = $refund_payload;
// log_message('error','test: ' . __METHOD__ . ': ' . __LINE__ . ' ' . PHP_EOL . var_export($response, 1));
// var_dump($response);
@ -1480,58 +1486,87 @@ class AlipayTradeService extends CI_Controller
// var_dump($generateId, $dealId, $paymentOrder);
$query_res = $this->query_refund($generateId, $dealId, $paymentOrder);
$refund_res->query_refund_result = $query_res;
$refund_res->status2 = isset($query_res->refund_status) && $query_res->refund_status === 'REFUND_SUCCESS' ? '1' : '3';
$query_refund_status = isset($query_res->refund_status) && $query_res->refund_status === 'REFUND_SUCCESS' ? '1' : '3';
$refund_res->status = $query_refund_status === '1' ? $query_refund_status : $refund_status;
$res['result'] = $refund_res;
// if ($refund_res->status !== '3') {
if ($refund_res->status2 === '1') {
$this->Alipay_note_model->save_alipay(
strval($generateId) // $ALI_dealId,
,strval($paymentOrder) // $ALI_orderId,
,"CNY" // $ALI_currencyCode,
,"-" . strval($query_res->refund_amount) // $ALI_orderAmount,
,NULL // $ALI_payAmount,
,NULL // $ALI_stateCode,
,date('Y-m-d H:i:s') // $ALI_acquiringTime,
,date('Y-m-d H:i:s') // $ALI_completeTime,
,json_encode($query_res,JSON_UNESCAPED_UNICODE) // $ALI_memo,
,'refund' // $ALI_payType,
,NULL // $ALI_resultCode=null,
,$refund_res->status2 === '1' ? 'TRADE_SUCCESS' : 'pending' // $ALI_resultMsg=null,
,NULL // $ALI_payerName=null,
,NULL // $ALI_payerEmail=NULL,
// $check_exists=false
);
if ($refund_status !== '3') {
// if ($query_refund_status === '1') {
$this->Alipay_note_model->save_alipay(
strval($generateId) // $ALI_dealId,
,strval($paymentOrder) // $ALI_orderId,
,"CNY" // $ALI_currencyCode,
,"-" . strval($query_res->refund_amount) // $ALI_orderAmount,
,NULL // $ALI_payAmount,
,NULL // $ALI_stateCode,
,date('Y-m-d H:i:s') // $ALI_acquiringTime,
,date('Y-m-d H:i:s') // $ALI_completeTime,
,json_encode($refund_res, JSON_UNESCAPED_UNICODE) // $ALI_memo,
,'refund' // $ALI_payType,
,NULL // $ALI_resultCode=null,
,$query_refund_status === '1' ? 'TRADE_SUCCESS' : 'pending' // $ALI_resultMsg=null,
,NULL // $ALI_payerName=null,
,$refund_res->buyer_logon_id // $ALI_payerEmail=NULL,
// $check_exists=false
);
// } else {
// }
}
return $this->output->set_content_type('application/json')->set_output(json_encode($res));
}
public function query_refund_result()
/**
* private
* 异步查询退款结果
* * 更新交易记录的状态
* * 更新财务系统状态
*/
private function query_refund_result($item)
{
// $res = array(
// "errcode" => "0",
// "errmsg" => "",
// "result" => new stdClass(),
// );
$ready_to_financesystem = new stdClass();
// if (empty($item)) { // debug:
// $item = new stdClass();
// $item->ALI_resultMsg = '';
// $item->ALI_dealId = '-1';
// $item->ALI_memo = json_encode(array(
// "id" => "2F179741CB174200G-qqs",
// "custom_id" => "75",
// "trade_no" => "-1",
// "out_trade_no" => "-1",
// "merchant_account" => "cht",
// "status" => "4",
// ), JSON_UNESCAPED_UNICODE);
// }
$raw = json_decode($item->ALI_memo);
$merchant_account = $raw->merchant_account;
$this->set_merchant($merchant_account);
$request_no = $this->input->get_post('request_no');
$trade_no = $this->input->get_post('trade_no');
$order_id = $this->input->get_post('order_id');
$request_no = $raw->id;
$trade_no = $raw->trade_no;
$order_id = $raw->out_trade_no;
$query_res = $this->query_refund($request_no, $trade_no, $order_id);
$status2 = isset($query_res->refund_status) && $query_res->refund_status === 'REFUND_SUCCESS' ? '1' : '3';
$trade_status = isset($query_res->refund_status) && $query_res->refund_status === 'REFUND_SUCCESS' ? 'TRADE_SUCCESS' : $item->ALI_resultMsg;
$query_refund_status = isset($query_res->refund_status) && $query_res->refund_status === 'REFUND_SUCCESS' ? '1' : '3';
$note_status = $query_refund_status === '1' ? '' : 'sendfail';
$originRow = $this->Alipay_note_model->note($request_no);
// $originRows = $this->online_note->get_note_where(['OPN_transactionId' => ['=', $dealId]]);
$ready_to_financesystem = $query_res;
$ready_to_financesystem->status = $query_res->code === '10000' ? $query_refund_status : $raw->status;
$ready_to_financesystem->id = $request_no;
$ready_to_financesystem->custom_id = $raw->custom_id;
// $res['errcode'] = $query_res->code === '10000' ? '0' : '1';
// $res['result'] = $query_res;
// return $this->output->set_content_type('application/json')->set_output(json_encode($res));
}
$this->Alipay_note_model->update_note(['ALI_dealId' => $request_no], ['ALI_resultMsg' => $trade_status]);
$this->Alipay_note_model->update_send($item->ALI_dealId, $note_status);
public function query_refund_queue()
{
// call_api('https://p9axztuwd7x8a7.mycht.cn/Service_BaseInfoWeb/alipayrefund_status_set', $ready_to_financesystem, 'POST');
call_api('http://202.103.68.144:890/Service_BaseInfoWeb/alipayrefund_status_set', $ready_to_financesystem, 'POST');
return;
}
}

@ -595,3 +595,49 @@ function getGuid()
// @codingStandardsIgnoreEnd
}
function call_api($url, $body = false, $method = 'GET')
{
$header = array(
"Content-Type:application/json",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_SSLVERSION, 6);
// CURL_SSLVERSION_TLSv1_2 Available since PHP 5.5.19 and 5.6.3
$param_str = "";
// $param_str = $this->toJSON($body);
$param_str = to_json($body);
if ($method == 'POST')
curl_setopt($ch, CURLOPT_POST, true);
// if (is_array($body) && 0 < count($body)) {
if ((is_array($body) && !empty($body)) || (is_object($body) && get_class($body) === 'stdClass' && !empty((array) $body))) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $param_str);
}
$reponse = curl_exec($ch);
$reponse_obj = json_decode($reponse);
if (curl_errno($ch)) {
log_message('error', "curl REST API error code: " . curl_error($ch) . "; $method: ".$param_str);
} else {
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode) {
log_message('error', "curl REST API Request http Status Code: ".$httpStatusCode);
if (is_object($reponse_obj)) {
$reponse_obj->httpStatusCode = $httpStatusCode;
}
}
}
curl_close($ch);
return ($reponse_obj);
// return json_decode($reponse);
}

@ -247,5 +247,11 @@ class Alipay_note_model extends CI_Model {
return $this->INFO->query($sql, array($payer,$dealId));
}
public function update_note($where, $column)
{
$update_str = $this->INFO->update_string('AlipayLog', $column, $where);
$this->INFO->query($update_str);
return TRUE;
}
}

Loading…
Cancel
Save