Memberpayment.php 18 KB

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