Payment.php 9.4 KB

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