WxPayServices.php 2.3 KB

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