Memberpayment.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. use think\facade\Db;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 支付控制器
  11. */
  12. class Memberpayment extends MobileMember
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/buy.lang.php');
  18. }
  19. private function use_predeposit($order_info, $post, $virtual = 0)
  20. {
  21. if ($virtual == 1) {
  22. $logic_buy = model('buyvirtual', 'logic');
  23. } elseif ($virtual == 2) {
  24. $logic_buy = model('storejoinin');
  25. } else {
  26. $logic_buy = model('buy_1', 'logic');
  27. }
  28. if (empty($post['password'])) {
  29. return $order_info;
  30. }
  31. $member_model = model('member');
  32. $buyer_info = $member_model->getMemberInfoByID($this->member_info['member_id']);
  33. if ($buyer_info['member_paypwd'] == '' || $buyer_info['member_paypwd'] != md5($post['password'])) {
  34. ds_json_encode(10001, lang('password_mistake'));
  35. }
  36. if ($buyer_info['available_rc_balance'] == 0) {
  37. $post['rcb_pay'] = null;
  38. }
  39. if ($buyer_info['available_predeposit'] == 0) {
  40. $post['pd_pay'] = null;
  41. }
  42. try {
  43. Db::startTrans();
  44. if (!empty($post['rcb_pay'])) {
  45. $order_info = $logic_buy->rcbPay($order_info, $post, $buyer_info);
  46. }
  47. if (!empty($post['pd_pay'])) {
  48. $order_info = $logic_buy->pdPay($order_info, $post, $buyer_info);
  49. }
  50. Db::commit();
  51. } catch (\Exception $e) {
  52. Db::rollback();
  53. ds_json_encode(10001, $e->getMessage());
  54. }
  55. return $order_info;
  56. }
  57. private function get_order_info($result)
  58. {
  59. //计算本次需要在线支付的订单总金额
  60. $pay_amount = 0;
  61. $pay_order_id_list = array();
  62. if (!empty($result['data']['order_list'])) {
  63. foreach ($result['data']['order_list'] as $order_info) {
  64. if ($order_info['order_state'] == ORDER_STATE_NEW || $order_info['order_state'] == ORDER_STATE_DEPOSIT || $order_info['order_state'] == ORDER_STATE_REST) {
  65. $pay_amount += ($order_info['order_state'] == ORDER_STATE_DEPOSIT ? $order_info['presell_deposit_amount'] : ($order_info['order_amount'] - $order_info['presell_deposit_amount'] + $order_info['presell_rcb_amount'] + $order_info['presell_pd_amount'])) - $order_info['pd_amount'] - $order_info['rcb_amount'];
  66. $pay_order_id_list[] = $order_info['order_id'];
  67. }
  68. }
  69. }
  70. if (round($pay_amount, 2) == 0) {
  71. $result['data']['pay_end'] = 1;
  72. } else {
  73. $result['data']['pay_end'] = 0;
  74. }
  75. $result['data']['api_pay_amount'] = ds_price_format($pay_amount);
  76. //临时注释
  77. if (!empty($pay_order_id_list)) {
  78. $update = model('order')->editOrder(array('payment_time' => TIMESTAMP), array(array('order_id', 'in', $pay_order_id_list)));
  79. // if (!$update) {
  80. // exit('更新订单信息发生错误,请重新支付');//因为微信支付时会重定向获取openid所以会更新两次
  81. // }
  82. }
  83. //如果是开始支付尾款,则把支付单表重置了未支付状态,因为支付接口通知时需要判断这个状态
  84. if (isset($result['data']['if_buyer_repay'])) {
  85. $update = model('order')->editOrderpay(array('api_paystate' => 0), array('pay_id' => $result['data']['pay_id']));
  86. if (!$update) {
  87. exit(lang('order_pay_fail'));
  88. }
  89. $result['data']['api_paystate'] = 0;
  90. }
  91. return $result;
  92. }
  93. private function get_vr_order_info($result)
  94. {
  95. //计算本次需要在线支付的订单总金额
  96. $pay_amount = 0;
  97. if ($result['data']['order_state'] == ORDER_STATE_NEW) {
  98. $pay_amount += $result['data']['order_amount'] - $result['data']['pd_amount'] - $result['data']['rcb_amount'];
  99. }
  100. if ($pay_amount == 0) {
  101. $result['data']['pay_end'] = 1;
  102. } else {
  103. $result['data']['pay_end'] = 0;
  104. }
  105. $result['data']['api_pay_amount'] = ds_price_format($pay_amount);
  106. //临时注释
  107. //$update = model('order')->editOrder(array('api_pay_time'=>TIMESTAMP),array('order_id'=>$result['data']['order_id']));
  108. //if(!$update) {
  109. // return array('error' => '更新订单信息发生错误,请重新支付');
  110. //}
  111. //计算本次需要在线支付的订单总金额
  112. $pay_amount = $result['data']['order_amount'] - $result['data']['pd_amount'] - $result['data']['rcb_amount'];
  113. $result['data']['api_pay_amount'] = ds_price_format($pay_amount);
  114. return $result;
  115. }
  116. /**
  117. * @api {POST} api/Memberpayment/pay_new 实物订单支付
  118. * @apiVersion 1.0.0
  119. * @apiGroup MemberPayment
  120. *
  121. * @apiHeader {String} X-DS-KEY 用户授权token
  122. *
  123. * @apiParam {String} pay_sn 支付单号
  124. * @apiParam {Int} password 支付密码
  125. * @apiParam {Int} rcb_pay 充值卡支付金额
  126. * @apiParam {Int} pd_pay 预存款支付金额
  127. * @apiParam {String} payment_code 支付方式名称代码
  128. *
  129. * @apiSuccess {String} code 返回码,10000为成功
  130. * @apiSuccess {String} message 返回消息
  131. * @apiSuccess {Object} result 返回数据
  132. */
  133. public function pay_new()
  134. {
  135. $this->if_back();
  136. //H5 相关接口的调用
  137. @header("Content-type: text/html; charset=UTF-8");
  138. $pay_sn = input('param.pay_sn');
  139. $payment_code = input('param.payment_code');
  140. if (!preg_match('/^\d{20}$/', $pay_sn)) {
  141. ds_json_encode(10001, lang('param_error'));
  142. }
  143. $sub_payment_code = '';
  144. if (strpos($payment_code, 'allinpay_h5') !== false) {
  145. $sub_payment_code = str_replace('allinpay_h5_', '', $payment_code);
  146. $payment_code = 'allinpay_h5';
  147. }
  148. $logic_payment = model('payment', 'logic');
  149. $result = $logic_payment->getPaymentInfo($payment_code);
  150. if (!$result['code']) {
  151. ds_json_encode(10001, $result['msg']);
  152. }
  153. $payment_info = $result['data'];
  154. $payment_info['payment_config']['sub_payment_code'] = $sub_payment_code;
  155. //计算所需支付金额等支付单信息
  156. $result = $logic_payment->getRealOrderInfo($pay_sn, $this->member_info['member_id']);
  157. if (!$result['code']) {
  158. ds_json_encode(10001, $result['msg']);
  159. }
  160. if ($result['data']['api_paystate'] || empty($result['data']['api_pay_amount'])) {
  161. ds_json_encode(12001, '该订单不需要支付');
  162. }
  163. $result['data']['order_list'] = $this->use_predeposit($result['data']['order_list'], input('param.'), 0);
  164. $result = $this->get_order_info($result);
  165. if ($result['data']['pay_end'] == 1) {
  166. //站内支付了全款
  167. ds_json_encode(12001, '', 'success');
  168. exit;
  169. }
  170. //第三方API支付
  171. $this->_api_pay($result['data'], $payment_info);
  172. }
  173. /**
  174. * 虚拟订单支付
  175. */
  176. public function vr_pay_new()
  177. {
  178. $this->if_back();
  179. //H5 相关接口的调用
  180. @header("Content-type: text/html; charset=UTF-8");
  181. $pay_sn = input('param.pay_sn');
  182. $payment_code = input('param.payment_code');
  183. if (!preg_match('/^\d{20}$/', $pay_sn)) {
  184. ds_json_encode(10001, lang('param_error'));
  185. }
  186. $sub_payment_code = '';
  187. if (strpos($payment_code, 'allinpay_h5') !== false) {
  188. $sub_payment_code = str_replace('allinpay_h5_', '', $payment_code);
  189. $payment_code = 'allinpay_h5';
  190. }
  191. $logic_payment = model('payment', 'logic');
  192. $result = $logic_payment->getPaymentInfo($payment_code);
  193. if (!$result['code']) {
  194. ds_json_encode(10001, $result['msg']);
  195. }
  196. $payment_info = $result['data'];
  197. $payment_info['payment_config']['sub_payment_code'] = $sub_payment_code;
  198. //计算所需支付金额等支付单信息
  199. $result = $logic_payment->getVrOrderInfo($pay_sn, $this->member_info['member_id']);
  200. if (!$result['code']) {
  201. ds_json_encode(10001, $result['msg']);
  202. }
  203. if ($result['data']['order_state'] != ORDER_STATE_NEW || empty($result['data']['api_pay_amount'])) {
  204. ds_json_encode(12001, lang('no_payment_required_this_order'));
  205. }
  206. $result['data'] = $this->use_predeposit($result['data'], input('param.'), 1);
  207. $result = $this->get_vr_order_info($result);
  208. if ($result['data']['pay_end'] == 1) {
  209. ds_json_encode(12001, '', 'success');
  210. }
  211. //转到第三方API支付
  212. $this->_api_pay($result['data'], $payment_info);
  213. }
  214. /**
  215. * @api {POST} api/Memberpayment/sj_pay 店铺入驻
  216. * @apiVersion 1.0.0
  217. * @apiGroup MemberPayment
  218. *
  219. * @apiHeader {String} X-DS-KEY 用户授权token
  220. *
  221. * @apiParam {String} pay_sn 支付单号
  222. * @apiParam {String} payment_code 支付方式名称代码
  223. *
  224. * @apiSuccess {String} code 返回码,10000为成功
  225. * @apiSuccess {String} message 返回消息
  226. * @apiSuccess {Object} result 返回数据
  227. */
  228. public function sj_pay()
  229. {
  230. $this->if_back();
  231. //H5 相关接口的调用
  232. @header("Content-type: text/html; charset=UTF-8");
  233. $storejoinin_model = model('storejoinin');
  234. $joinin_detail = $storejoinin_model->getOneStorejoinin(array('member_id' => $this->member_info['member_id']));
  235. if (!$joinin_detail) {
  236. ds_json_encode(10001, '店铺入驻不存在');
  237. }
  238. $pay_sn = $joinin_detail['pay_sn'];
  239. if (!$pay_sn) {
  240. $pay_sn = makePaySn($this->member_info['member_id']);
  241. $storejoinin_model->editStorejoinin(array('pay_sn' => $pay_sn), array('member_id' => $this->member_info['member_id'], 'pay_sn' => ''));
  242. }
  243. $payment_code = input('param.payment_code');
  244. $sub_payment_code = '';
  245. if (strpos($payment_code, 'allinpay_h5') !== false) {
  246. $sub_payment_code = str_replace('allinpay_h5_', '', $payment_code);
  247. $payment_code = 'allinpay_h5';
  248. }
  249. $logic_payment = model('payment', 'logic');
  250. $result = $logic_payment->getPaymentInfo($payment_code);
  251. if (!$result['code']) {
  252. ds_json_encode(10001, $result['msg']);
  253. }
  254. $payment_info = $result['data'];
  255. $payment_info['payment_config']['sub_payment_code'] = $sub_payment_code;
  256. //计算所需支付金额等支付单信息
  257. $result = $logic_payment->getSjOrderInfo($pay_sn);
  258. if (!$result['code']) {
  259. ds_json_encode(10001, $result['msg']);
  260. }
  261. if ($result['data']['joinin_state'] != STORE_JOIN_STATE_VERIFY_SUCCESS || empty($result['data']['api_pay_amount'])) {
  262. ds_json_encode(12001, lang('no_payment_required_this_order'));
  263. }
  264. $result['data'] = $this->use_predeposit($result['data'], input('param.'), 2);
  265. $result = $this->get_sj_order_info($result);
  266. if ($result['data']['pay_end'] == 1) {
  267. ds_json_encode(12001, '', 'success');
  268. }
  269. //转到第三方API支付
  270. $this->_api_pay($result['data'], $payment_info);
  271. }
  272. private function get_sj_order_info($result)
  273. {
  274. //计算本次需要在线支付的订单总金额
  275. $pay_amount = 0;
  276. if ($result['data']['joinin_state'] == STORE_JOIN_STATE_VERIFY_SUCCESS) {
  277. $pay_amount += $result['data']['paying_amount'] - $result['data']['pd_amount'] - $result['data']['rcb_amount'];
  278. }
  279. if ($pay_amount == 0) {
  280. $result['data']['pay_end'] = 1;
  281. } else {
  282. $result['data']['pay_end'] = 0;
  283. }
  284. $result['data']['api_pay_amount'] = ds_price_format($pay_amount);
  285. return $result;
  286. }
  287. /**
  288. * @api {POST} api/Memberpayment/pd_pay 账户充值
  289. * @apiVersion 1.0.0
  290. * @apiGroup MemberPayment
  291. *
  292. * @apiHeader {String} X-DS-KEY 用户授权token
  293. *
  294. * @apiParam {String} pay_sn 支付单号
  295. * @apiParam {String} payment_code 支付方式名称代码
  296. *
  297. * @apiSuccess {String} code 返回码,10000为成功
  298. * @apiSuccess {String} message 返回消息
  299. * @apiSuccess {Object} result 返回数据
  300. */
  301. public function pd_pay()
  302. {
  303. $this->if_back();
  304. $pay_sn = input('param.pay_sn');
  305. $payment_code = input('param.payment_code');
  306. $sub_payment_code = '';
  307. if (strpos($payment_code, 'allinpay_h5') !== false) {
  308. $sub_payment_code = str_replace('allinpay_h5_', '', $payment_code);
  309. $payment_code = 'allinpay_h5';
  310. }
  311. $condition = array();
  312. $condition[] = array('payment_code', '=', $payment_code);
  313. $logic_payment = model('payment', 'logic');
  314. $result = $logic_payment->getPaymentInfo($payment_code);
  315. if (!$result['code']) {
  316. ds_json_encode(10001, $result['msg']);
  317. }
  318. $payment_info = $result['data'];
  319. $result = $logic_payment->getPdOrderInfo($pay_sn, $this->member_info['member_id']);
  320. if (!$result['code']) {
  321. ds_json_encode(10001, $result['msg']);
  322. exit();
  323. }
  324. if ($result['data']['pdr_payment_state'] || empty($result['data']['api_pay_amount'])) {
  325. ds_json_encode(12001, lang('no_payment_required'));
  326. exit();
  327. }
  328. $payment_info['payment_config']['sub_payment_code'] = $sub_payment_code;
  329. $this->_api_pay($result['data'], $payment_info);
  330. }
  331. private function if_back()
  332. {
  333. $random_number = input('param.random_number');
  334. $code_number = input('param.code');
  335. if ($random_number && input('param.payment_code') == 'wxpay_jsapi') {
  336. if (session('pay_random_number') == $random_number) {
  337. //是返回(排除微信获取openid的重定向)
  338. if (session('pay_code_number') == $code_number) {
  339. header('Location:' . config('ds_config.h5_site_url'));
  340. exit;
  341. } else {
  342. session('pay_code_number', $code_number);
  343. }
  344. } else {
  345. session('pay_random_number', $random_number);
  346. }
  347. }
  348. }
  349. /**
  350. * 第三方在线支付接口
  351. *
  352. */
  353. private function _api_pay($order_info, $payment_info)
  354. {
  355. try {
  356. $payment_api = new $payment_info['payment_code']($payment_info);
  357. $payment_api->get_payform($order_info);
  358. } catch (\Exception $e) {
  359. ds_json_encode(10001, $e->getMessage());
  360. }
  361. }
  362. /**
  363. * @api {POST} api/Memberpayment/payment_list 可用支付参数列表
  364. * @apiVersion 1.0.0
  365. * @apiGroup MemberPayment
  366. *
  367. * @apiHeader {String} X-DS-KEY 用户授权token
  368. *
  369. * @apiParam {String} payment_platform 支付平台
  370. * @apiParam {String} payment_code 支付方式代码
  371. *
  372. * @apiSuccess {String} code 返回码,10000为成功
  373. * @apiSuccess {String} message 返回消息
  374. * @apiSuccess {Object} result 返回数据
  375. * @apiSuccess {Object[]} result.payment_list 支付方式数组
  376. * @apiSuccess {String} result.payment_list.payment_code 支付方式代码
  377. * @apiSuccess {String} result.payment_list.payment_name 支付方式名称
  378. */
  379. public function payment_list()
  380. {
  381. $payment_model = model('payment');
  382. $condition = array();
  383. $payment_code = input('param.payment_code');
  384. $payment_platform = input('param.payment_platform');
  385. if ($payment_code) {
  386. $condition[] = array('payment_code', '=', $payment_code);
  387. } else {
  388. if ($payment_platform) {
  389. $condition[] = array('payment_platform', '=', $payment_platform);
  390. } else {
  391. $condition[] = array('payment_platform', '=', 'h5');
  392. $condition[] = array('payment_code', 'not in', ['wxpay_jsapi', 'wxpay_minipro']);
  393. }
  394. }
  395. $payment_list = $payment_model->getPaymentOpenList($condition);
  396. $payment_array = array();
  397. if (!empty($payment_list)) {
  398. foreach ($payment_list as $value) {
  399. $payment_array[] = array(
  400. 'payment_code' => $value['payment_code'],
  401. 'payment_name' => $value['payment_name'],
  402. );
  403. }
  404. }
  405. ds_json_encode(10000, '', array('payment_list' => $payment_array));
  406. }
  407. }