309 lines
10 KiB
PHP
309 lines
10 KiB
PHP
<?php
|
||
|
||
|
||
namespace App\ServicePay;
|
||
|
||
use App\Models\Merchant;
|
||
use App\Models\Order;
|
||
use App\Models\UploadQrcode;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Illuminate\Support\Collection;
|
||
use Illuminate\Http\Request;
|
||
|
||
class ZhanXinPayServices implements PayApiInterface
|
||
{
|
||
use PayTrait;
|
||
use ThirdPaySignTrait;
|
||
|
||
public $gateway_list;//当前类型的支付通道列表
|
||
public $pay_type;//支付类型
|
||
public $pay_method;//支付方法
|
||
public $pay_gateway;//当前的支付通道实例
|
||
public $pay_config;//这个通道的支付配置信息
|
||
public $order;
|
||
public $request;
|
||
public $gateway_id;
|
||
public $pay_data;
|
||
public $log_name;
|
||
public $result_data;
|
||
public $qrcodeInfo;
|
||
public static $result;
|
||
|
||
|
||
//支付发起第一步:取得通道getGateway,随机通道,第二部配置支付信息,payConfig,第三部发起支付
|
||
|
||
//第二部配置支付信息,payConfig
|
||
public function payConfig($type, $setConfig = [])
|
||
{
|
||
$config = [
|
||
'url' => 'http://pay.bypay1688.com/pay/Preorder/alipay',
|
||
'debug' => 1,//调试
|
||
'logDir' => to_linux_path(storage_path() . '/logs/'),
|
||
'mch_id' => $this->pay_gateway['app_id'], //客户交易者账号
|
||
'token' => $this->pay_gateway['app_key'],
|
||
'interface' => $this->pay_gateway['mch_id'],
|
||
'return_url' => action('Pay\PayController@returnNotify', ['type' => $type]),
|
||
'notify_url' => action('Pay\PayController@notify', ['type' => $type]),
|
||
];
|
||
$this->pay_config = $config;
|
||
}
|
||
|
||
//统一下单支付
|
||
public function pay($pay_data)
|
||
{
|
||
//商户号
|
||
$accountId = $this->pay_config['mch_id'];
|
||
//商户秘钥
|
||
// $key = $this->pay_config['token'];
|
||
// $payData = [
|
||
// 'cpid' => $accountId,//商户
|
||
// 'orderid' => strval($pay_data['out_trade_no']),//订单号
|
||
// 'describe' => $pay_data['subject'],
|
||
// 'product' => mt_rand(100000, 999999), //商品名称
|
||
// 'amount' => intval($pay_data['total_amount']), //订单金额(元)
|
||
//// 'pay_bankcode' => $this->pay_gateway['mch_id'],//对应的三方通道
|
||
// 'ip' => request()->getClientIp(),
|
||
// 'synurl' => strval($this->pay_config['notify_url']),// //异步回调地址
|
||
// 'jumpurl' => strval($this->pay_config['return_url']),// //页面跳转通知
|
||
// ];
|
||
|
||
$data = [
|
||
'url' => $this->qrcodeInfo->thumb,
|
||
'order' => $this->qrcodeInfo->id,
|
||
'username' => $this->qrcodeInfo->username,
|
||
'pic_content' => $this->qrcodeInfo->pic_content,
|
||
'type_name' => '展信支付'
|
||
];
|
||
return view('web.zhanxin.scan', $data);
|
||
}
|
||
|
||
//统一验证签名
|
||
public function verify(): bool
|
||
{
|
||
return true;
|
||
}
|
||
|
||
|
||
//返回给支付商的成功
|
||
public function success()
|
||
{
|
||
echo 'SUCCESS';
|
||
exit;
|
||
}
|
||
|
||
//统一回调处理
|
||
|
||
/**
|
||
* 回调第一部,如果是同步,直接回传
|
||
* @param $return
|
||
* @param $pay_type
|
||
* @param $request
|
||
* @deprecated
|
||
* @return bool|\Illuminate\Http\RedirectResponse
|
||
*/
|
||
public function notify($return, $pay_type, $request)
|
||
{
|
||
$this->setLogName($pay_type);
|
||
Log::channel('pay_order')->info('回调原始数据', $request->all());
|
||
$this->pay_type = 'zhanxinpay';
|
||
$this->request = $request;
|
||
|
||
//取得订单,是否有此订单,如果没有直接返回空
|
||
$order_sn = $request->input('orderId');
|
||
//检查回调订单状态
|
||
//判断有没支付成功先,没有的话,直接就没有下一步了
|
||
$pay_status = $request->input('pay_msg');
|
||
if ($pay_status != 'SUCCESS') {
|
||
$this->debugLog('订单支付失败', $this->request->all());
|
||
return '订单支付失败';
|
||
}
|
||
//取得订单
|
||
$this->getOrder($order_sn);
|
||
|
||
$return = ($return) ? 1 : 0;
|
||
if ($return) {
|
||
|
||
//判断是否是商户id
|
||
if ($this->order->merchant_id == 0) {
|
||
return redirect()->route('web.user.index');
|
||
}
|
||
//
|
||
return $this->merchantReturnSend();
|
||
}
|
||
|
||
/* $this->order = Order::where('order_sn', $order_sn)->first();
|
||
if(empty($this->order))
|
||
{
|
||
return false;
|
||
}*/
|
||
//取得通道id
|
||
$this->gateway = $this->pay_gateway = config('gateway.config')[$this->gateway_id];
|
||
//dd($this->gateway);
|
||
//取得支付配置
|
||
$this->payConfig($this->pay_type);
|
||
|
||
$this->result_data = $request->all();
|
||
//检查三方的订单
|
||
|
||
//dump($this->result_data);
|
||
//进行验证签名
|
||
if (!$this->verify()) {
|
||
$this->debugLog('签名验证失败');
|
||
return '签名验证失败';
|
||
}
|
||
|
||
return $this->payNotify();
|
||
}
|
||
|
||
|
||
/**
|
||
* 同步回调
|
||
* @return \Illuminate\Http\RedirectResponse
|
||
*/
|
||
public function returnPay()
|
||
{
|
||
//判断是否是商户id
|
||
if ($this->order->merchant_id == 0) {
|
||
return redirect()->route('web.user.index');
|
||
}
|
||
//
|
||
return $this->merchantReturnSend();
|
||
}
|
||
|
||
//异步回调
|
||
|
||
/**
|
||
* 获得通道信息,取得支付配置模型,验证签名,
|
||
* @return bool|void
|
||
*/
|
||
private function payNotify()
|
||
{
|
||
//取得通道信息内容
|
||
$data = $this->result_data;
|
||
//如果应答状态不是01/A6表示支付失败
|
||
|
||
$this->debugLog('-------------' . $this->order->order_sn . '--选择----' . $this->pay_type . '----' . $this->pay_method . '----开始回调处理-------------------------');
|
||
|
||
try {
|
||
$pay_cn_name = '';
|
||
//统一格式化
|
||
$this->unionHandle(['order_sn'=>$this->order->order_sn,'money'=>$data['money'],'orderId'=>$data['orderid']]);
|
||
//支付返回信息
|
||
$this->pay_data = $data;
|
||
$this->debugLog('验证签名成功,数据', $data);
|
||
//更新订单
|
||
$this->updateOrder();
|
||
$this->debugLog('updateOrder Ok');
|
||
|
||
} catch (\Exception $exception) {
|
||
$this->debugLog('失败异常内容:' . $exception->getMessage());
|
||
|
||
$this->order->notify_status = 1;//支付回调成功
|
||
$this->order->pay_status = 1;//支付状态
|
||
$this->order->pay_ok_at = date('Y-m-d H:i:s');
|
||
$this->order->save();//保存
|
||
Log::channel('pay_success')->info($pay_cn_name . '支付成功,但处理订单失败', ['data' => json_encode($data)]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 统一格式化
|
||
*/
|
||
public function unionHandle($data)
|
||
{
|
||
|
||
$result = [
|
||
'order_sn' => $data['orderId'],//我方订单号
|
||
'pay_order_sn' => $data['orderId'],//三方的订单号
|
||
'pay_money' => $data['money'],
|
||
'pay_type' => 'zhanxinpay',
|
||
'account' => ''
|
||
];
|
||
|
||
$this->setResult($result);
|
||
Log::channel('pay_success')->info('支付成功', [
|
||
'pay_type' => $result['pay_type'], 'pay_money' => $result['pay_money'],
|
||
'account' => $result['account'], 'order_sn' => $result['order_sn'],
|
||
'pay_order_sn' => $result['pay_order_sn']]);
|
||
|
||
}
|
||
|
||
|
||
private function getOrder($order_sn)
|
||
{
|
||
$this->order = Order::where('out_trade_sn', $order_sn)->first();
|
||
//订单不存在返回空
|
||
if (empty($this->order)) {
|
||
$this->debugLog('订单不存在:' . $order_sn);
|
||
return false;
|
||
}
|
||
$this->pay_method = $this->order->gateway_method;//支付方法
|
||
$this->gateway_id = $this->order->gateway_id;//通道ID
|
||
$this->merchant = Merchant::find($this->order->merchant_id);
|
||
}
|
||
|
||
//ineed回调验证完后执行这个方法
|
||
public function afterPay($notifyMoney){
|
||
$pay_type='zhanxinpay';
|
||
|
||
$this->setLogName($pay_type);
|
||
Log::channel('zhanxin')->info('回调原始数据', $notifyMoney);
|
||
$this->pay_type = $pay_type;
|
||
|
||
$this->result_data = $notifyMoney;
|
||
//取得订单,是否有此订单,如果没有直接返回空
|
||
$order_sn = $notifyMoney['out_trade_sn'];
|
||
//检查回调订单状态
|
||
//判断有没支付成功先,没有的话,直接就没有下一步了
|
||
|
||
|
||
// dd($this->order);
|
||
// var_dump($this->order);exit;
|
||
Log::channel('zhanxin')->info('订单数据', $this->order->toArray());
|
||
//取得订单
|
||
$this->getOrder($order_sn);
|
||
if ($notifyMoney['money'] != $this->order->order_money) {
|
||
Log::channel('zhanxin')->info('支付金额不一致', []);
|
||
return '支付金额不一致';
|
||
}
|
||
//取得通道id
|
||
$this->gateway = $this->pay_gateway = config('gateway.config')[$this->gateway_id];
|
||
//dd($this->gateway);
|
||
//取得支付配置
|
||
$this->payConfig($this->pay_type);
|
||
|
||
//dump($this->result_data);
|
||
//进行验证签名
|
||
if (!$this->verify()) {
|
||
$this->debugLog('签名验证失败');
|
||
return '签名验证失败';
|
||
}
|
||
|
||
return $this->payNotify();
|
||
}
|
||
|
||
//这个在pay 之前执行
|
||
public function beforeCreateOrder($orderData){
|
||
$merchant = \App\Models\Merchant::where('id', $orderData['merchant_id'])->first();
|
||
if ($merchant && isset($merchant['total_price']) && !is_null($merchant['total_price'])) {
|
||
$walts = \App\Models\MerchantWallet::where('merchant_id', $orderData['merchant_id'])->first();
|
||
if ($walts->trans_money + $orderData['order_money'] > $merchant['total_price']) {
|
||
return $this->msg(['error' => 1, 'msg' => '额度不足']);
|
||
}
|
||
}
|
||
$qrcode = new UploadQrcode();
|
||
$picinfo = $qrcode->findOneQrCodeByPay($orderData);//查询一个可用的二维码
|
||
$this->qrcodeInfo = $picinfo;
|
||
}
|
||
|
||
|
||
/**
|
||
* 这个调试需要配置在config/logging.php
|
||
* @param $pay_type
|
||
*/
|
||
public function setLogName($pay_type)
|
||
{
|
||
$name = 'zhanxinpay_';
|
||
$this->log_name = $name;
|
||
}
|
||
} |