Memberpayment.php 17 KB

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