Api.php 5.2 KB

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