Api.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace Pay\Src;
  3. use Dever;
  4. class Api
  5. {
  6. /**
  7. * 发起跳转
  8. *
  9. * @return mixed
  10. */
  11. public function jump()
  12. {
  13. $url = Dever::input('refer');
  14. $url = urldecode($url);
  15. Dever::location($url);
  16. }
  17. # 获取支付渠道
  18. public function channel()
  19. {
  20. $data = Dever::db('pay/channel')->getData();
  21. return $data;
  22. }
  23. /**
  24. * 发起支付 下单 获取预支付信息
  25. *
  26. * @return mixed
  27. */
  28. public function get($type = 1, $param = array())
  29. {
  30. if (!$type) {
  31. $type = 1;
  32. }
  33. $this->init($param);
  34. if ($this->order_id) {
  35. $pay = Dever::db('pay/order')->one(array('order_id' => $this->order_id));
  36. if ($pay && $pay['status'] == 1 && $pay['param']) {
  37. $order = Dever::array_decode($pay['param']);
  38. if (isset($order['payMsg']) && $order['payMsg']) {
  39. return $order['payMsg'];
  40. }
  41. return $order;
  42. }
  43. }
  44. 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, $this->other, $this->refer);
  45. }
  46. /**
  47. * notify
  48. *
  49. * @return mixed
  50. */
  51. public function notify($param = array())
  52. {
  53. $this->checkAccount($param);
  54. $this->handle();
  55. return $this->method->notify();
  56. }
  57. /**
  58. * 发起支付 通用的发起支付方法
  59. *
  60. * @return mixed
  61. */
  62. public function pay($param = array())
  63. {
  64. $system_source = $this->getParam($param, 'system_source');
  65. $receipt = $this->getParam($param, 'receipt');
  66. $account_id = $this->getParam($param, 'account_id');
  67. $config = Dever::config('base', 'pay')->pay['method'];
  68. if (isset($config[$system_source])) {
  69. $param['account_id'] = $account_id ? $account_id : $system_source;
  70. $method = $config[$system_source];
  71. if ($system_source == 3 && $receipt) {
  72. $method = $method[1];
  73. $param['other'] = $receipt;
  74. }
  75. $result = array();
  76. $result['order'] = $this->$method($param);
  77. $result['order_id'] = $this->method->order_id;
  78. $result['type'] = $this->type;
  79. return $result;
  80. } else {
  81. Dever::alert('错误的source');
  82. }
  83. }
  84. /**
  85. * 发起支付 用于小程序支付
  86. *
  87. * @return mixed
  88. */
  89. public function applet($param = array())
  90. {
  91. $param['system_source'] = 4;
  92. $this->init($param);
  93. return $this->method->applet($this->get(1));
  94. }
  95. /**
  96. * 发起支付 用于app支付
  97. *
  98. * @return mixed
  99. */
  100. public function app($param = array())
  101. {
  102. $param['system_source'] = 1;
  103. $this->init($param);
  104. $this->openid = -1;
  105. return $this->method->app($this->get(3));
  106. }
  107. /**
  108. * 发起支付 用于苹果内购支付
  109. *
  110. * @return mixed
  111. */
  112. public function apple($param = array())
  113. {
  114. $this->init($param);
  115. # 只需验证苹果过来的参数即可
  116. 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);
  117. }
  118. /**
  119. * 发起支付 用于页面支付
  120. *
  121. * @return mixed
  122. */
  123. public function page($param = array())
  124. {
  125. $this->init($param);
  126. if (!$this->refer && $this->type != 'test') {
  127. Dever::alert('没有回调refer');
  128. }
  129. $type = 1;
  130. if ($this->h5 == 1) {
  131. $type = 4;
  132. }
  133. return $this->method->page($this->get($type), $this->refer);
  134. }
  135. /**
  136. * 发起支付 用于扫码支付
  137. *
  138. * @return mixed
  139. */
  140. public function qrcode($param = array())
  141. {
  142. $this->init($param);
  143. return $this->method->qrcode($this->get(2), $this->refer);
  144. Dever::apply('sdk/qrcode');
  145. return \QRcode::png($url);
  146. }
  147. /**
  148. * 查询支付
  149. *
  150. * @return mixed
  151. */
  152. public function search($param = array())
  153. {
  154. $this->checkAccount($param);
  155. $this->handle();
  156. $this->order_id = $this->getParam($param, 'order_id');
  157. return $this->method->search($this->order_id);
  158. }
  159. /**
  160. * 退款
  161. *
  162. * @return mixed
  163. */
  164. public function refund($param = array())
  165. {
  166. $this->checkAccount($param);
  167. $this->handle();
  168. $this->other = $this->getParam($param, 'other');
  169. $this->order_id = $this->getParam($param, 'order_id');
  170. $this->refund_order_id = $this->getParam($param, 'refund_order_id');
  171. $this->refund_cash = $this->getParam($param, 'refund_cash');
  172. return $this->method->refundByOrder($this->order_id, $this->refund_order_id, $this->refund_cash, $this->other);
  173. }
  174. /**
  175. * 初始化 设置参数
  176. *
  177. * @return mixed
  178. */
  179. private function init($param = array())
  180. {
  181. if (isset($this->account_id)) {
  182. return;
  183. }
  184. $this->checkAccount($param);
  185. $this->project_id = $this->getParam($param, 'project_id');
  186. $this->uid = $this->getParam($param, 'uid');
  187. $this->openid = $this->getParam($param, 'openid');
  188. $this->username = $this->getParam($param, 'username');
  189. $this->product_id = $this->getParam($param, 'product_id');
  190. $this->name = $this->getParam($param, 'name');
  191. $this->cash = $this->getParam($param, 'cash');
  192. $this->refer = $this->getParam($param, 'refer');
  193. $this->order_id = $this->getParam($param, 'order_id');
  194. $this->other = $this->getParam($param, 'other');
  195. $this->h5 = $this->getParam($param, 'h5');
  196. $this->ip = $this->getParam($param, 'ip');
  197. if (!$this->project_id) {
  198. $this->project_id = false;
  199. }
  200. if (!$this->order_id) {
  201. $this->order_id = false;
  202. }
  203. if (!$this->uid) {
  204. Dever::alert('没有用户信息');
  205. }
  206. if (!$this->product_id) {
  207. Dever::alert('没有产品信息');
  208. }
  209. if (!$this->name) {
  210. Dever::alert('没有支付信息');
  211. }
  212. if (!$this->cash) {
  213. Dever::alert('没有支付金额');
  214. }
  215. if (!$this->ip) {
  216. $this->ip = Dever::ip();
  217. }
  218. return $this->handle();
  219. }
  220. /**
  221. * 初始化 验证账户信息
  222. *
  223. * @return mixed
  224. */
  225. private function checkAccount($param = array())
  226. {
  227. $this->channel_id = $this->getParam($param, 'channel_id');
  228. $this->system_source = $this->getParam($param, 'system_source');
  229. $this->account_id = $this->getParam($param, 'account_id');
  230. if (!$this->account_id || (!$this->channel_id && !$this->system_source)) {
  231. Dever::alert('没有账户信息');
  232. }
  233. }
  234. /**
  235. * 初始化
  236. *
  237. * @return mixed
  238. */
  239. private function getParam($param, $key)
  240. {
  241. if (isset($param[$key])) {
  242. return $param[$key];
  243. }
  244. return Dever::input($key, false);
  245. }
  246. /**
  247. * 获取支付类
  248. *
  249. * @return mixed
  250. */
  251. private function handle()
  252. {
  253. $pay = false;
  254. if ($this->account_id) {
  255. $pay = Dever::db('pay/account')->one($this->account_id);
  256. } elseif ($this->channel_id && $this->system_source) {
  257. $pay = Dever::db('pay/account')->one(array('channel_id' => $this->channel_id, 'system_source' => $this->system_source));
  258. }
  259. if (!$pay || ($pay && $pay['state'] != 1)) {
  260. Dever::alert('没有账户信息');
  261. }
  262. if (!$pay['type']) {
  263. Dever::alert('账户类型错误');
  264. }
  265. $this->account_id = $pay['id'];
  266. /*
  267. $this->channel_id = $pay['channel_id'];
  268. $this->system_source = $pay['system_source'];
  269. */
  270. if ($pay['box'] == 3) {
  271. $pay['type'] = 'test';
  272. }
  273. $this->type = $pay['type'];
  274. $method = '\\Pay\\Lib\\' . ucwords($pay['type']);
  275. if (isset($this->refer) && $this->refer) {
  276. $pay['refer'] = $this->refer;
  277. }
  278. if (isset($this->ip) && $this->ip) {
  279. $pay['ip'] = $this->ip;
  280. }
  281. $this->method = new $method($pay);
  282. return $this;
  283. }
  284. public function buy_order($data)
  285. {
  286. $id = Dever::input('merchant_id');
  287. $data = Dever::db('pay/yspay_cash')->select(['merchant_id' => $id]);
  288. if (!$data) {
  289. Dever::alert('无导出数据');
  290. }
  291. $file = date('Y-m-d H:i:s') . '资金流水数据';
  292. $header = array('商户名称', '流水号', '关联订单号', '交易金额', '银联手续费', '平台手续费', '实际金额', '商户划付金额', '平台分账金额', '订单状态', '分账状态', '下单时间', '入账时间', '分账时间');
  293. $body = array();
  294. $orderStatus = Dever::db('pay/yspay_cash')->config['status'];
  295. $fenStatus = Dever::db('pay/yspay_cash')->config['fenzhang_status'];
  296. $factory = Dever::db('pay/yspay_merchant')->all();
  297. $factory = array_column($factory, 'name', 'id');
  298. foreach ($data as $k => $v) {
  299. $factory_name = isset($factory[$v['account_id']]) ? $factory[$v['account_id']] : '';
  300. $d = array(
  301. $factory_name,
  302. $v['order_num'],
  303. $v['source_order_num'],
  304. $v['ycash'] / 100,
  305. $v['yl_cash'] / 100,
  306. $v['pt_cash'] / 100,
  307. $v['cash'] / 100,
  308. $v['hf_cash'] / 100,
  309. $v['fz_cash'] / 100,
  310. $orderStatus[$v['status']] ?? '',
  311. $fenStatus[$v['fenzhang_status']] ?? '',
  312. empty($v['cdate']) ? '' : date('Y-m-d H:i:s', $v['cdate']),
  313. empty($v['rdate']) ? '' : date('Y-m-d H:i:s', $v['rdate']),
  314. empty($v['fdate']) ? '' : date('Y-m-d H:i:s', $v['fdate']),
  315. );
  316. $body[] = $d;
  317. };
  318. return Dever::excelExport($body, $header, $file);
  319. }
  320. }