1225 行
59 KiB
PHP
可执行文件
1225 行
59 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace Pay\Controller;
|
||
|
||
use Think\Controller;
|
||
//[1.2.7]
|
||
class WxPayController extends BaseController
|
||
{
|
||
public function toWxPay($orderid, $out_trade_no, $code, $storeid, $money, $adminid, $merchantid, $orderInfo)
|
||
{
|
||
// print_r($orderInfo);exit();
|
||
$config = D('Payset')->getFindConfig($storeid);//获取商户微信配置信息
|
||
if (empty($config)) {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '商家支付参数未配置';
|
||
$this->ajaxReturn($data);
|
||
}
|
||
if ($config["status"] == 1) {
|
||
$post['appid'] = $config['wx_appid'];
|
||
$post['mch_id'] = $config['wx_mch_id'];
|
||
$post['body'] = $config['name'] . '-' . $config['storetag'];
|
||
$post['out_trade_no'] = $out_trade_no;
|
||
$post['total_fee'] = $money;
|
||
$post['auth_code'] = $code;
|
||
$post['key'] = $config['wx_secret_key'];
|
||
} else if ($config["status"] == 3) {
|
||
//查询合作伙伴配置
|
||
$id = $config["merchantid"];
|
||
$partnerid = M("retail_merchant")->where(array("id" => $id))->getField("partnerid");
|
||
$cache = M("systemconfig")->where(array("partnerid" => $partnerid))->find();
|
||
$post['appid'] = $cache['wx_appid'];
|
||
$post['mch_id'] = $cache['mch_id'];
|
||
$post['body'] = $config['name'] . '-' . $config['storetag'];
|
||
$post['out_trade_no'] = $out_trade_no;
|
||
$post['total_fee'] = $money;
|
||
$post['auth_code'] = $code;
|
||
$post['key'] = $cache['wx_key'];
|
||
$post['sub_mch_id'] = $config["wx_sub_mchid"];
|
||
if(!empty($config['wx_sub_appid'])){
|
||
$post['wx_sub_appid'] = $config['wx_sub_appid'];
|
||
}
|
||
file_put_contents("wxpay.txt", json_encode($post));
|
||
} else if ($config["status"] == 4) {
|
||
//新大陆支付
|
||
$pays['tradeNo'] = $out_trade_no;
|
||
$pays['authCode'] = $code;
|
||
$pays['types'] = 2;
|
||
$pays['subject'] = '测试';
|
||
$pays['amount'] = $money;
|
||
$pays['total_amount'] = $money;
|
||
$data = A('Pay/PayContinent')->pay($pays, $merchantid, $adminid, $storeid, $orderInfo, $orderid);
|
||
}
|
||
$WxPaysdk = new \Util\Wx\WxMicroPay($post);
|
||
$result = $WxPaysdk->postWxMicro();
|
||
|
||
if (!empty($result) && $result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
|
||
//修改订单状态
|
||
$save ['trade_no'] = $result ['transaction_id'];
|
||
$save ['order_state'] = 'SUCCESS';
|
||
$time = substr($result['time_end'], 0, strlen($result['time_end']) - 10) . '-' . substr($result['time_end'], -10, 2) . '-' . substr($result['time_end'], -8, 2) . ' ' . substr($result['time_end'], -6, 2) . ':' . substr($result['time_end'], -4, 2) . ':' . substr($result['time_end'], -2, 2);
|
||
$save ['tradedate'] = strtotime($time);
|
||
$save ['extend_content'] = json_encode($result);
|
||
|
||
$PayOrderModel = D('PayOrder');
|
||
$update = $PayOrderModel->updateOrder(array('id' => $orderid), $save);//更新订单信息
|
||
if ($update) {
|
||
if ($config["status"] == 3) {
|
||
A('Api/Vip')->toMark($result['sub_openid'], $orderid, $money);
|
||
} else {
|
||
A('Api/Vip')->toMark($result['openid'], $orderid, $money);
|
||
}
|
||
file_put_contents('./orderid.txt', $orderid);
|
||
A("Sellapi/PayRefund")->payOvers(3, $adminid, $storeid, $merchantid, $orderInfo, $orderid);
|
||
$data['status'] = 1;
|
||
$data['msg'] = '支付成功';
|
||
$data['out_trade_no'] = $out_trade_no;
|
||
$data['mdate'] = $save ['tradedate'];
|
||
$data['pay_type'] = "微信";
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '付款成功,更新订单失败,请联系管理员1';
|
||
}
|
||
} else if (!empty($result) && $result['err_code'] == 'USERPAYING' && $result["return_code"] == "SUCCESS" && $result["return_msg"] == "OK") {//用户支付中
|
||
$this->getMicroPayState($orderid, $storeid, $adminid, $merchantid, $orderInfo);
|
||
} else if (!empty($result)) {
|
||
$data['status'] = 0;
|
||
$data['msg'] = $result['return_msg'];
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = "收款失败,请重试";
|
||
}
|
||
$this->ajaxReturn($data);
|
||
}
|
||
|
||
//反复请求用户长时间才输入密码的支付状态
|
||
protected function getMicroPayState($orderid, $storeid, $adminid, $merchantid, $orderInfo)
|
||
{
|
||
$PayOrderModel = D('PayOrder');
|
||
$map['id'] = $orderid;
|
||
$Order = $PayOrderModel->getFind($map);
|
||
$res = $this->searchWxOrder($Order['storeid'], $Order['out_trade_no']);
|
||
if ($res['trade_state'] == "USERPAYING" || $res['trade_code'] == "USERPAYING") {
|
||
//用户支付中
|
||
$time = get_runtime();
|
||
if ($time < 20) {
|
||
$this->getMicroPayState($orderid, $storeid, $adminid, $merchantid, $orderInfo);
|
||
} else {
|
||
//撤销订单
|
||
$this->canselOrder($Order['storeid'],$Order);
|
||
}
|
||
} else if ($res['trade_state'] == "SUCCESS" || $res['trade_code'] == "SUCCESS") {//支付成功修改订单
|
||
$save ['trade_no'] = $res ['transaction_id'];
|
||
$save ['order_state'] = 'SUCCESS';
|
||
$time = substr($res['time_end'], 0, strlen($res['time_end']) - 10) . '-' . substr($res['time_end'], -10, 2) . '-' . substr($res['time_end'], -8, 2) . ' ' . substr($res['time_end'], -6, 2) . ':' . substr($res['time_end'], -4, 2) . ':' . substr($res['time_end'], -2, 2);
|
||
$save ['tradedate'] = strtotime($time);
|
||
$save ['mdate'] = time();
|
||
$save ['extend_content'] = json_encode($res);
|
||
|
||
$update = $PayOrderModel->updateOrder($map, $save);//更新订单信息
|
||
if ($update) {
|
||
if (isset($res['sub_appid'])) {
|
||
A('Api/Vip')->toMark($res['sub_openid'], $orderid, $Order['trade_amount']);
|
||
} else {
|
||
A('Api/Vip')->toMark($res['openid'], $orderid, $Order['trade_amount']);
|
||
}
|
||
file_put_contents('./orderid2.txt', $orderid);
|
||
A("Sellapi/PayRefund")->payOvers(3, $adminid, $storeid, $merchantid, $orderInfo, $orderid);
|
||
$data['status'] = 1;
|
||
$data['msg'] = '收款成功';
|
||
$data['out_trade_no'] = $Order ['out_trade_no'];
|
||
$data['mdate'] = $save ['tradedate'];
|
||
$data['pay_type'] = "微信";
|
||
$this->ajaxReturn($data);
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '付款成功,更新订单失败,请联系管理员2';
|
||
$this->ajaxReturn($data);
|
||
}
|
||
|
||
} else {
|
||
$save ['order_state'] = 'REVOKED';
|
||
$save ['mdate'] = time();
|
||
$update = $PayOrderModel->updateOrder($map, $save);//更新订单信息
|
||
$data['status'] = 0;
|
||
$data['msg'] = '用户取消支付';
|
||
$data['res'] = $res;
|
||
$this->ajaxReturn($data);
|
||
}
|
||
}
|
||
|
||
// 查询订单信息
|
||
protected function searchWxOrder($storeid, $out_trade_no)
|
||
{
|
||
file_put_contents('wx_searchWxOrder.txt','out_trade_no: '.$out_trade_no."\r\n",FILE_APPEND);
|
||
$config = D('Payset')->getFindConfig($storeid);//获取商户微信配置信息
|
||
$order = M('retail_pay_order')->where(array('out_trade_no' => $out_trade_no, 'storeid' => $storeid))->find();
|
||
if ($order["m_pay_type"] == 1) {
|
||
$key = $config['wx_secret_key'];
|
||
$obj['appid'] = $config['wx_appid'];
|
||
$obj['mch_id'] = $config['wx_mch_id'];
|
||
} else if ($order["m_pay_type"] == 2) {
|
||
$partnerid = M("retail_merchant")->where(array("id" => $config["merchantid"]))->getField("partnerid");
|
||
$cache = M("systemconfig")->where(array("partnerid" => $partnerid))->find();
|
||
$key = $cache['wx_key'];
|
||
$obj['appid'] = $cache['wx_appid'];
|
||
$obj['mch_id'] = $cache['mch_id'];
|
||
if(!empty($config['wx_sub_appid'])){
|
||
$obj['sub_appid'] = $config['wx_sub_appid'];
|
||
}
|
||
$obj['sub_mch_id'] = $config['wx_sub_mchid'];
|
||
}
|
||
$WxMicroPay = new \Util\Wx\WxMicroPay(array('key' => $key));
|
||
$obj['out_trade_no'] = $out_trade_no;
|
||
$obj['nonce_str'] = $WxMicroPay->createNoncestr(32);
|
||
$obj['sign'] = $WxMicroPay->getSign($obj);
|
||
|
||
file_put_contents('./okok.txt', '2: ' . json_encode($obj) . "\r\n", FILE_APPEND);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/pay/orderquery';
|
||
$xml = $WxMicroPay->arrayToXml($obj);//转换为xml
|
||
$response = $WxMicroPay->postXmlCurl($xml, $url, 30);
|
||
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
file_put_contents("./2.txt", '3: '.json_encode($response) . "\r\n", FILE_APPEND);
|
||
file_put_contents("./2.txt", json_encode($obj) . "\r\n", FILE_APPEND);
|
||
$response['obj'] = $obj;
|
||
return $response;
|
||
}
|
||
|
||
// 撤销订单
|
||
protected function canselOrder($storeid, $Order)
|
||
{
|
||
$config = D('Payset')->getFindConfig($storeid);
|
||
if ($Order["m_pay_type"] == 1) {
|
||
$post['appid'] = $config['wx_appid'];
|
||
$post['mch_id'] = $config['wx_mch_id'];
|
||
$arr['apiclient_cert'] = $config['apiclient_cert'];
|
||
$arr['apiclient_key'] = $config['apiclient_key'];
|
||
$arr['key'] = $config['wx_secret_key'];
|
||
} else if ($config["m_pay_type"] == 2) {
|
||
//查询合作伙伴配置
|
||
$id = $config["merchantid"];
|
||
$partnerid = M("retail_merchant")->where(array("id" => $id))->getField("partnerid");
|
||
$cache = M("systemconfig")->where(array("partnerid" => $partnerid))->find();
|
||
$post['appid'] = $cache['wx_appid'];
|
||
$post['mch_id'] = $cache['mch_id'];
|
||
if(!empty($config['wx_sub_appid'])){
|
||
$post['sub_appid'] = $config['wx_sub_appid'];
|
||
}
|
||
$post['sub_mch_id'] = $config['wx_sub_mchid'];
|
||
$arr['apiclient_cert'] = $cache['wx_apiclient_cert'];
|
||
$arr['apiclient_key'] = $cache['wx_apiclient_key'];
|
||
$arr['key'] = $cache['wx_key'];
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '商户支付模式有误';
|
||
$this->ajaxReturn($data);
|
||
}
|
||
$WxMicroPay = new \Util\Wx\WxMicroPay($arr);
|
||
$post['out_trade_no'] = $Order['out_trade_no'];
|
||
$post['nonce_str'] = $WxMicroPay->createNoncestr(32);
|
||
$post['sign'] = $WxMicroPay->getSign($post);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/secapi/pay/reverse';
|
||
$xml = $WxMicroPay->arrayToXml($post);
|
||
$response = $WxMicroPay->postXmlCurlCancel($xml, $url, 30);
|
||
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
|
||
if($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS'){
|
||
$res = $this->searchWxOrder($storeid, $Order['out_trade_no']);
|
||
if($res['trade_state'] == "REVOKED" || $res['trade_code'] == "CLOSED"){
|
||
$save ['order_state'] = 'REVOKED';
|
||
$PayOrderModel = D('PayOrder');
|
||
$res = $PayOrderModel->updateOrder(array('out_trade_no' => $Order['out_trade_no']), $save);//更新订单信息
|
||
$data['status'] = 1;
|
||
$data['msg'] = '订单超时,并已撤销';
|
||
}else{
|
||
$time = get_runtime();
|
||
if ($time < 10) {
|
||
$this->canselOrder($storeid, $Order);
|
||
}else{
|
||
$data['responseS'] = $res;
|
||
$data['status'] = 0;
|
||
$data['msg'] = '订单超时,但撤销失败,请用户别支付';
|
||
}
|
||
}
|
||
}else{
|
||
$data['status'] = 0;
|
||
$data['msg'] = '订单超时,但撤销失败';
|
||
}
|
||
$this->ajaxReturn($data);
|
||
}
|
||
|
||
/*
|
||
* 10-30 之前的退款
|
||
*
|
||
* */
|
||
// 直连退款接口
|
||
public function refund($storeid, $trade_no, $itemAll)
|
||
{
|
||
$PayOrder = D('PayOrder');
|
||
$map['trade_no'] = $trade_no;
|
||
$Order = $PayOrder->getFind($map);
|
||
|
||
$config = D('Payset')->getFindConfig($storeid);
|
||
|
||
//以下为往微信发送的数据
|
||
$item1['apiclient_cert'] = $config['apiclient_cert'];
|
||
$item1['apiclient_key'] = $config['apiclient_key'];
|
||
$item1['key'] = $config['wx_secret_key'];
|
||
$WxMicroPay = new \Util\Wx\WxMicroPay($item1);
|
||
|
||
$obj['appid'] = $config['wx_appid'];
|
||
$obj['mch_id'] = $config['wx_mch_id'];
|
||
$obj['nonce_str'] = $WxMicroPay->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $itemAll['out_refund_no'];
|
||
$obj['total_fee'] = $Order['trade_amount'] * 100;
|
||
$obj['refund_fee'] = $itemAll['refund_money'] * 100;
|
||
$obj['sign'] = $WxMicroPay->getSign($obj);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
|
||
$xml = $WxMicroPay->arrayToXml($obj);//转换为xml
|
||
|
||
$response = $WxMicroPay->postXmlCurlCancel($xml, $url, 30);
|
||
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
$this->searchRefund($Order['id'], $obj['out_refund_no'], $response['refund_id'], $itemAll);
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = $response;
|
||
$this->ajaxReturn($data);
|
||
}
|
||
}
|
||
/*
|
||
* 10-30 之前的退款 查询
|
||
* */
|
||
// 退款查询
|
||
protected function searchRefund($orderid, $out_refund_no, $refund_id, $itemAll)
|
||
{
|
||
$PayOrder = D('PayOrder');
|
||
$Order = $PayOrder->getFind(array('id' => $orderid));
|
||
|
||
$config = D('Payset')->getFindConfig($Order['storeid']);
|
||
|
||
$item1['apiclient_cert'] = $config['apiclient_cert'];
|
||
$item1['apiclient_key'] = $config['apiclient_key'];
|
||
$item1['key'] = $config['wx_secret_key'];
|
||
$WxMicroPay = new \Util\Wx\WxMicroPay($item1);
|
||
|
||
$obj['appid'] = $config['wx_appid'];
|
||
$obj['mch_id'] = $config['wx_mch_id'];
|
||
$obj['nonce_str'] = $WxMicroPay->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $out_refund_no;
|
||
$obj['refund_id'] = $refund_id;
|
||
$obj['sign'] = $WxMicroPay->getSign($obj);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/pay/refundquery';
|
||
$xml = $WxMicroPay->arrayToXml($obj);
|
||
|
||
$response = $WxMicroPay->postXmlCurlCancel($xml, $url, 30);
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
switch ($response['refund_status_0']) {
|
||
case 'PROCESSING':
|
||
case 'SUCCESS':
|
||
$PayOrder = D('PayOrder');
|
||
$save['goods_info'] = json_encode($itemAll['original_order']);
|
||
$res = $PayOrder->updateOrder(array('trade_no' => $Order['trade_no']), $save);
|
||
if ($res) {
|
||
$time = time();
|
||
$item['pid'] = $Order['id'];
|
||
$item['order_type'] = 3;
|
||
$item['pay_type'] = $Order['pay_type'];
|
||
$item['trade_amount'] = $itemAll['refund_money'];
|
||
$item['storeid'] = $Order['storeid'];
|
||
$item['adminid'] = $itemAll['adminid'];
|
||
$item['order_state'] = 'REFUND';
|
||
$item['cdate'] = $time;
|
||
$item['extend_content'] = json_encode($response);
|
||
$item['goods_info'] = json_encode($itemAll['refund_goods_info']);
|
||
$item['remark'] = '退款成功';
|
||
$item['trade_no'] = $response['refund_id_0'];
|
||
$item['out_trade_no'] = $itemAll['out_refund_no'];
|
||
$addRefund = $PayOrder->addOrder($item);
|
||
A('Sellapi/Order')->refund(json_encode($itemAll['refund_goods_info']), $Order['storeid'], $Order['id'], $itemAll['adminid'], $Order['pay_type'] == 1 ? 3 : 4, $Order['id']);
|
||
if ($addRefund) {
|
||
$data['status'] = 1;
|
||
$data['msg'] = "退款成功";
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = "已提交退款申请<br>更新订单失败";
|
||
}
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = "已提交退款申请<br>更新订单失败";
|
||
}
|
||
$this->ajaxReturn($data);
|
||
break;
|
||
case 'REFUNDCLOSE':
|
||
//退款关闭
|
||
$data['status'] = 0;
|
||
$data['msg'] = '退款关闭';
|
||
$this->ajaxReturn($data);
|
||
break;
|
||
case 'CHANGE':
|
||
//退款异常-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,可前往商户平台(pay.weixin.qq.com)-交易中心,手动处理此笔退款。
|
||
$data['status'] = 0;
|
||
$data['msg'] = '退款异常,商户银行卡异常';
|
||
$this->ajaxReturn($data);
|
||
}
|
||
} else {
|
||
$this->searchRefund($Order, $out_refund_no, $refund_id, $itemAll);
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 10-30退款
|
||
* */
|
||
// 退款接口
|
||
public function oemrefund($storeid, $trade_no, $itemAll)
|
||
{
|
||
$config = D('Payset')->getFindConfig($storeid);//获取商户微信配置信息
|
||
if (empty($config)) {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '商家支付参数未配置';
|
||
$this->ajaxReturn($data);
|
||
}
|
||
if ($config["status"] == 1) {
|
||
// 普通模式退款 商家自己配置的支付
|
||
$this->ptrefund($storeid, $trade_no, $itemAll);
|
||
} else if ($config["status"] == 3) {
|
||
//查询合作伙伴配置 服务商模式支付退款
|
||
$this->fwsrefund($storeid, $trade_no, $itemAll);
|
||
// file_put_contents("wxpay.txt",json_encode($post));
|
||
} else if ($config["status"] == 4) {
|
||
//新大陆支付退款
|
||
A('Pay/PayContinent')->BarcodePay($trade_no, $config['merchantid'], 2, $itemAll);
|
||
}
|
||
}
|
||
/*
|
||
* 10-30
|
||
* */
|
||
// 普通模式退款 商家自己配置的支付直连退款接口
|
||
public function ptrefund($storeid, $trade_no, $itemAll)
|
||
{
|
||
$PayOrder = D('PayOrder');
|
||
$map['trade_no'] = $trade_no;
|
||
$Order = $PayOrder->getFind($map);
|
||
|
||
$config = D('Payset')->getFindConfig($storeid);
|
||
|
||
//以下为往微信发送的数据
|
||
$item1['apiclient_cert'] = $config['apiclient_cert'];
|
||
$item1['apiclient_key'] = $config['apiclient_key'];
|
||
$item1['key'] = $config['wx_secret_key'];
|
||
$WxMicroPay = new \Util\Wx\WxMicroPay($item1);
|
||
|
||
$obj['appid'] = $config['wx_appid'];
|
||
$obj['mch_id'] = $config['wx_mch_id'];
|
||
$obj['nonce_str'] = $WxMicroPay->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $itemAll['out_refund_no'];
|
||
$obj['total_fee'] = $Order['trade_amount'] * 100;
|
||
$obj['refund_fee'] = $itemAll['refund_money'] * 100;
|
||
$obj['sign'] = $WxMicroPay->getSign($obj);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
|
||
$xml = $WxMicroPay->arrayToXml($obj);//转换为xml
|
||
|
||
$response = $WxMicroPay->postXmlCurlCancel($xml, $url, 30);
|
||
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
$this->ptsearchRefund($Order['id'], $obj['out_refund_no'], $response['refund_id'], $itemAll, $config, $Order);
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = $response;
|
||
$this->ajaxReturn($data);
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 10 - 30
|
||
* 普通模式退款 商家自己配置的支付直连退款接口退款查询
|
||
* */
|
||
protected function ptsearchRefund($orderid, $out_refund_no, $refund_id, $itemAll, $config, $Order)
|
||
{
|
||
$item1['apiclient_cert'] = $config['apiclient_cert'];
|
||
$item1['apiclient_key'] = $config['apiclient_key'];
|
||
$item1['key'] = $config['wx_secret_key'];
|
||
$WxMicroPay = new \Util\Wx\WxMicroPay($item1);
|
||
|
||
$obj['appid'] = $config['wx_appid'];
|
||
$obj['mch_id'] = $config['wx_mch_id'];
|
||
$obj['nonce_str'] = $WxMicroPay->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $out_refund_no;
|
||
$obj['refund_id'] = $refund_id;
|
||
$obj['sign'] = $WxMicroPay->getSign($obj);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/pay/refundquery';
|
||
$xml = $WxMicroPay->arrayToXml($obj);
|
||
|
||
$response = $WxMicroPay->postXmlCurlCancel($xml, $url, 30);
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
switch ($response['refund_status_0']) {
|
||
case 'PROCESSING':
|
||
case 'SUCCESS':
|
||
$PayOrder = D('PayOrder');
|
||
$save['goods_info'] = json_encode($itemAll['original_order']);
|
||
$res = $PayOrder->updateOrder(array('trade_no' => $Order['trade_no']), $save);
|
||
if ($res) {
|
||
$time = time();
|
||
$item['pid'] = $Order['id'];
|
||
$item['order_type'] = 3;
|
||
$item['pay_type'] = $Order['pay_type'];
|
||
$item['trade_amount'] = $itemAll['refund_money'];
|
||
$item['storeid'] = $Order['storeid'];
|
||
$item['adminid'] = $itemAll['adminid'];
|
||
$item['order_state'] = 'REFUND';
|
||
$item['cdate'] = $time;
|
||
$item['extend_content'] = json_encode($response);
|
||
$item['goods_info'] = json_encode($itemAll['refund_goods_info']);
|
||
$item['remark'] = '退款成功';
|
||
$item['trade_no'] = $response['refund_id_0'];
|
||
$item['out_trade_no'] = $itemAll['out_refund_no'];
|
||
$addRefund = $PayOrder->addOrder($item);
|
||
$merchantid = M('retail_store')->where(array('id' => $Order['storeid']))->getField('merchantid');
|
||
// 退货商品
|
||
// A('Sellapi/Order')->refund(json_encode($itemAll['refund_goods_info']),$Order['storeid'],$Order['id'],$itemAll['adminid'],$Order['pay_type']==1?3:4,$Order['id']);
|
||
$res = A('Sellapi/Refund')->hasOrderRefund($itemAll['adminid'], $itemAll['refund_goods_info'], $Order['storeid'], $merchantid, $itemAll['oldorderid'], $addRefund);
|
||
if ($res['status'] == 0) {
|
||
$data['msg2'] = $res['msg'];
|
||
}
|
||
if ($addRefund) {
|
||
$data['status'] = 1;
|
||
$data['msg'] = "退款成功";
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = "已提交退款申请<br>更新订单失败";
|
||
}
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = "已提交退款申请<br>更新订单失败";
|
||
}
|
||
$this->ajaxReturn($data);
|
||
break;
|
||
case 'REFUNDCLOSE':
|
||
//退款关闭
|
||
$data['status'] = 0;
|
||
$data['msg'] = '退款关闭';
|
||
$this->ajaxReturn($data);
|
||
break;
|
||
case 'CHANGE':
|
||
//退款异常-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,可前往商户平台(pay.weixin.qq.com)-交易中心,手动处理此笔退款。
|
||
$data['status'] = 0;
|
||
$data['msg'] = '退款异常,商户银行卡异常';
|
||
$this->ajaxReturn($data);
|
||
}
|
||
} else {
|
||
$this->ptsearchRefund($Order, $out_refund_no, $refund_id, $itemAll, $config, $Order);
|
||
}
|
||
}
|
||
/*
|
||
* 10-30
|
||
* 服务商模式退款
|
||
* */
|
||
// 直连退款接口
|
||
public function fwsrefund($storeid, $trade_no, $itemAll)
|
||
{
|
||
$PayOrder = D('PayOrder');
|
||
$map['trade_no'] = $trade_no;
|
||
$Order = $PayOrder->getFind($map);
|
||
$config = D('Payset')->getFindConfig($storeid);
|
||
$id = $config["merchantid"];
|
||
$partnerid = M("retail_merchant")->where(array("id" => $id))->getField("partnerid");
|
||
$cache = M("systemconfig")->where(array("partnerid" => $partnerid))->find();
|
||
$obj['appid'] = $cache['wx_appid'];
|
||
$obj['mch_id'] = $cache['mch_id'];
|
||
$obj['sub_appid'] = $config['wx_appid'];
|
||
$obj['sub_mch_id'] = $config['wx_sub_mchid'];
|
||
//print_r($dcping_api);
|
||
//以下为往微信发送的数据
|
||
$WxPaysdk = new \Util\Wx\Wxpaysdk(array('key' => $cache['wx_key']));
|
||
$obj['nonce_str'] = $WxPaysdk->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $itemAll['out_refund_no'];
|
||
$obj['total_fee'] = $Order['trade_amount'] * 100;
|
||
$obj['refund_fee'] = $itemAll['refund_money'] * 100;
|
||
$obj['sign'] = $WxPaysdk->getSign($obj);
|
||
$arr['apiclient_cert'] = $cache['wx_apiclient_cert'];
|
||
$arr['apiclient_key'] = $cache['wx_apiclient_key'];
|
||
//trade_state
|
||
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
|
||
$xml = $WxPaysdk->arrayToXml($obj);//转换为xml
|
||
file_put_contents('./wxrefunddata.txt', 1);
|
||
$postWx = new \Util\Wx\WxMicroPay($arr);
|
||
$response = $postWx->postXmlCurlCancel($xml, $url, 30);
|
||
// var_dump($postWx);
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
file_put_contents('./wxrefundreturn.txt', json_encode($response) . "\r\n", FILE_APPEND);
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
$this->fwssearchRefund($Order['id'], $obj['out_refund_no'], $response['refund_id'], $itemAll, $config, $Order, $cache);
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = $response['err_code_des'] ? $response['err_code_des'] : $response['return_msg'] ? $response['return_msg'] : '退款失败';
|
||
}
|
||
$this->ajaxReturn($data);
|
||
}
|
||
/*
|
||
* 服务商模式退款查询
|
||
* */
|
||
// 退款查询
|
||
public function fwssearchRefund($orderid, $out_refund_no, $refund_id, $itemAll, $config, $Order, $cache)
|
||
{
|
||
$obj['appid'] = $cache['wx_appid'];
|
||
$obj['mch_id'] = $cache['mch_id'];
|
||
$obj['sub_appid'] = $config['wx_appid'];
|
||
$obj['sub_mch_id'] = $config['wx_sub_mchid'];
|
||
//以下为往微信发送的数据
|
||
$WxPaysdk = new \Util\Wx\Wxpaysdk(array('key' => $cache['wx_key']));
|
||
$obj['nonce_str'] = $WxPaysdk->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $itemAll['out_refund_no'];
|
||
//以下为往微信发送的数据
|
||
$obj['refund_id'] = $refund_id;
|
||
$obj['sign'] = $WxPaysdk->getSign($obj);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/pay/refundquery';
|
||
$xml = $WxPaysdk->arrayToXml($obj);//转换为xml
|
||
$arr['apiclient_cert'] = $cache['wx_apiclient_cert'];
|
||
$arr['apiclient_key'] = $cache['wx_apiclient_key'];
|
||
$postWx = new \Util\Wx\WxMicroPay($arr);
|
||
$response = $postWx->postXmlCurlCancel($xml, $url, 30);
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
file_put_contents('./wxrefund.txt', json_encode($response));
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
switch ($response['refund_status_0']) {
|
||
case 'PROCESSING':
|
||
case 'SUCCESS':
|
||
$PayOrder = D('PayOrder');
|
||
$time = time();
|
||
$item['pid'] = $Order['id'];
|
||
$item['order_type'] = 3;
|
||
$item['pay_type'] = $Order['pay_type'];
|
||
$item['trade_amount'] = $itemAll['refund_money'];
|
||
$item['storeid'] = $Order['storeid'];
|
||
$item['adminid'] = $itemAll['adminid'];
|
||
$item['order_state'] = 'REFUND';
|
||
$item['cdate'] = $time;
|
||
$item['extend_content'] = json_encode($response);
|
||
$item['goods_info'] = json_encode($itemAll['refund_goods_info']);
|
||
$item['remark'] = '退款成功';
|
||
$item['trade_no'] = $response['refund_id_0'];
|
||
$item['out_trade_no'] = $itemAll['out_refund_no'];
|
||
$addRefund = $PayOrder->addOrder($item);
|
||
$merchantid = M('retail_store')->where(array('id' => $Order['storeid']))->getField('merchantid');
|
||
// 退货商品
|
||
$res2 = A('Sellapi/Refund')->hasOrderRefund($itemAll['adminid'], $itemAll['refund_goods_info'], $Order['storeid'], $merchantid, $itemAll['oldorderid'], $addRefund);
|
||
if ($res2['status'] == 0) {
|
||
$data['msg2'] = $res2['msg'];
|
||
}
|
||
if ($addRefund) {
|
||
$data['status'] = 1;
|
||
$data['msg'] = "退款成功";
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = "已提交退款申请<br>更新订单失败";
|
||
}
|
||
$this->ajaxReturn($data);
|
||
break;
|
||
case 'REFUNDCLOSE':
|
||
//退款关闭
|
||
$data['status'] = 0;
|
||
$data['msg'] = '退款关闭';
|
||
$this->ajaxReturn($data);
|
||
break;
|
||
case 'CHANGE':
|
||
//退款异常-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,可前往商户平台(pay.weixin.qq.com)-交易中心,手动处理此笔退款。
|
||
$data['status'] = 0;
|
||
$data['msg'] = '退款异常,商户银行卡异常';
|
||
$this->ajaxReturn($data);
|
||
}
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '退款失败';
|
||
$this->ajaxReturn($data);
|
||
}
|
||
}
|
||
|
||
|
||
//10-31 修改支付流程 PS:下方orderid 为retail_pay_order支付表id 并非后台用户商品订单需要的id
|
||
public function toWxPaybackups($pay_order, $bill_id, $config)
|
||
{
|
||
if ($pay_order["m_pay_type"] == 1) {
|
||
$post['appid'] = $config['wx_appid'];
|
||
$post['mch_id'] = $config['wx_mch_id'];
|
||
$post['body'] = $config['name'] . '-' . $config['storetag'];
|
||
$post['out_trade_no'] = $pay_order['out_trade_no'];
|
||
$post['total_fee'] = $pay_order['trade_amount'];
|
||
$post['auth_code'] = $pay_order['code'];
|
||
$post['key'] = $config['wx_secret_key'];
|
||
} else if ($pay_order["m_pay_type"] == 2) {
|
||
//查询合作伙伴配置
|
||
$id = $config["merchantid"];
|
||
$partnerid = M("retail_merchant")->where(array("id" => $id))->getField("partnerid");
|
||
$cache = M("systemconfig")->where(array("partnerid" => $partnerid))->find();
|
||
$post['appid'] = $cache['wx_appid'];
|
||
$post['mch_id'] = $cache['mch_id'];
|
||
$post['body'] = $config['name'] . '-' . $config['storetag'];
|
||
$post['out_trade_no'] = $pay_order['out_trade_no'];
|
||
$post['total_fee'] = $pay_order['trade_amount'];
|
||
$post['auth_code'] = $pay_order['code'];
|
||
$post['key'] = $cache['wx_key'];
|
||
$post['sub_mch_id'] = $config["wx_sub_mchid"];
|
||
if(!empty($config['wx_sub_appid'])){
|
||
$post['sub_appid'] = $config['wx_sub_appid'];
|
||
}
|
||
} else if ($pay_order["m_pay_type"] == 3) {
|
||
//新大陆支付
|
||
$continent['tradeNo'] = $pay_order['out_trade_no'];
|
||
$continent['authCode'] = $pay_order['code'];
|
||
$continent['types'] = 2;
|
||
$continent['subject'] = '测试';
|
||
$continent['amount'] = $pay_order['trade_amount'];
|
||
$continent['total_amount'] = $pay_order['trade_amount'];
|
||
A('Pay/PayContinent')->pay($continent, $pay_order, $bill_id);
|
||
exit;
|
||
}
|
||
//调起支付
|
||
$WxPaysdk = new \Util\Wx\WxMicroPay($post);
|
||
$result = $WxPaysdk->postWxMicro();
|
||
|
||
file_put_contents('./okok.txt', '1: ' . json_encode($post) . "\r\n", FILE_APPEND);
|
||
file_put_contents('./okok.txt', '2: ' . json_encode($result) . "\r\n", FILE_APPEND);
|
||
|
||
if (!empty($result) && $result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
|
||
//修改订单状态
|
||
$save ['trade_no'] = $result ['transaction_id'];
|
||
$save ['order_state'] = 'SUCCESS';
|
||
$time = substr($result['time_end'], 0, strlen($result['time_end']) - 10) . '-' . substr($result['time_end'], -10, 2) . '-' . substr($result['time_end'], -8, 2) . ' ' . substr($result['time_end'], -6, 2) . ':' . substr($result['time_end'], -4, 2) . ':' . substr($result['time_end'], -2, 2);
|
||
$save ['tradedate'] = strtotime($time);
|
||
$save ['extend_content'] = json_encode($result);
|
||
|
||
$PayOrderModel = D('PayOrder');
|
||
$update = $PayOrderModel->updateOrder(array('id' => $pay_order['id']), $save);//更新订单信息
|
||
if ($update) {
|
||
if ($config["status"] == 3) {
|
||
A('Api/Vip')->toMark($result['sub_openid'], $pay_order['id'], $pay_order['trade_amount']);
|
||
} else {
|
||
A('Api/Vip')->toMark($result['openid'], $pay_order['id'], $pay_order['trade_amount']);
|
||
}
|
||
file_put_contents('./orderid.txt', $pay_order['id']);
|
||
$data['status'] = 1;
|
||
$data['msg'] = '支付成功';
|
||
$data['out_trade_no'] = $pay_order['out_trade_no'];
|
||
$data['mdate'] = $save ['tradedate'];
|
||
$data['pay_type'] = "微信";
|
||
//打印机
|
||
A('Sellapi/Pay')->wxChange($bill_id, 3);
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '付款成功,更新订单失败,请联系管理员1';
|
||
}
|
||
} else if (!empty($result) && $result['err_code'] == 'USERPAYING' && $result["return_code"] == "SUCCESS" && $result["return_msg"] == "OK") {//用户支付中
|
||
$this->getMicroPayStatebackups($pay_order, $bill_id);
|
||
} else if (!empty($result)) {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '收款失败';
|
||
$data['data'] = $result;
|
||
$this->ajaxReturn($data);
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['data'] = $result;
|
||
$data['msg'] = "收款失败,请重试";
|
||
$this->ajaxReturn($data);
|
||
}
|
||
}
|
||
//2019-3-4加盟店店铺余额
|
||
//10-31 修改支付流程 PS:下方orderid 为retail_pay_order支付表id 并非后台用户商品订单需要的id
|
||
public function toWxPaybackups_v1($pay_order, $bill_id, $config)
|
||
{
|
||
if ($pay_order["m_pay_type"] == 1) {
|
||
$post['appid'] = $config['wx_appid'];
|
||
$post['mch_id'] = $config['wx_mch_id'];
|
||
$post['body'] = $config['name'] . '-' . $config['storetag'];
|
||
$post['out_trade_no'] = $pay_order['out_trade_no'];
|
||
$post['total_fee'] = $pay_order['trade_amount'];
|
||
$post['auth_code'] = $pay_order['code'];
|
||
$post['key'] = $config['wx_secret_key'];
|
||
} else if ($pay_order["m_pay_type"] == 2) {
|
||
//查询合作伙伴配置
|
||
$id = $config["merchantid"];
|
||
$partnerid = M("retail_merchant")->where(array("id" => $id))->getField("partnerid");
|
||
$cache = M("systemconfig")->where(array("partnerid" => $partnerid))->find();
|
||
$post['appid'] = $cache['wx_appid'];
|
||
$post['mch_id'] = $cache['mch_id'];
|
||
$post['body'] = $config['name'] . '-' . $config['storetag'];
|
||
$post['out_trade_no'] = $pay_order['out_trade_no'];
|
||
$post['total_fee'] = $pay_order['trade_amount'];
|
||
$post['auth_code'] = $pay_order['code'];
|
||
$post['key'] = $cache['wx_key'];
|
||
$post['sub_mch_id'] = $config["wx_sub_mchid"];
|
||
$post['sub_appid'] = $config['wx_appid'];
|
||
} else if ($pay_order["m_pay_type"] == 3) {
|
||
//新大陆支付
|
||
$continent['tradeNo'] = $pay_order['out_trade_no'];
|
||
$continent['authCode'] = $pay_order['code'];
|
||
$continent['types'] = 2;
|
||
$continent['subject'] = '测试';
|
||
$continent['amount'] = $pay_order['trade_amount'];
|
||
$continent['total_amount'] = $pay_order['trade_amount'];
|
||
A('Pay/PayContinent')->pay($continent, $pay_order, $bill_id);
|
||
exit;
|
||
}
|
||
//调起支付
|
||
$WxPaysdk = new \Util\Wx\WxMicroPay($post);
|
||
$result = $WxPaysdk->postWxMicro();
|
||
|
||
file_put_contents('./okok.txt', '1: ' . json_encode($post) . "\r\n", FILE_APPEND);
|
||
file_put_contents('./okok.txt', '2: ' . json_encode($result) . "\r\n", FILE_APPEND);
|
||
|
||
if (!empty($result) && $result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
|
||
//修改订单状态
|
||
$save ['trade_no'] = $result ['transaction_id'];
|
||
$save ['order_state'] = 'SUCCESS';
|
||
$time = substr($result['time_end'], 0, strlen($result['time_end']) - 10) . '-' . substr($result['time_end'], -10, 2) . '-' . substr($result['time_end'], -8, 2) . ' ' . substr($result['time_end'], -6, 2) . ':' . substr($result['time_end'], -4, 2) . ':' . substr($result['time_end'], -2, 2);
|
||
$save ['tradedate'] = strtotime($time);
|
||
$save ['extend_content'] = json_encode($result);
|
||
|
||
$PayOrderModel = D('PayOrder');
|
||
$update = $PayOrderModel->updateOrder(array('id' => $pay_order['id']), $save);//更新订单信息
|
||
if ($update) {
|
||
if ($config["status"] == 3) {
|
||
A('Api/Vip')->toMark($result['sub_openid'], $pay_order['id'], $pay_order['trade_amount']);
|
||
} else {
|
||
A('Api/Vip')->toMark($result['openid'], $pay_order['id'], $pay_order['trade_amount']);
|
||
}
|
||
file_put_contents('./orderid.txt', $pay_order['id']);
|
||
$data['status'] = 1;
|
||
$data['msg'] = '支付成功';
|
||
$data['out_trade_no'] = $pay_order['out_trade_no'];
|
||
$data['mdate'] = $save ['tradedate'];
|
||
$data['pay_type'] = "微信";
|
||
//打印机
|
||
A('Sellapi/Pay')->wxChange_v1($bill_id, 3);
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '付款成功,更新订单失败,请联系管理员1';
|
||
}
|
||
} else if (!empty($result) && $result['err_code'] == 'USERPAYING' && $result["return_code"] == "SUCCESS" && $result["return_msg"] == "OK") {//用户支付中
|
||
$this->getMicroPayStatebackups($pay_order, $bill_id);
|
||
} else if (!empty($result)) {
|
||
$data['status'] = 0;
|
||
$data['msg'] = '收款失败';
|
||
$data['data'] = $result;
|
||
$this->ajaxReturn($data);
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['data'] = $result;
|
||
$data['msg'] = "收款失败,请重试";
|
||
$this->ajaxReturn($data);
|
||
}
|
||
}
|
||
|
||
//反复请求用户长时间才输入密码的支付状态 PS:下方orderid 为retail_pay_order支付表id 并非后台用户商品订单需要的id
|
||
protected function getMicroPayStatebackups($Order, $bill_id)
|
||
{
|
||
$PayOrderModel = D('PayOrder');
|
||
$map['id'] = $Order['id'];
|
||
$res = $this->searchWxOrder($Order['storeid'], $Order['out_trade_no']);
|
||
$data['data'] = $res;
|
||
if ($res['trade_state'] == "USERPAYING" || $res['trade_code'] == "USERPAYING") {
|
||
//用户支付中
|
||
$time = get_runtime();
|
||
if ($time < 20) {
|
||
$this->getMicroPayStatebackups($Order, $bill_id);
|
||
} else {
|
||
//撤销订单
|
||
$this->canselOrder($Order['storeid'],$Order);
|
||
}
|
||
} else if ($res['trade_state'] == "SUCCESS" || $res['trade_code'] == "SUCCESS") {//支付成功修改订单
|
||
$save ['trade_no'] = $res ['transaction_id'];
|
||
$save ['order_state'] = 'SUCCESS';
|
||
$time = substr($res['time_end'], 0, strlen($res['time_end']) - 10) . '-' . substr($res['time_end'], -10, 2) . '-' . substr($res['time_end'], -8, 2) . ' ' . substr($res['time_end'], -6, 2) . ':' . substr($res['time_end'], -4, 2) . ':' . substr($res['time_end'], -2, 2);
|
||
$save ['tradedate'] = strtotime($time);
|
||
$save ['mdate'] = time();
|
||
$save ['extend_content'] = json_encode($res);
|
||
|
||
$update = $PayOrderModel->updateOrder($map, $save);//更新订单信息
|
||
if ($update) {
|
||
if (isset($res['sub_appid'])) {
|
||
A('Api/Vip')->toMark($res['sub_openid'], $Order['id'], $Order['trade_amount']);
|
||
} else {
|
||
A('Api/Vip')->toMark($res['openid'], $Order['id'], $Order['trade_amount']);
|
||
}
|
||
// file_put_contents('./orderid2.txt', $Order['id']);
|
||
//支付成功
|
||
A('Sellapi/Pay')->wxChange($bill_id, 3);
|
||
} else {
|
||
A('Sellapi/Pay')->wxChange($bill_id, 3);
|
||
$data['status'] = 0;
|
||
$data['msg'] = '付款成功,更新订单失败,请联系管理员2';
|
||
}
|
||
} else {
|
||
$save ['order_state'] = 'REVOKED';
|
||
$save ['mdate'] = time();
|
||
$update = $PayOrderModel->updateOrder($map, $save);//更新订单信息
|
||
$data['status'] = 0;
|
||
$data['msg'] = '用户取消支付';
|
||
$data['res'] = $res;
|
||
$this->ajaxReturn($data);
|
||
}
|
||
}
|
||
|
||
|
||
/*
|
||
* 2018-11-16 退款
|
||
* */
|
||
public function newRefund_v1($trade_no, $itemAll, $pay_order)
|
||
{
|
||
if ($pay_order['m_pay_type'] == 1) {
|
||
$res = $this->ptRefund_v1($itemAll, $pay_order); // 普通模式退款 商家自己配置的支付
|
||
} else if ($pay_order['m_pay_type'] == 2) {
|
||
$res = $this->fwsRefund_v1($trade_no, $itemAll); //查询合作伙伴配置 服务商模式支付退款
|
||
} else if ($pay_order['m_pay_type'] == 3) {
|
||
$res = A('Pay/PayContinent')->newRefund_v1($trade_no, $itemAll['merchantid'], $itemAll);//新大陆支付退款
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '订单信息有误';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
/*
|
||
* 2018-11-16
|
||
* 普通模式退款 商家自己配置的支付直连退款接口
|
||
* */
|
||
public function ptRefund_v1($itemAll, $Order)
|
||
{
|
||
$config = D('Payset')->getFindConfig($itemAll['storeid']);
|
||
//以下为往微信发送的数据
|
||
$item1['apiclient_cert'] = $config['apiclient_cert'];
|
||
$item1['apiclient_key'] = $config['apiclient_key'];
|
||
$item1['key'] = $config['wx_secret_key'];
|
||
$WxMicroPay = new \Util\Wx\WxMicroPay($item1);
|
||
|
||
$obj['appid'] = $config['wx_appid'];
|
||
$obj['mch_id'] = $config['wx_mch_id'];
|
||
$obj['nonce_str'] = $WxMicroPay->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $itemAll['out_refund_no'];
|
||
$obj['total_fee'] = $Order['trade_amount'] * 100;
|
||
$obj['refund_fee'] = $itemAll['refund_money'] * 100;
|
||
$obj['sign'] = $WxMicroPay->getSign($obj);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
|
||
$xml = $WxMicroPay->arrayToXml($obj);//转换为xml
|
||
|
||
$response = $WxMicroPay->postXmlCurlCancel($xml, $url, 30);
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
file_put_contents('./wx_refund1.txt', json_encode($response) . "\r\n", FILE_APPEND);
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
$res = $this->ptSearchRefund_v1($response['refund_id'], $itemAll, $config, $Order);
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['data'] = $response;
|
||
$res['msg'] = '退款失败,请商家检查微信可用余额';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
/*
|
||
* 2018-11-16
|
||
* 普通模式退款查询 商家自己配置的支付直连退款接口退款查询
|
||
* */
|
||
protected function ptSearchRefund_v1($refund_id, $itemAll, $config, $Order)
|
||
{
|
||
$item1['apiclient_cert'] = $config['apiclient_cert'];
|
||
$item1['apiclient_key'] = $config['apiclient_key'];
|
||
$item1['key'] = $config['wx_secret_key'];
|
||
$WxMicroPay = new \Util\Wx\WxMicroPay($item1);
|
||
|
||
$obj['appid'] = $config['wx_appid'];
|
||
$obj['mch_id'] = $config['wx_mch_id'];
|
||
$obj['nonce_str'] = $WxMicroPay->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $itemAll['out_refund_no'];
|
||
$obj['refund_id'] = $refund_id;
|
||
$obj['sign'] = $WxMicroPay->getSign($obj);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/pay/refundquery';
|
||
$xml = $WxMicroPay->arrayToXml($obj);
|
||
|
||
$response = $WxMicroPay->postXmlCurlCancel($xml, $url, 30);
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
switch ($response['refund_status_0']) {
|
||
case 'PROCESSING':
|
||
case 'SUCCESS':
|
||
$res['status'] = 1;
|
||
$res['data'] = $response;
|
||
$res['msg'] = '退款成功';
|
||
break;
|
||
case 'REFUNDCLOSE':
|
||
//退款关闭
|
||
$res['status'] = 0;
|
||
$res['msg'] = '退款关闭';
|
||
break;
|
||
case 'CHANGE':
|
||
//退款异常-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,可前往商户平台(pay.weixin.qq.com)-交易中心,手动处理此笔退款。
|
||
$res['status'] = 0;
|
||
$res['msg'] = '退款异常,商户银行卡异常';
|
||
}
|
||
} else {
|
||
$res['obj'] = $obj;
|
||
$res['data'] = $response;
|
||
$res['status'] = 0;
|
||
$res['msg'] = '退款失败';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
/*
|
||
* 2018-11-16
|
||
* 服务商模式退款
|
||
* */
|
||
public function fwsRefund_v1($trade_no, $itemAll)
|
||
{
|
||
$PayOrder = D('PayOrder');
|
||
$map['trade_no'] = $trade_no;
|
||
$Order = $PayOrder->getFind($map);
|
||
$config = D('Payset')->getFindConfig($itemAll['storeid']);
|
||
$id = $config["merchantid"];
|
||
$partnerid = M("retail_merchant")->where(array("id" => $id))->getField("partnerid");
|
||
$cache = M("systemconfig")->where(array("partnerid" => $partnerid))->find();
|
||
//以下为往微信发送的数据
|
||
$obj['appid'] = $cache['wx_appid'];
|
||
$obj['mch_id'] = $cache['mch_id'];
|
||
if(!empty($config['wx_sub_appid'])){
|
||
$obj['sub_appid'] = $config['wx_sub_appid'];
|
||
}
|
||
$obj['sub_mch_id'] = $config['wx_sub_mchid'];
|
||
|
||
$WxPaysdk = new \Util\Wx\Wxpaysdk(array('key' => $cache['wx_key']));
|
||
$obj['nonce_str'] = $WxPaysdk->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $itemAll['out_refund_no'];
|
||
$obj['total_fee'] = $Order['trade_amount'] * 100;
|
||
$obj['refund_fee'] = $itemAll['refund_money'] * 100;
|
||
$obj['sign'] = $WxPaysdk->getSign($obj);
|
||
$arr['apiclient_cert'] = $cache['wx_apiclient_cert'];
|
||
$arr['apiclient_key'] = $cache['wx_apiclient_key'];
|
||
$arr['key'] = $cache['wx_key'];
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
|
||
$xml = $WxPaysdk->arrayToXml($obj);
|
||
file_put_contents('./wxrefunddata.txt', 1);
|
||
$postWx = new \Util\Wx\WxMicroPay($arr);
|
||
$response = $postWx->postXmlCurlCancel($xml, $url, 30);
|
||
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
file_put_contents('./wxrefundreturn.txt', json_encode($response) . "\r\n", FILE_APPEND);
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
$res = $this->fwsSearchRefund_v1($response['refund_id'], $itemAll, $config, $Order, $cache);
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['msg'] = $response['err_code_des'] ? $response['err_code_des'] : $response['return_msg'] ? $response['return_msg'] : '退款失败';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
/*
|
||
* 服务商模式退款查询
|
||
* */
|
||
public function fwsSearchRefund_v1($refund_id, $itemAll, $config, $Order, $cache)
|
||
{
|
||
//以下为往微信发送的数据
|
||
$WxPaysdk = new \Util\Wx\Wxpaysdk(array('key' => $cache['wx_key']));
|
||
$obj['appid'] = $cache['wx_appid'];
|
||
$obj['mch_id'] = $cache['mch_id'];
|
||
if(!empty($config['wx_sub_appid'])){
|
||
$obj['sub_appid'] = $config['wx_sub_appid'];
|
||
}
|
||
$obj['sub_mch_id'] = $config['wx_sub_mchid'];
|
||
$obj['nonce_str'] = $WxPaysdk->createNoncestr(32);
|
||
$obj['transaction_id'] = $Order['trade_no'];
|
||
$obj['out_trade_no'] = $Order['out_trade_no'];
|
||
$obj['out_refund_no'] = $itemAll['out_refund_no'];
|
||
$obj['refund_id'] = $refund_id;
|
||
$obj['sign'] = $WxPaysdk->getSign($obj);
|
||
|
||
$url = 'https://api.mch.weixin.qq.com/pay/refundquery';
|
||
$xml = $WxPaysdk->arrayToXml($obj);
|
||
$arr['apiclient_cert'] = $cache['wx_apiclient_cert'];
|
||
$arr['apiclient_key'] = $cache['wx_apiclient_key'];
|
||
$postWx = new \Util\Wx\WxMicroPay($arr);
|
||
$response = $postWx->postXmlCurlCancel($xml, $url, 30);
|
||
$response = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
|
||
file_put_contents('./wxrefund.txt', json_encode($response));
|
||
if ($response['result_code'] == 'SUCCESS' && $response['return_code'] == 'SUCCESS') {
|
||
switch ($response['refund_status_0']) {
|
||
case 'PROCESSING':
|
||
case 'SUCCESS':
|
||
$res['status'] = 1;
|
||
$res['data'] = $response;
|
||
$res['msg'] = '退款成功';
|
||
break;
|
||
case 'REFUNDCLOSE':
|
||
//退款关闭
|
||
$res['status'] = 0;
|
||
$res['msg'] = '退款关闭';
|
||
break;
|
||
case 'CHANGE':
|
||
//退款异常-退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,可前往商户平台(pay.weixin.qq.com)-交易中心,手动处理此笔退款。
|
||
$res['status'] = 0;
|
||
$res['msg'] = '退款异常,商户银行卡异常';
|
||
}
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['data'] = $response;
|
||
$res['msg'] = '退款失败';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
|
||
/*
|
||
* 2018-11-28 会员充值
|
||
* */
|
||
public function toWxPayRecharge($order, $config)
|
||
{
|
||
if ($order["m_pay_type"] == 1) {
|
||
$post['appid'] = $config['wx_appid'];
|
||
$post['mch_id'] = $config['wx_mch_id'];
|
||
$post['body'] = $config['name'] . '-' . $config['storetag'];
|
||
$post['out_trade_no'] = $order['out_trade_no'];
|
||
$post['total_fee'] = $order['trade_amount'];
|
||
$post['auth_code'] = $order['code'];
|
||
$post['key'] = $config['wx_secret_key'];
|
||
} else if ($order["m_pay_type"] == 2) {
|
||
//查询合作伙伴配置
|
||
$partnerid = M("retail_merchant")->where(array("id" => $config["merchantid"]))->getField("partnerid");
|
||
$cache = M("systemconfig")->where(array("partnerid" => $partnerid))->find();
|
||
$post['appid'] = $cache['wx_appid'];
|
||
$post['mch_id'] = $cache['mch_id'];
|
||
$post['body'] = $config['name'] . '-' . $config['storetag'];
|
||
$post['out_trade_no'] = $order['out_trade_no'];
|
||
$post['total_fee'] = $order['trade_amount'];
|
||
$post['auth_code'] = $order['code'];
|
||
$post['key'] = $cache['wx_key'];
|
||
$post['sub_mch_id'] = $config["wx_sub_mchid"];
|
||
if(!empty($config['wx_sub_appid'])){
|
||
$post['sub_appid'] = $config['wx_sub_appid'];
|
||
}
|
||
// file_put_contents("wxpay.txt", json_encode($post));
|
||
} else if ($order["m_pay_type"] == 3) {
|
||
//新大陆支付
|
||
$continent['tradeNo'] = $order['out_trade_no'];
|
||
$continent['authCode'] = $order['code'];
|
||
$continent['type'] = 2;
|
||
$continent['subject'] = '测试';
|
||
$continent['amount'] = $order['trade_amount'];
|
||
$continent['total_amount'] = $order['trade_amount'];
|
||
A('Pay/PayContinent')->payRecharge($continent, $config["merchantid"], $order);
|
||
}
|
||
$WxPaysdk = new \Util\Wx\WxMicroPay($post);
|
||
$result = $WxPaysdk->postWxMicro();
|
||
if (!empty($result) && $result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
|
||
// file_put_contents('./towxpay.txt', 'data: ' . json_encode($order) . "\r\n", FILE_APPEND); //写入文件 调试
|
||
//修改订单状态
|
||
$save ['trade_no'] = $result ['transaction_id'];
|
||
$save ['order_state'] = 'SUCCESS';
|
||
$time = substr($result['time_end'], 0, strlen($result['time_end']) - 10) . '-' . substr($result['time_end'], -10, 2) . '-' . substr($result['time_end'], -8, 2) . ' ' . substr($result['time_end'], -6, 2) . ':' . substr($result['time_end'], -4, 2) . ':' . substr($result['time_end'], -2, 2);
|
||
$save ['tradedate'] = strtotime($time);
|
||
$save ['extend_content'] = json_encode($result);
|
||
$PayOrderModel = D('PayOrder');
|
||
$this->vipRecharge($order, $config["merchantid"]);
|
||
// 充值送次卡
|
||
A('Xcxapi/VipCard')->rechargeGiveCard($order['merchantid'],$order['vid'],$order['trade_amount'],2);
|
||
$update = $PayOrderModel->updateOrder(array('id' => $order['id']), $save);//更新订单信息
|
||
if ($update) {
|
||
$data['status'] = 1;
|
||
$data['msg'] = '充值成功';
|
||
$data['order_id'] = $order['id'];
|
||
} else {
|
||
$data['status'] = 1;
|
||
$data['msg'] = '充值成功,更新订单失败,请联系管理员1';
|
||
$data['order_id'] = $order['id'];
|
||
}
|
||
} else if (!empty($result) && $result['err_code'] == 'USERPAYING' && $result["return_code"] == "SUCCESS" && $result["return_msg"] == "OK") {//用户支付中
|
||
$this->getMicroPayStateRecharge($order, $config["merchantid"]);
|
||
} else if (!empty($result)) {
|
||
$data['status'] = 0;
|
||
$data['post'] = $post;
|
||
$data['msg'] = $result['return_msg'];
|
||
} else {
|
||
$data['status'] = 0;
|
||
$data['msg'] = "收款失败,请重试";
|
||
}
|
||
$this->ajaxReturn($data);
|
||
}
|
||
|
||
private function getMicroPayStateRecharge($Order, $merchantid)
|
||
{
|
||
$PayOrderModel = D('PayOrder');
|
||
// file_put_contents('wx_searchWxOrder.txt','order: '.json_encode($Order)."\r\n",FILE_APPEND);
|
||
$res = $this->searchWxOrder($Order['storeid'], $Order['out_trade_no']);
|
||
if ($res['trade_state'] == "USERPAYING" || $res['trade_code'] == "USERPAYING") {
|
||
//用户支付中
|
||
$time = get_runtime();
|
||
if ($time < 20) {
|
||
$this->getMicroPayStateRecharge($Order, $merchantid);
|
||
} else {
|
||
//撤销订单
|
||
$this->canselOrder($Order['storeid'],$Order);
|
||
}
|
||
} else if ($res['trade_state'] == "SUCCESS" || $res['trade_code'] == "SUCCESS") {//支付成功修改订单
|
||
$save ['trade_no'] = $res ['transaction_id'];
|
||
$save ['order_state'] = 'SUCCESS';
|
||
$time = substr($res['time_end'], 0, strlen($res['time_end']) - 10) . '-' . substr($res['time_end'], -10, 2) . '-' . substr($res['time_end'], -8, 2) . ' ' . substr($res['time_end'], -6, 2) . ':' . substr($res['time_end'], -4, 2) . ':' . substr($res['time_end'], -2, 2);
|
||
$save ['tradedate'] = strtotime($time);
|
||
$save ['mdate'] = time();
|
||
$save ['extend_content'] = json_encode($res);
|
||
|
||
$update = $PayOrderModel->updateOrder(array('id' => $Order['id']), $save);//更新订单信息
|
||
$this->vipRecharge($Order, $merchantid);
|
||
if ($update) {
|
||
$data['status'] = 1;
|
||
$data['msg'] = '充值成功';
|
||
$data['order_id'] = $Order['id'];
|
||
} else {
|
||
$data['status'] = 1;
|
||
$data['msg'] = '付款成功,更新订单失败,请联系管理员2';
|
||
$data['order_id'] = $Order['id'];
|
||
}
|
||
$this->ajaxReturn($data);
|
||
} else {
|
||
$save ['order_state'] = 'REVOKED';
|
||
$save ['mdate'] = time();
|
||
$update = $PayOrderModel->updateOrder(array('id' => $Order['id']), $save);//更新订单信息
|
||
$data['status'] = 0;
|
||
$data['msg'] = '用户取消支付';
|
||
$data['res'] = $res;
|
||
$this->ajaxReturn($data);
|
||
}
|
||
}
|
||
} |