Api.php 6.6 KB

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