WxPayServices.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * 微信支付
  4. */
  5. namespace app\api\services;
  6. use utils\RedisCache;
  7. use utils\WxPay;
  8. /**
  9. * Class WxPayServices
  10. * @package app\services
  11. * @method $this data($data)
  12. * @method $this trade_type(string $trade_type)
  13. * @method $this uid(int $uid)
  14. */
  15. class WxPayServices extends BasePayServices
  16. {
  17. /**
  18. * 用户id
  19. * @var
  20. */
  21. protected $uid;
  22. /**
  23. * 数据体
  24. * @var
  25. */
  26. protected $data;
  27. protected static $instance = null;
  28. public static function instance ()
  29. {
  30. if (is_null(self::$instance)) {
  31. self::$instance = new static();
  32. }
  33. return self::$instance;
  34. }
  35. /**
  36. * 微信统一下单
  37. * @return mixed
  38. */
  39. public function getUnifiedOrder ()
  40. {
  41. $uid = $this->uid;
  42. if (!$uid || !$this->data){
  43. return app('json')->json_error('下单失败');
  44. }
  45. list($body, $total_amount, $order_type, $remarks, $trade_type, $pay_way) = $this->_payConf($uid, $this->data, 2);
  46. $out_trade_no = rand(1000000000, 9999999999) . (string)date('ymdhis', time()) . (int)microtime(true); // 订单号
  47. /** @var WxPay $pay */
  48. $cacheKey = "caches:payment:wxPay:otn_{$out_trade_no}:";
  49. RedisCache::set($cacheKey.'params',['data'=> $this->data,'params'=>['uid'=>$uid,'body'=>$body,'out_trade_no'=>$out_trade_no,'total_amount'=>$total_amount]], 7200);
  50. $pay = app()->make(WxPay::class);
  51. $rpm = $pay->unifiedOrder(compact('uid', 'body', 'out_trade_no', 'total_amount'));
  52. RedisCache::set($cacheKey.'result',['data'=> $this->data,'result'=>$rpm,'params'=>['uid'=>$uid,'body'=>$body,'out_trade_no'=>$out_trade_no,'total_amount'=>$total_amount]], 7200);
  53. if (!$rpm['flag']) {
  54. return app('json')->json_error($rpm['msg']);
  55. }
  56. $this->setPaymentOrder(compact('body', 'out_trade_no', 'total_amount', 'remarks', 'uid', 'trade_type', 'order_type', 'pay_way'), 2);
  57. return app('json')->json_success('微信统一下单成功', $rpm['data']);
  58. }
  59. /**
  60. * 支付回调
  61. * @return $this
  62. */
  63. public function getNotifyInfo ()
  64. {
  65. $pay = new WxPay();
  66. if ($pay->checkSign($this->data)) {
  67. if ($this->data['trade_status'] == 'TRADE_SUCCESS') {
  68. $this->notify_info = $this->data;
  69. $this->pay_status = true;
  70. $this->pay_way = 2;
  71. }
  72. }
  73. return $this;
  74. }
  75. /**
  76. * 魔术方法
  77. * @param $name
  78. * @param $arguments
  79. * @return $this
  80. */
  81. public function __call ($name, $arguments)
  82. {
  83. // TODO: Implement __call() method.
  84. if ($name == 'data') {
  85. $this->{$name} = $arguments[0];
  86. } else {
  87. $this->{$name} = $arguments[0];
  88. }
  89. return $this;
  90. }
  91. }