123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace App\Services\Common;
- use Aix\Pay\AixPay;
- use Aix\Pay\Data\PayOrder;
- use App\Exceptions\ResponseException;
- use App\Jobs\PayHookJob;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Cache;
- /**
- * 支付服务.
- * Class SmsService
- * @package App\Services\Common
- */
- class PayService
- {
- public function pay($platform, $type, PayOrder $payOrder)
- {
- if (Cache::has($payOrder->trade_no)) {
- return Cache::get($payOrder->trade_no);
- }
- $checkOrder=PayOrder::where('trade_no', $payOrder->trade_no)->first();
- if ($checkOrder) {
- $checkOrder->subject=$payOrder->subject;
- $checkOrder->detail=$payOrder->detail;
- $checkOrder->price=$payOrder->price;
- if (!isset($payOrder->openid)) {
- $checkOrder->openid=$payOrder->openid;
- }
- if (!isset($payOrder->attch)) {
- $checkOrder->attch=$payOrder->attch;
- }
- $payOrder=$checkOrder;
- }
- $payOrder->pay_platform = $platform;
- $payOrder->pay_type = $type;
- $result=AixPay::$platform()->$type($payOrder);
- if (isset($payOrder->return_url)) {
- unset($payOrder->return_url);
- }
- $payOrder->save();
- Cache::put($payOrder->trade_no, $result, 60);
- return $result;
- }
- public function payHook($platform)
- {
- $pay=AixPay::$platform();
- try {
- $data=$pay->verify();
- } catch (\Exception $e) {
- //
- }
- $job=new PayHookJob($platform, $data->all());
- dispatch($job);
- return $pay->success();
- }
- /**
- * 触发线下支付回调
- * @param array $data :
- * [
- * "out_trade_no"=>"订单编号",
- * "operate_admin_id"=>"操作管理员id",
- * "operate_time"=>"操作时间"
- * ]
- * @return bool
- */
- public function offlineHook($data)
- {
- $job=new PayHookJob('offline', $data);
- dispatch_now($job);
- return true;
- }
- public function getTradeNo($id)
- {
- // $carbon = Carbon::now();
- // return $carbon->year . sprintf('%02d', $carbon->month) . sprintf('%02d', $carbon->day)
- // . rand(1000, 9999)
- // . sprintf('%05d', ($carbon->hour * 3600 + $carbon->minute * 60 + $carbon->second))
- // . $id;
- return time().rand(10, 99).$id;
- }
- public function close($trade_no)
- {
- //
- }
- public function testhook(PayOrder $payOrder)
- {
- $payOrder->attch="回调收到";
- $payOrder->save();
- }
- public function getPayOpenid()
- {
- if (session()->has('wechat_pay_openid')) {
- return session('wechat_pay_openid');
- }
- throw new ResponseException("openid不存在");
- }
- }
|