376 行
16 KiB
PHP
可执行文件
376 行
16 KiB
PHP
可执行文件
<?php
|
|
|
|
|
|
namespace Pay\Controller;
|
|
|
|
use Common\Business\BranchProfit;
|
|
use Common\Model\RetailPayOrderModel;
|
|
use Common\Model\RetailPaySetModel;
|
|
use Pay\Library\Cloud\CloudAliPay as CloudAliPayCtrl;
|
|
use Pay\Library\Cloud\CloudWxPay as CloudWxPayCtrl;
|
|
use Pay\Library\Cloud\Cloud as CloudCtrl;
|
|
|
|
//[1.7.90]
|
|
|
|
/**
|
|
* 云闪付
|
|
* Class CloudPayController
|
|
* @package Pay\Controller
|
|
*/
|
|
class CloudPayController extends BaseController
|
|
{
|
|
// 编写日志控件
|
|
private $debug = 0;
|
|
|
|
/*刷卡支付 付款码支付*/
|
|
public function soltCard($Order, $config, $payType)
|
|
{
|
|
// 判断支付类型
|
|
// $payType 1 微信 ,2,支付宝 CODE
|
|
$PayOrderModel = new RetailPayOrderModel();
|
|
$configData = $this->retailPayConfigToCloudPayConfig($config);
|
|
if (!$configData) {
|
|
return $this->failCloudPay('云闪付服务商参数未配置');
|
|
}
|
|
if ($payType == 1) {
|
|
$CloudPay = new CloudWxPayCtrl($configData);
|
|
} else {
|
|
$CloudPay = new CloudAliPayCtrl($configData);
|
|
}
|
|
$order_no = $Order[$PayOrderModel::OUT_TRADE_NO]; // 外部订单号
|
|
$total_fee = $Order[$PayOrderModel::TRADE_AMOUNT]; // 付款金额 (元)
|
|
$total_fee = bcmul($total_fee, 100); // 付款金额 (元转分)
|
|
$code = $Order[$PayOrderModel::CODE]; // 付款码
|
|
$body = $configData['body'] ? $configData['body'] : '购买商品';
|
|
|
|
$res = $CloudPay->slotCard($order_no, $code, $total_fee, $body);
|
|
if (!$res) {
|
|
return $this->failCloudPay($CloudPay->getMsg());
|
|
}
|
|
// 查询订单判断是否支付成功
|
|
$result = $CloudPay->queryOrder($order_no);
|
|
if (!$result) {
|
|
return $this->failCloudPay($CloudPay->getMsg());
|
|
} else {
|
|
// 更新订单
|
|
if (isset($result['trade_status']) && $result['trade_status'] == 'TRADE_SUCCESS') {
|
|
$save = array(
|
|
$PayOrderModel::TRADE_NO => $result['trade_no'],
|
|
$PayOrderModel::ORDER_STATE => 'SUCCESS',
|
|
$PayOrderModel::TRADE_DATE => strtotime($result['time_end']),
|
|
$PayOrderModel::EXTEND_CONTENT => json_encode($result),
|
|
$PayOrderModel::MER_ID => $config['cloud_mer_id']
|
|
);
|
|
$update = $PayOrderModel->updateOrder(array($PayOrderModel::ID => $Order[$PayOrderModel::ID]), $save);
|
|
if ($Order[$PayOrderModel::M_PAY_TYPE] == 4) {
|
|
$this->doAddRate($result['trade_no'], $Order, 4);//分润(云闪付)
|
|
}
|
|
if ($update) {
|
|
return $this->successCloudPay($result);// 支付成功
|
|
}
|
|
return $this->failCloudPay('数据库发生错误,收款成功,订单更新失败,请联系服务商');// 支付成功 但数据库更新失败
|
|
}
|
|
return $this->failCloudPay('云闪付收款接口成功,但查询验证时失败,请联系服务商登录云闪付后台查看单据状态');// 支付成功 但数据库更新失败
|
|
}
|
|
}
|
|
|
|
/*生成支付二维吗*/
|
|
public function cloudPaybarCodeAll($Order, $config, $payType, $body = '购买商品')
|
|
{
|
|
// $payType 1 微信 ,2,支付宝 CODE
|
|
$PayOrderModel = new RetailPayOrderModel();
|
|
$configData = $this->retailPayConfigToCloudPayConfig($config);
|
|
if (!$configData) {
|
|
return $this->failCloudPay('云闪付服务商参数未配置');
|
|
}
|
|
if ($payType == 1) {
|
|
$CloudPay = new CloudWxPayCtrl($configData);
|
|
} else {
|
|
$CloudPay = new CloudAliPayCtrl($configData);
|
|
}
|
|
$order_no = $Order[$PayOrderModel::OUT_TRADE_NO]; // 外部订单号
|
|
$total_fee = $Order[$PayOrderModel::TRADE_AMOUNT]; // 付款金额 (元)
|
|
$total_fee = bcmul($total_fee, 100); // 付款金额 (元转分)
|
|
$backUrl = '/index.php/Pay/CloudPay/cloudBack';
|
|
$cache = $CloudPay->setBackUrl($backUrl)->native($order_no, $total_fee, $body);
|
|
if ($cache) {
|
|
return array('status' => 1, 'msg' => '二维码获取成功', 'qr_code' => $cache);
|
|
}
|
|
return $this->failCloudPay($CloudPay->getMsg());
|
|
}
|
|
|
|
/*统一下单仅微信可用*/
|
|
public function unifiedorder($Order, $config, $wxType = 1, $body = '')
|
|
{
|
|
$PayOrderModel = new RetailPayOrderModel();
|
|
$configData = $this->retailPayConfigToCloudPayConfig($config);
|
|
if (!$configData) {
|
|
return $this->failCloudPay('云闪付服务商参数未配置');
|
|
}
|
|
$CloudPay = new CloudWxPayCtrl($configData); // 统一下单仅微信可用
|
|
if ($wxType == 1) {
|
|
$orderType = 'wechatJs'; // 微信公众号支付
|
|
} else {
|
|
$orderType = 'wechatMiniProgram'; // 微信小程序支付
|
|
}
|
|
$open_id = $Order['userId']; // 付款码
|
|
$order_no = $Order[$PayOrderModel::OUT_TRADE_NO]; // 外部订单号
|
|
$total_fee = $Order[$PayOrderModel::TRADE_AMOUNT]; // 付款金额 (元)
|
|
$total_fee = bcmul($total_fee, 100); // 付款金额 (元转分)
|
|
$backUrl = '/index.php/Pay/CloudPay/cloudBack';
|
|
$cache = $CloudPay->setBackUrl($backUrl)->unifiedorder($order_no, $open_id, $total_fee, $orderType, $body);
|
|
if ($cache) {
|
|
return $this->successCloudPay($cache, '微信下单成功');
|
|
}
|
|
return $this->failCloudPay($CloudPay->getMsg());
|
|
}
|
|
|
|
/*云闪付支付回调 (统一下单);*/
|
|
public function cloudBack()
|
|
{
|
|
$data = file_get_contents('php://input');
|
|
$postData = json_decode($data, true);
|
|
$this->doLogs(array('name' => '云闪付回调', 'data' => $postData), __LINE__);
|
|
// 验证签名一套代码有问题 【修改为查询订单】。。。。
|
|
$signature = $postData['signature'];
|
|
unset($postData['signature']);
|
|
|
|
$PayOrderModel = new RetailPayOrderModel();
|
|
$PaySetModel = new RetailPaySetModel();
|
|
$pMap = array(
|
|
$PayOrderModel::OUT_TRADE_NO => strval($postData['orderNo']),
|
|
);
|
|
$payOrder = $PayOrderModel->getFind($pMap);
|
|
$this->doLogs(array('name' => '云闪付回调1', 'data' => $payOrder), __LINE__);
|
|
if (empty($payOrder)) { // 订单不存在
|
|
die('fail');
|
|
}
|
|
if ($payOrder[$PayOrderModel::ORDER_STATE] == 'SUCCESS') { // 订单已更新
|
|
die('success');
|
|
}
|
|
try {
|
|
// 1,微信,2,支付宝,3,小程序
|
|
$payTypeArr = array('1' => 1, '2' => 2, '4' => 3);
|
|
$paySet = $PaySetModel->getFindMerchantConfig($payOrder[$PayOrderModel::MERCHANT_ID], $payTypeArr[$payOrder[$PayOrderModel::PAY_TYPE]]);
|
|
$configData = $this->retailPayConfigToCloudPayConfig($paySet['config']);
|
|
$CloudCtrl = new CloudCtrl($configData);
|
|
// 因为云闪付支付 回调验证签名无法成功,故使用查询接口,查询订单状态,如果支付成功,则默认验证签名成功
|
|
$newSign = $CloudCtrl->getNotifyCheck($postData['orderNo'], $payOrder['pay_type'] == 2 ? 'alipay' : 'wechat');
|
|
$this->doLogs(array('name' => '云闪付回调2', 'data' => $payOrder), __LINE__);
|
|
if ($newSign) {
|
|
//分润
|
|
A('Pay/Base')->doAddRate($payOrder['trade_no'], $payOrder, 4);//分润
|
|
//签名验证成功
|
|
$updateData = array(
|
|
$PayOrderModel::ORDER_STATE => 'SUCCESS',
|
|
$PayOrderModel::EXTEND_CONTENT => json_encode($postData),
|
|
$PayOrderModel::TRADE_DATE => strtotime($postData['payTime']),
|
|
$PayOrderModel::TRADE_NO => $postData['transIndex']
|
|
);
|
|
$pMap = array(
|
|
$PayOrderModel::ID => $payOrder[$PayOrderModel::ID]
|
|
);
|
|
$PayOrderModel->updateOrder($pMap, $updateData); //更新pay_order表
|
|
if ($payOrder[$PayOrderModel::ORDER_TYPE] == 6) {
|
|
die('success'); // 快捷收银
|
|
}
|
|
define('IsPayToUpdateOrder', 'YES');//存入常量
|
|
R($payOrder[$PayOrderModel::NOTIFY_URL], array($payOrder));//执行回调函数
|
|
$this->doLogs(array('name' => '云闪付回调', 'msg' => '验证签名成功', 'data' => $postData), __LINE__);
|
|
die('success');
|
|
} else {
|
|
die('fail');
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->doLogs(array('name' => '云闪付回调error', 'data' => $e->getMessage()), __LINE__);
|
|
}
|
|
|
|
}
|
|
|
|
/*云闪付退款*/
|
|
public function cloudRefund($Order, $config, $payType)
|
|
{
|
|
|
|
$PayOrderModel = new RetailPayOrderModel();
|
|
$configData = $this->retailPayConfigToCloudPayConfig($config);
|
|
if (!$configData) {
|
|
return $this->failCloudPay('云闪付服务商参数未配置');
|
|
}
|
|
if ($payType == 1 || $payType == 4) {
|
|
$CloudPay = new CloudWxPayCtrl($configData);
|
|
} else {
|
|
$CloudPay = new CloudAliPayCtrl($configData);
|
|
}
|
|
$order_no = $Order['before_trade_no']; // 外部订单号(付款时的 out_trade_no) 原订单号
|
|
$total_fee = $Order['before_amount']; // 付款金额 (元)
|
|
$total_fee = bcmul($total_fee, 100); // 付款金额 (元转分)
|
|
$refund_fee = $Order[$PayOrderModel::TRADE_AMOUNT]; // 退款金额
|
|
$refund_fee = bcmul($refund_fee, 100); // 退款金额 (元转分)
|
|
|
|
$refund_id = $Order[$PayOrderModel::OUT_TRADE_NO]; // 退款订单号
|
|
$res = $CloudPay->refund($order_no, $total_fee, $refund_fee, $refund_id);
|
|
if (!$res) {
|
|
return $this->failCloudPay($CloudPay->getMsg());
|
|
}
|
|
// 查询订单判断是否退款成功
|
|
$result = $CloudPay->refundQuery($refund_id);
|
|
if (!$result) {
|
|
return $this->failCloudPay($CloudPay->getMsg());
|
|
}
|
|
$BranchProfitClass = new BranchProfit();
|
|
$BranchProfitClass->refundBranchProfit([
|
|
'pay_order' => $Order
|
|
]);//分润操作
|
|
return $this->successCloudPay($result, '退款成功');
|
|
}
|
|
|
|
/*支付失败的返回*/
|
|
public function failCloudPay($msg = '支付失败')
|
|
{
|
|
return array('status' => 0, 'msg' => $msg);
|
|
}
|
|
|
|
// 支付成功的返回
|
|
public function successCloudPay($data = array(), $msg = '支付成功')
|
|
{
|
|
return array('status' => 1, 'msg' => $msg, 'data' => $data);
|
|
}
|
|
|
|
/* 处理转换云闪付支付配置*/
|
|
public function retailPayConfigToCloudPayConfig($config)
|
|
{
|
|
$configData = $config['systemConfig'];
|
|
if (!$configData) {
|
|
return false;
|
|
}
|
|
$configData['cloud_mer_id'] = $config['cloud_mer_id'];
|
|
$configData['merid'] = $config['cloud_mer_id'];
|
|
$configData['cloud_sub_appid'] = $config['cloud_sub_appid'];
|
|
$configData['cloud_xcx_appid'] = $config['cloud_xcx_appid'];
|
|
$configData['body'] = $config['body'] ? $config['body'] : '购买商品';
|
|
return $configData;
|
|
}
|
|
|
|
private function doLogs($values, $line = __LINE__)
|
|
{
|
|
if ($this->debug) {
|
|
$data = array(
|
|
'line' => $line,
|
|
'data' => $values
|
|
);
|
|
return $this->write_log($data, __DIR__);
|
|
}
|
|
}
|
|
|
|
public function write_log($values, $dir)
|
|
{
|
|
if (is_array($values))
|
|
$values = print_r($values, true);
|
|
// 日志内容
|
|
$content = '[' . date('Y-m-d H:i:s') . ']' . PHP_EOL . $values . PHP_EOL . PHP_EOL;
|
|
try {
|
|
// 文件路径
|
|
$filePath = $dir . '/logs/';
|
|
// 路径不存在则创建
|
|
!is_dir($filePath) && mkdir($filePath, 0755, true);
|
|
// 写入文件
|
|
return file_put_contents($filePath . date('Ymd') . '.log', $content, FILE_APPEND);
|
|
} catch (\Exception $e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 查询订单状态并更新订单信息
|
|
* @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'], $order['pay_type'] == 4 ? 3 : $order['pay_type']);
|
|
$config = $paySet['config'];
|
|
$configData = $this->retailPayConfigToCloudPayConfig($config);
|
|
if (!$configData) {
|
|
$result['status'] = 0;
|
|
$result['msg'] = '云闪付服务商参数未配置';
|
|
$this->ajaxReturn($result);
|
|
}
|
|
if ($order['pay_type'] == 1) {
|
|
$CloudPay = new CloudWxPayCtrl($configData);
|
|
} else {
|
|
$CloudPay = new CloudAliPayCtrl($configData);
|
|
}
|
|
// 查询订单判断是否支付成功
|
|
$response = $CloudPay->queryOrder($order['out_trade_no']); // 外部订单号
|
|
if (!$response) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = $CloudPay->getMsg();
|
|
} else {
|
|
// 更新订单
|
|
if (isset($response['trade_status']) && $response['trade_status'] == 'TRADE_SUCCESS') {
|
|
|
|
$PayOrderModel = new RetailPayOrderModel();
|
|
$save = array(
|
|
'trade_no' => $response['trade_no'],
|
|
'order_state' => 'SUCCESS',
|
|
'trade_date' => strtotime($response['time_end']),
|
|
'extend_content' => json_encode($response),
|
|
'mer_id' => $config['cloud_mer_id']
|
|
);
|
|
$PayOrderModel->updateOrder(array('id' => $order['id']), $save);
|
|
$res['status'] = 1;
|
|
$res['msg'] = '支付成功';
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '云闪付收款接口成功,但查询验证时失败,请联系服务商登录云闪付后台查看单据状态';
|
|
}
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
|
|
/**
|
|
* 刷卡支付 付款码支付 2020-10-17
|
|
* @param $Order
|
|
* @param $config
|
|
* @param $payType 1 微信 ,2,支付宝
|
|
* @return array
|
|
* @throws \think\Exception
|
|
*/
|
|
public function soltCard_v1($Order, $config, $payType)
|
|
{
|
|
$configData = $this->retailPayConfigToCloudPayConfig($config);
|
|
if (!$configData) {
|
|
return $this->failCloudPay('云闪付服务商参数未配置');
|
|
}
|
|
if ($payType == 1) {
|
|
$CloudPay = new CloudWxPayCtrl($configData);
|
|
} else {
|
|
$CloudPay = new CloudAliPayCtrl($configData);
|
|
}
|
|
$order_no = $Order['out_trade_no']; // 外部订单号
|
|
$total_fee = $Order['trade_amount']; // 付款金额 (元)
|
|
$total_fee = bcmul($total_fee, 100); // 付款金额 (元转分)
|
|
$code = $Order['code']; // 付款码
|
|
$body = $configData['body'] ? $configData['body'] : '购买商品';
|
|
$res = $CloudPay->slotCard_v1($order_no, $code, $total_fee, $body);
|
|
if (!$res) {
|
|
$res = [
|
|
'status' => 0,
|
|
'message' => '支付失败' . $CloudPay->getMsg()
|
|
];
|
|
} else {
|
|
$res = [
|
|
'status' => 1,
|
|
'message' => '发起支付成功'
|
|
];
|
|
}
|
|
return $res;
|
|
}
|
|
} |