Payment.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Payment extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/payment.lang.php');
  18. }
  19. /**
  20. * 支付方式
  21. */
  22. public function index()
  23. {
  24. $payment_model = model('payment');
  25. //获取数据库中已安装的支付方式
  26. $install_payment_list = $payment_model->getPaymentList(array(array('payment_code', '<>', 'predeposit')));
  27. $install_payment_list = ds_change_arraykey($install_payment_list, 'payment_code');
  28. //获取已存在的支付列表文件
  29. $file_payment_list = $payment_model->get_builtin();
  30. $payment_platform = input('param.payment_platform');
  31. if (!in_array($payment_platform, array('pc', 'h5', 'app'))) {
  32. $payment_platform = 'pc';
  33. }
  34. foreach ($file_payment_list as $key => $value) {
  35. if ($value['payment_platform'] != $payment_platform) {
  36. unset($file_payment_list[$key]);
  37. continue;
  38. }
  39. if (array_key_exists($key, $install_payment_list)) {
  40. $file_payment_list[$key]['install'] = 1;
  41. //已安装的支付,配置信息使用数据库中配置信息
  42. $file_payment_list[$key]['payment_config'] = $install_payment_list[$key]['payment_config'];
  43. $file_payment_list[$key]['payment_state'] = $install_payment_list[$key]['payment_state'];
  44. } else {
  45. $file_payment_list[$key]['install'] = 0;
  46. $file_payment_list[$key]['payment_state'] = 0;
  47. }
  48. }
  49. View::assign('payment_list', $file_payment_list);
  50. $this->setAdminCurItem('index_' . $payment_platform);
  51. return View::fetch();
  52. }
  53. /**
  54. * 安装支付方式
  55. */
  56. function install()
  57. {
  58. $payment_code = input('param.payment_code');
  59. $payment_mod = model('payment');
  60. //如果是小程序支付、微信JS支付、微信H5支付、微信APP支付则必须先开启微信扫码支付
  61. if (in_array($payment_code, array('wxpay_minipro', 'wxpay_jsapi', 'wxpay_h5', 'wxpay_app'))) {
  62. $payment = model('payment')->getPaymentInfo(array('payment_code' => 'wxpay_native'));
  63. if (empty($payment) || empty(unserialize($payment['payment_config']))) {
  64. ds_json_encode('10001', lang('please_open_wechat_payment'));
  65. }
  66. }
  67. //如果是支付宝H5支付则开启支付宝支付
  68. if (in_array($payment_code, array('alipay_h5'))) {
  69. $payment = model('payment')->getPaymentInfo(array('payment_code' => 'alipay'));
  70. if (empty($payment) || empty(unserialize($payment['payment_config']))) {
  71. ds_json_encode('10001', lang('please_open_alipay_payment'));
  72. }
  73. }
  74. $payment = model('payment')->getPaymentInfo(array('payment_code' => $payment_code));
  75. if (empty($payment)) {
  76. $file_payment = include_once(PLUGINS_PATH . '/payments/' . $payment_code . '/payment.info.php');
  77. $data['payment_code'] = $file_payment['payment_code'];
  78. $data['payment_name'] = $file_payment['payment_name'];
  79. $data['payment_state'] = 1;
  80. $data['payment_platform'] = $file_payment['payment_platform'];
  81. $data['payment_config'] = serialize(array());
  82. $resutlt = $payment_mod->addPayment($data);
  83. if ($resutlt) {
  84. ds_json_encode('10000', lang('ds_common_op_succ'));
  85. } else {
  86. ds_json_encode('10001', lang('ds_common_op_fail'));
  87. }
  88. } else {
  89. ds_json_encode('10001', lang('ds_common_op_fail'));
  90. }
  91. }
  92. /**
  93. * 编辑
  94. */
  95. public function edit()
  96. {
  97. $payment_model = model('payment');
  98. $payment_code = trim(input('param.payment_code'));
  99. $install_payment = $payment_model->getPaymentInfo(array('payment_code' => $payment_code));
  100. $file_payment = include_once(PLUGINS_PATH . '/payments/' . $install_payment['payment_code'] . '/payment.info.php');
  101. if (is_array($file_payment['payment_config'])) {
  102. $install_payment_config = unserialize($install_payment['payment_config']);
  103. unset($install_payment['payment_config']);
  104. foreach ($file_payment['payment_config'] as $key => $value) {
  105. $install_payment['payment_config'][$key]['name'] = $value['name'];
  106. $install_payment['payment_config'][$key]['type'] = $value['type'];
  107. $install_payment['payment_config'][$key]['desc'] = lang($value['name'] . '_desc');
  108. $install_payment['payment_config'][$key]['lable'] = lang($value['name']);
  109. $install_payment['payment_config'][$key]['value'] = isset($install_payment_config[$value['name']]) ? $install_payment_config[$value['name']] : $value['value'];
  110. }
  111. }
  112. if (!(request()->isPost())) {
  113. View::assign('payment', $install_payment);
  114. return View::fetch();
  115. } else {
  116. $data = array();
  117. $data['payment_state'] = intval(input('post.payment_state'));
  118. $config_info = array();
  119. $cfg_value_array = input('post.cfg_value/a'); #获取数组
  120. $cfg_name_array = input('post.cfg_name/a'); #获取数组
  121. if (is_array($cfg_value_array) && !empty($cfg_value_array)) {
  122. foreach ($cfg_value_array as $i => $v) {
  123. $config_info[trim($cfg_name_array[$i])] = trim($cfg_value_array[$i]);
  124. }
  125. }
  126. $cfg_name2_array = input('post.cfg_name2/a'); #获取数组
  127. if (is_array($cfg_name2_array)) {
  128. foreach ($cfg_name2_array as $i => $v) {
  129. $cfg_value2 = isset($install_payment_config[trim($cfg_name2_array[$i])]) ? $install_payment_config[trim($cfg_name2_array[$i])] : '';
  130. $file = array();
  131. foreach ($_FILES['cfg_value2_' . $i] as $key => $value) {
  132. $file[$key] = $value;
  133. }
  134. if (!empty($file['name'])) {
  135. $upload_file = PLUGINS_PATH . '/payments/' . $install_payment['payment_code'] . '/asserts';
  136. $file = request()->file('cfg_value2_' . $i);
  137. $file_config = array(
  138. 'disks' => array(
  139. 'local' => array(
  140. 'root' => $upload_file
  141. )
  142. )
  143. );
  144. config($file_config, 'filesystem');
  145. try {
  146. validate(['image' => 'fileSize:' . ALLOW_IMG_SIZE . '|fileExt:pfx'])
  147. ->check(['image' => $file]);
  148. $file_name = \think\facade\Filesystem::putFile('', $file);
  149. $cfg_value2 = $file_name;
  150. } catch (\Exception $e) {
  151. $this->error($e->getMessage());
  152. }
  153. }
  154. $config_info[trim($cfg_name2_array[$i])] = $cfg_value2;
  155. }
  156. }
  157. $data['payment_config'] = serialize($config_info);
  158. $payment_model->editPayment($data, array('payment_code' => $payment_code));
  159. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  160. }
  161. }
  162. /**
  163. * 删除支付方式,卸载
  164. */
  165. public function del()
  166. {
  167. $payment_model = model('payment');
  168. $payment_code = trim(input('param.payment_code'));
  169. $condition = array();
  170. $condition[] = array('payment_code', '=', $payment_code);
  171. $result = $payment_model->delPayment($condition);
  172. if ($result) {
  173. ds_json_encode('10000', lang('ds_common_op_succ'));
  174. } else {
  175. ds_json_encode('10001', lang('ds_common_op_fail'));
  176. }
  177. }
  178. /**
  179. * 获取卖家栏目列表,针对控制器下的栏目
  180. */
  181. protected function getAdminItemList()
  182. {
  183. $menu_array = array(
  184. array(
  185. 'name' => 'index_pc',
  186. 'text' => lang('payment_index_pc'),
  187. 'url' => (string) url('Payment/index')
  188. ),
  189. array(
  190. 'name' => 'index_h5',
  191. 'text' => lang('payment_index_h5'),
  192. 'url' => (string) url('Payment/index', ['payment_platform' => 'h5'])
  193. ),
  194. array(
  195. 'name' => 'index_app',
  196. 'text' => lang('payment_index_app'),
  197. 'url' => (string) url('Payment/index', ['payment_platform' => 'app'])
  198. ),
  199. );
  200. return $menu_array;
  201. }
  202. }