Api.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php namespace Pay\Src;
  2. use Dever;
  3. class Api
  4. {
  5. /**
  6. * 发起支付 下单 获取预支付信息
  7. *
  8. * @return mixed
  9. */
  10. public function get($type = 1, $param = array())
  11. {
  12. if (!$type) {
  13. $type = 1;
  14. }
  15. $this->init($param);
  16. if ($this->order_id > 0) {
  17. $pay = Dever::db('pay/order')->one(array('order_id' => $this->order_id));
  18. if ($pay && $pay['status'] == 1 && $pay['param']) {
  19. $order = Dever::array_decode($pay['param']);
  20. return $order;
  21. }
  22. }
  23. return $this->method->order($this->account_id, $this->project_id, $this->uid, $this->username, $this->product_id, $this->name, $this->cash, $this->openid, $type, $this->order_id);
  24. }
  25. /**
  26. * notify
  27. *
  28. * @return mixed
  29. */
  30. public function notify($param = array())
  31. {
  32. $this->account_id = Dever::input('account_id', false);
  33. if (!$this->account_id) {
  34. Dever::alert('没有账户信息');
  35. }
  36. $this->pay();
  37. return $this->method->notify();
  38. }
  39. /**
  40. * 发起支付 用于小程序支付
  41. *
  42. * @return mixed
  43. */
  44. public function applet($param = array())
  45. {
  46. $this->init($param);
  47. return $this->method->applet($this->get(1));
  48. }
  49. /**
  50. * 发起支付 用于app支付
  51. *
  52. * @return mixed
  53. */
  54. public function app($param = array())
  55. {
  56. $this->init($param);
  57. $this->openid = -1;
  58. return $this->method->app($this->get(3));
  59. }
  60. /**
  61. * 发起支付 用于苹果内购支付
  62. *
  63. * @return mixed
  64. */
  65. public function apple($param = array())
  66. {
  67. $this->init($param);
  68. # 只需验证苹果过来的参数即可
  69. return Dever::load('pay/lib/apple')->check($this->other, $this->order_id, $this->cash);
  70. }
  71. /**
  72. * 发起支付 用于页面支付
  73. *
  74. * @return mixed
  75. */
  76. public function page($param = array())
  77. {
  78. $this->init($param);
  79. if (!$this->refer) {
  80. Dever::alert('没有回调refer');
  81. }
  82. return $this->method->page($this->get(1), $this->refer);
  83. }
  84. /**
  85. * 发起支付 用于扫码支付
  86. *
  87. * @return mixed
  88. */
  89. public function qrcode($param = array())
  90. {
  91. $this->init($param);
  92. $url = $this->method->qrcode($this->get(2), $this->refer);
  93. Dever::apply('sdk/qrcode');
  94. return \QRcode::png($url);
  95. }
  96. /**
  97. * 初始化 设置参数
  98. *
  99. * @return mixed
  100. */
  101. private function init($param = array())
  102. {
  103. if (isset($this->account_id)) {
  104. return;
  105. }
  106. $this->account_id = $this->getParam($param, 'account_id');
  107. $this->project_id = $this->getParam($param, 'project_id');
  108. $this->uid = $this->getParam($param, 'uid');
  109. $this->username = $this->getParam($param, 'username');
  110. $this->product_id = $this->getParam($param, 'product_id');
  111. $this->name = $this->getParam($param, 'name');
  112. $this->cash = $this->getParam($param, 'cash');
  113. $this->refer = $this->getParam($param, 'refer');
  114. $this->order_id = $this->getParam($param, 'order_id');
  115. $this->openid = $this->getParam($param, 'openid');
  116. $this->other = $this->getParam($param, 'other');
  117. if (!$this->project_id) {
  118. $this->project_id = false;
  119. }
  120. if (!$this->order_id) {
  121. $this->order_id = false;
  122. }
  123. if (!$this->account_id) {
  124. Dever::alert('没有账户信息');
  125. }
  126. if (!$this->uid || !$this->username) {
  127. Dever::alert('没有用户信息');
  128. }
  129. if (!$this->product_id) {
  130. Dever::alert('没有产品信息');
  131. }
  132. if (!$this->name) {
  133. Dever::alert('没有支付信息');
  134. }
  135. if (!$this->cash) {
  136. Dever::alert('没有支付金额');
  137. }
  138. return $this->pay();
  139. }
  140. /**
  141. * 初始化
  142. *
  143. * @return mixed
  144. */
  145. private function getParam($param, $key)
  146. {
  147. if (isset($param[$key])) {
  148. return $param[$key];
  149. }
  150. return Dever::input($key, false);
  151. }
  152. /**
  153. * 获取支付类
  154. *
  155. * @return mixed
  156. */
  157. private function pay()
  158. {
  159. $pay = Dever::db('pay/account')->one($this->account_id);
  160. $method = '\\Pay\\Lib\\' . ucwords($pay['type']);
  161. $this->method = new $method($pay);
  162. return $this;
  163. }
  164. }