PayService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Services\Common;
  3. use Aix\Pay\AixPay;
  4. use Aix\Pay\Data\PayOrder;
  5. use App\Exceptions\ResponseException;
  6. use App\Jobs\PayHookJob;
  7. use Carbon\Carbon;
  8. use Illuminate\Support\Facades\Cache;
  9. /**
  10. * 支付服务.
  11. * Class SmsService
  12. * @package App\Services\Common
  13. */
  14. class PayService
  15. {
  16. public function pay($platform, $type, PayOrder $payOrder)
  17. {
  18. if (Cache::has($payOrder->trade_no)) {
  19. return Cache::get($payOrder->trade_no);
  20. }
  21. $checkOrder=PayOrder::where('trade_no', $payOrder->trade_no)->first();
  22. if ($checkOrder) {
  23. $checkOrder->subject=$payOrder->subject;
  24. $checkOrder->detail=$payOrder->detail;
  25. $checkOrder->price=$payOrder->price;
  26. if (!isset($payOrder->openid)) {
  27. $checkOrder->openid=$payOrder->openid;
  28. }
  29. if (!isset($payOrder->attch)) {
  30. $checkOrder->attch=$payOrder->attch;
  31. }
  32. $payOrder=$checkOrder;
  33. }
  34. $payOrder->pay_platform = $platform;
  35. $payOrder->pay_type = $type;
  36. $result=AixPay::$platform()->$type($payOrder);
  37. if (isset($payOrder->return_url)) {
  38. unset($payOrder->return_url);
  39. }
  40. $payOrder->save();
  41. Cache::put($payOrder->trade_no, $result, 60);
  42. return $result;
  43. }
  44. public function payHook($platform)
  45. {
  46. $pay=AixPay::$platform();
  47. try {
  48. $data=$pay->verify();
  49. } catch (\Exception $e) {
  50. //
  51. }
  52. $job=new PayHookJob($platform, $data->all());
  53. dispatch($job);
  54. return $pay->success();
  55. }
  56. /**
  57. * 触发线下支付回调
  58. * @param array $data :
  59. * [
  60. * "out_trade_no"=>"订单编号",
  61. * "operate_admin_id"=>"操作管理员id",
  62. * "operate_time"=>"操作时间"
  63. * ]
  64. * @return bool
  65. */
  66. public function offlineHook($data)
  67. {
  68. $job=new PayHookJob('offline', $data);
  69. dispatch_now($job);
  70. return true;
  71. }
  72. public function getTradeNo($id)
  73. {
  74. // $carbon = Carbon::now();
  75. // return $carbon->year . sprintf('%02d', $carbon->month) . sprintf('%02d', $carbon->day)
  76. // . rand(1000, 9999)
  77. // . sprintf('%05d', ($carbon->hour * 3600 + $carbon->minute * 60 + $carbon->second))
  78. // . $id;
  79. return time().rand(10, 99).$id;
  80. }
  81. public function close($trade_no)
  82. {
  83. //
  84. }
  85. public function testhook(PayOrder $payOrder)
  86. {
  87. $payOrder->attch="回调收到";
  88. $payOrder->save();
  89. }
  90. public function getPayOpenid()
  91. {
  92. if (session()->has('wechat_pay_openid')) {
  93. return session('wechat_pay_openid');
  94. }
  95. throw new ResponseException("openid不存在");
  96. }
  97. }