sifangpay/app/ServicePay/GuMaPayServices.php

321 lines
9.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\ServicePay;
use App\Models\Merchant;
use App\Models\Message;
use App\ServicePay\traits\ReturnTrait;
use App\ServicePay\TransCard\BaseCard;
use App\ServicePay\TransCard\GetTransInfo;
use Illuminate\Support\Facades\Log;
class GuMaPayServices implements PayApiInterface
{
use PayTrait;
use ReturnTrait;
public $gateway_list;//当前类型的支付通道列表
public $pay_type;//支付类型
public $pay_method;//支付方法
public $pay_gateway;//当前的支付通道实例
public $pay_config;//这个通道的支付配置信息
public $pay_model;//支付接口实例模型
public $order;
public $request;
public $gateway_id;
public $pay_data;
public $log_name;
public static $result;
public $timeOut=15;
//支付第一步getGateway
//第二payConfig
//3:pay
/**
* 检查通道是否存在错误
*/
public function checkGatewayConfig()
{
$pay_method = $this->pay_method;
if (count($this->gateway_list) <= 0) {
self::addError(['msg' => '通道支付不存在']);
}
if (!isset($this->gateway_list[$pay_method])) {
self::addError(['msg' => '通道支付类型不存在']);
}
}
/**
* 支付包配置实列化
* @param $type
* @param array $setConfig
* @return $this
*/
public function payConfig($type, $setConfig = [])
{
$this->pay_config = [
'username'=>$this->gateway['username'],
'source'=>$this->gateway['source'],
'url'=>$this->gateway['url'],
'app_id'=>$this->pay_gateway['app_id'],
'mch_id'=>$this->gateway['mch_id'],//收款 银行卡号
'token' => $this->pay_gateway['app_key'],//密钥
];
$this->pay_model=$this->pay_config;
return $this;
}
//回调指定通道配置信息gateway
public function getConfig($gateway_id, $pay_method)
{
$this->pay_method = $pay_method;
return $this->pay_model = config('gateway.config')[$gateway_id];
}
public function getPayConfig()
{
return $this->pay_config;
}
public function pay($pay_data)
{
$return = request()->all();
$re_type = 'json';
if (isset($return['re_type'])) {
$re_type = $return['re_type'];
}
if (isset($return['app_id'])) {
$host = request()->getSchemeAndHttpHost();
$url = 'order/show?out_trade_sn=' . $this->order->out_trade_sn;
$url = $host . '/' . $url;
return $this->returnPayRes($re_type, '', 1, $url);
}
//订单开始时间,倒计时
$time_only = ((strtotime($this->order->created_at) + $this->timeOut * 60) - time());
$config=config('adconfig.pay_type');
$data = [
'gateway'=>$this->pay_gateway,
'title' => $config[$this->order->pay_type],
'pay_name' => $config[$this->order->pay_type],
'bank' => $this->pay_gateway,
'order' => $this->order,
'pay_type' => $this->order->pay_type,
'is_mobile' => is_mobile_client(),
'pay_status' => $this->order->pay_status == 1 ? '已支付' : '等待支付',
'only_time' =>$time_only
];
$lange = $this->order->language ?? 'zh_cn';
$appPath = base_path();
$dir = $appPath . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'web' . DIRECTORY_SEPARATOR . 'order' . DIRECTORY_SEPARATOR;
$file = $lange == 'zh_cn' ? '' : $lange;
if ($file != '') {
$filedir = $dir . $lange . DIRECTORY_SEPARATOR . 'code.blade.php';
} else {
$filedir = $dir . 'code.blade.php';
}
if ($file!='' && file_exists($filedir)) {
return view('web.order.' . $lange . '.code', $data);
} else {
return view('web.order.code', $data);
}
}
/*
* 验证签名
*/
public function verify()
{
$params = $this->result_data;
return true;
$allData = $this->request->all();
if (isset($allData['token'])) {
if ($this->pay_config['app_id'] == $allData['token']) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public function setLogName($pay_type)
{
$name = '';
switch ($pay_type) {
case 'alipay':
$name = 'alipay_';
break;
case 'weixin':
$name = 'weixin_';
break;
}
$this->log_name = $name;
}
/**
* 取得订单,需要分开处理
* @param GetTransInfo $getTrans
* @param array $requestArr
* @return false|void
*/
private function getOrderFromMsg(GetTransInfo $getTrans, array $requestArr=[])
{
//匹配阿拉伯短信
$this->order=$getTrans->getOrderInfo($requestArr);
//订单不存在返回空
if (empty($this->order)) {
$this->debugLog('订单不存在:'.json_encode($getTrans->getInfo()) ,$requestArr);
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);
}
//银行卡回调处理
public function cardNotify($return, $pay_type, $request, BaseCard $card)
{
$allData = $request->all();
$this->setLogName($pay_type);
$this->debugLog('回调原始数据', $allData);
(new Message())->addMsg($allData);//存入数据库
$card->message($allData['content']);
$getTrans = new GetTransInfo($card);
$requestArr = $getTrans->getInfo();
if (!is_array($requestArr)) {
$this->debugLog('短信参数异常');
return '短信参数异常';
}
$this->pay_type = $pay_type;
$this->request = $request;
//取得订单,是否有此订单,如果没有直接返回空
//order_money
$ewm_account = $requestArr['from_card'];
if (is_null($ewm_account)) {
return '参数异常';
}
//取得订单
$this->getOrderFromMsg($getTrans, []);
if (empty($this->order)) {
return '订单不存在';
}
$return = ($return) ? 1 : 0;
if ($return) {
//判断是否是商户id
if ($this->order->merchant_id == 0) {
return redirect()->route('web.user.index');
}
//
return $this->merchantReturnSend();
}
//取得通道id
$this->gateway = $this->pay_gateway = config('gateway.config')[$this->gateway_id];
//取得支付配置
$this->payConfig($this->pay_type);
$this->result_data = $requestArr;
$this->result_data['order_sn'] = $this->order->order_sn;
//进行验证签名
if (!$this->verify()) {
$this->debugLog('签名以及支付状态验证失败');
return '签名以及支付状态验证失败';
}
return $this->payNotify();
}
public function notify($return, $pay_type, $request)
{
return '不支持';
}
/**
* 获得通道信息,取得支付配置模型,验证签名,
* @return bool|void
*/
private function payNotify()
{
//取得通道信息内容
$data = $this->result_data;
$this->pay_data = $this->result_data;
//如果应答状态不是01/A6表示支付失败
$this->debugLog(json_encode($data, true));
$this->debugLog('-------------' . $this->order->order_sn . '--选择----' . $this->pay_type . '----' . $this->pay_method . '----开始回调处理-------------------------');
if (empty($data)) {
return $this->debugLog('验证签名失败', $data);
}
try {
$pay_cn_name = '';
//统一格式化
$this->unionHandle($data);
//支付返回信息
$this->pay_data = $data;
$this->debugLog('验证签名成功,数据', $data);
//更新订单
$this->updateOrder();
$this->debugLog('updateOrder Ok');
} catch (\Exception $exception) {
$this->debugLog('失败异常内容:' . json_encode($exception->getTrace()));
$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)]);
}
}
/**
* 统一格式化
*/
private function unionHandle($data)
{
$result = [
'order_sn' => $data['order_sn'],//我方订单号
'pay_order_sn' => $data['order_number'],//三方的订单号
'pay_money' => $data['pay_money'], //支付金额,
'money' => $data['pay_money'],
'pay_type' => $this->pay_type,
'ewm_name' => $data['from_name'],
'account' => ''
];
$this->setResult($result);
Log::channel('pay_success')->info('支付成功', [
'pay_type' => $result['pay_type'], 'pay_money' => $result['pay_money'],
'account' => $result['account'],
'pay_order_sn' => $result['pay_order_sn']]);
}
public function success()
{
echo 'success';
exit;
}
}