文件
mashangyoujiu/Application/Pay/Controller/WxMiniPayController.class.php

110 行
4.6 KiB
PHP
可执行文件

<?php
namespace Pay\Controller;
use Common\Model\PayInfoModel;
use Common\Model\RetailPayOrderModel;
use Common\Model\RetailPaySetModel;
use Think\Controller;
//[1.9.5]
/**
* 微信小程序支付
* Class WxMiniPayController
* @package Pay\Controller
*/
class WxMiniPayController extends Controller
{
/**
* 查询订单支付状态并更新
* @param $order
* @return mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function queryOrderAndUpdate($order)
{
$PaySetModel = new RetailPaySetModel();
$paySet = $PaySetModel->getFindMerchantConfig($order['merchant_id'], 3);
$config = $paySet['config'];
if ($order['m_pay_type'] == 3) {
// 新大陆
$PayContinent = new PayContinentController();
return $PayContinent->searchAndUpdateOrder_v3($order);
} else if ($order['m_pay_type'] == 4) {
// 云闪付模式
$CloudPay = new CloudPayController();
return $CloudPay->queryOrderAndUpdate($order);
} else if ($order['m_pay_type'] == 7) {
// 钱客云模式
$moneyGuest = new \MoneyGuestPay\Business\MoneyGuest();
$response = $moneyGuest->moneyGuestQueryOrder([
'pay_order' => $order,
'config' => $config
]);
return $response;
} else {
//直连
if ($order["m_pay_type"] == 1) {
// 普通模式
$key = $config[$PaySetModel::XCX_SECRET_KEY];
$post['appid'] = $config[$PaySetModel::XCX_APPID];
$post['mch_id'] = $config[$PaySetModel::XCX_MCH_ID];
} else {
// 服务商 $order['m_pay_type'] == 2
$SystemConfigModel = D('SystemConfig');
$systemConfig = $config['systemConfig'];
$post['appid'] = $systemConfig[$SystemConfigModel::WX_APPID];
$post['mch_id'] = $systemConfig[$SystemConfigModel::MCH_ID];
$post['sub_mch_id'] = $config[$PaySetModel::XCX_SUB_MCH_ID];
$post['sub_appid'] = $config[$PaySetModel::XCX_SUB_APPID];
$key = $systemConfig[$SystemConfigModel::WX_KEY];
}
$post['nonce_str'] = createKey(); //随机字符串
$post['out_trade_no'] = $order['out_trade_no'];
$post['sign'] = createSign($post, $key); //签名
// $result['post'] = $post;
$post_xml = arrToXml($post);
$url = 'https://api.mch.weixin.qq.com/pay/orderquery';
$xml = http_request($url, $post_xml); //POST方式请求http
$xcxArr = xmlToArray($xml); //将api返回xml数据转换成数组
if ($xcxArr['return_code'] == 'SUCCESS' && $xcxArr['result_code'] == 'SUCCESS' && $xcxArr['trade_state'] == 'SUCCESS') {
$PayOrderModel = new RetailPayOrderModel();
$updateData = array(
'order_state' => 'SUCCESS',
'extend_content' => json_encode($xcxArr),
'trade_date' => time(),
'trade_no' => $xcxArr['transaction_id'],
);
$PayOrderModel->updateOrder(array('id' => $order['id']), $updateData); //更新pay_order表
$where = [
'merchant_id' => $order['merchant_id'],
'openid' => $xcxArr['openid']
];
$PayInfo = new PayInfoModel();
$pay_info = $PayInfo->getFind($where);
if (empty($pay_info)) {
$pay_info = [
'merchant_id' => $order['merchant_id'],
'pay_order_id' => $order['id'],
'vip_id' => $order['vid'] ? $order['vid'] : 0,
'xcx_id' => $order['uid'] ? $order['uid'] : 0,
'm_pay_type' => $order['m_pay_type'],
'openid' => $xcxArr['openid'],
'sub_openid' => $xcxArr['sub_openid'] ? $xcxArr['sub_openid'] : '',
'type' => 1,
'add_time' => time(),
];
$PayInfo->addOrder($pay_info);
}
return callBack(statusCode()['success'], "支付成功");
} else {
return callBack(statusCode()['error'], "订单未支付");
}
}
}
}