Api.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. public function channel()
  18. {
  19. $data = Dever::db('pay/channel')->getData();
  20. return $data;
  21. }
  22. /**
  23. * 发起支付 下单 获取预支付信息
  24. *
  25. * @return mixed
  26. */
  27. public function get($type = 1, $param = array())
  28. {
  29. if (!$type) {
  30. $type = 1;
  31. }
  32. $this->init($param);
  33. if ($this->order_id > 0) {
  34. $pay = Dever::db('pay/order')->one(array('order_id' => $this->order_id));
  35. if ($pay && $pay['status'] == 1 && $pay['param']) {
  36. $order = Dever::array_decode($pay['param']);
  37. return $order;
  38. }
  39. }
  40. 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);
  41. }
  42. /**
  43. * notify
  44. *
  45. * @return mixed
  46. */
  47. public function notify($param = array())
  48. {
  49. $this->account_id = Dever::input('account_id', false);
  50. if (!$this->account_id) {
  51. Dever::alert('没有账户信息');
  52. }
  53. $this->handle();
  54. return $this->method->notify();
  55. }
  56. /**
  57. * 发起支付 通用的发起支付方法
  58. *
  59. * @return mixed
  60. */
  61. public function pay($param = array())
  62. {
  63. $system_source = $this->getParam($param, 'system_source');
  64. $receipt = $this->getParam($param, 'receipt');
  65. $account_id = $this->getParam($param, 'account_id');
  66. $config = Dever::config('base', 'pay')->pay['method'];
  67. if (isset($config[$system_source])) {
  68. $param['account_id'] = $account_id ? $account_id : $system_source;
  69. $method = $config[$system_source];
  70. if ($system_source == 3 && $receipt) {
  71. $method = $method[1];
  72. $param['other'] = $receipt;
  73. }
  74. $result = array();
  75. $result['order'] = $this->$method($param);
  76. $result['order_id'] = $this->method->order_id;
  77. $result['type'] = $this->type;
  78. return $result;
  79. } else {
  80. Dever::alert('错误的source');
  81. }
  82. }
  83. /**
  84. * 发起支付 用于小程序支付
  85. *
  86. * @return mixed
  87. */
  88. public function applet($param = array())
  89. {
  90. $param['system_source'] = 4;
  91. $this->init($param);
  92. return $this->method->applet($this->get(1));
  93. }
  94. /**
  95. * 发起支付 用于app支付
  96. *
  97. * @return mixed
  98. */
  99. public function app($param = array())
  100. {
  101. $param['system_source'] = 1;
  102. $this->init($param);
  103. $this->openid = -1;
  104. return $this->method->app($this->get(3));
  105. }
  106. /**
  107. * 发起支付 用于苹果内购支付
  108. *
  109. * @return mixed
  110. */
  111. public function apple($param = array())
  112. {
  113. $this->init($param);
  114. # 只需验证苹果过来的参数即可
  115. 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);
  116. }
  117. /**
  118. * 发起支付 用于页面支付
  119. *
  120. * @return mixed
  121. */
  122. public function page($param = array())
  123. {
  124. $this->init($param);
  125. if (!$this->refer && $this->type != 'test') {
  126. Dever::alert('没有回调refer');
  127. }
  128. $type = 1;
  129. if ($this->h5 == 1) {
  130. $type = 4;
  131. }
  132. return $this->method->page($this->get($type), $this->refer);
  133. }
  134. /**
  135. * 发起支付 用于扫码支付
  136. *
  137. * @return mixed
  138. */
  139. public function qrcode($param = array())
  140. {
  141. $this->init($param);
  142. return $this->method->qrcode($this->get(2), $this->refer);
  143. Dever::apply('sdk/qrcode');
  144. return \QRcode::png($url);
  145. }
  146. /**
  147. * 查询支付
  148. *
  149. * @return mixed
  150. */
  151. public function search($param = array())
  152. {
  153. $this->account_id = Dever::input('account_id', false);
  154. if (!$this->account_id) {
  155. Dever::alert('没有账户信息');
  156. }
  157. $this->handle();
  158. $this->order_id = $this->getParam($param, 'order_id');
  159. return $this->method->search($this->order_id);
  160. }
  161. /**
  162. * 退款
  163. *
  164. * @return mixed
  165. */
  166. public function refund($param = array())
  167. {
  168. $this->account_id = Dever::input('account_id', false);
  169. if (!$this->account_id) {
  170. Dever::alert('没有账户信息');
  171. }
  172. $this->handle();
  173. $this->order_id = $this->getParam($param, 'order_id');
  174. return $this->method->refundByOrder($this->order_id);
  175. }
  176. /**
  177. * 初始化 设置参数
  178. *
  179. * @return mixed
  180. */
  181. private function init($param = array())
  182. {
  183. if (isset($this->account_id)) {
  184. return;
  185. }
  186. $this->channel_id = $this->getParam($param, 'channel_id');
  187. $this->system_source = $this->getParam($param, 'system_source');
  188. $this->account_id = $this->getParam($param, 'account_id');
  189. $this->project_id = $this->getParam($param, 'project_id');
  190. $this->uid = $this->getParam($param, 'uid');
  191. $this->openid = $this->getParam($param, 'openid');
  192. $this->username = $this->getParam($param, 'username');
  193. $this->product_id = $this->getParam($param, 'product_id');
  194. $this->name = $this->getParam($param, 'name');
  195. $this->cash = $this->getParam($param, 'cash');
  196. $this->refer = $this->getParam($param, 'refer');
  197. $this->order_id = $this->getParam($param, 'order_id');
  198. $this->other = $this->getParam($param, 'other');
  199. $this->h5 = $this->getParam($param, 'h5');
  200. $this->ip = $this->getParam($param, 'ip');
  201. if (!$this->project_id) {
  202. $this->project_id = false;
  203. }
  204. if (!$this->order_id) {
  205. $this->order_id = false;
  206. }
  207. if (!$this->account_id && !$this->channel_id && !$this->system_source) {
  208. Dever::alert('没有账户信息');
  209. }
  210. if (!$this->uid) {
  211. Dever::alert('没有用户信息');
  212. }
  213. if (!$this->product_id) {
  214. Dever::alert('没有产品信息');
  215. }
  216. if (!$this->name) {
  217. Dever::alert('没有支付信息');
  218. }
  219. if (!$this->cash) {
  220. Dever::alert('没有支付金额');
  221. }
  222. if (!$this->ip) {
  223. $this->ip = Dever::ip();
  224. }
  225. return $this->handle();
  226. }
  227. /**
  228. * 初始化
  229. *
  230. * @return mixed
  231. */
  232. private function getParam($param, $key)
  233. {
  234. if (isset($param[$key])) {
  235. return $param[$key];
  236. }
  237. return Dever::input($key, false);
  238. }
  239. /**
  240. * 获取支付类
  241. *
  242. * @return mixed
  243. */
  244. private function handle()
  245. {
  246. $pay = false;
  247. if ($this->account_id > 0) {
  248. $pay = Dever::db('pay/account')->one($this->account_id);
  249. } elseif ($this->channel_id && $this->system_source) {
  250. $pay = Dever::db('pay/account')->one(array('channel_id' => $channel_id, 'system_source' => $this->system_source));
  251. }
  252. if (!$pay || ($pay && $pay['state'] != 1)) {
  253. Dever::alert('没有账户信息');
  254. }
  255. if (!$pay['type']) {
  256. Dever::alert('账户类型错误');
  257. }
  258. $this->account_id = $pay['id'];
  259. /*
  260. $this->channel_id = $pay['channel_id'];
  261. $this->system_source = $pay['system_source'];
  262. */
  263. if ($pay['box'] == 3) {
  264. $pay['type'] = 'test';
  265. }
  266. $this->type = $pay['type'];
  267. $method = '\\Pay\\Lib\\' . ucwords($pay['type']);
  268. if (isset($this->refer) && $this->refer) {
  269. $pay['refer'] = $this->refer;
  270. }
  271. if (isset($this->ip) && $this->ip) {
  272. $pay['ip'] = $this->ip;
  273. }
  274. $this->method = new $method($pay);
  275. return $this;
  276. }
  277. }