PayChannelService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\pay;
  12. use app\dict\common\ChannelDict;
  13. use app\dict\common\CommonDict;
  14. use app\dict\pay\PayChannelDict;
  15. use app\dict\pay\PayDict;
  16. use app\model\pay\PayChannel;
  17. use app\service\core\pay\CorePayChannelService;
  18. use core\base\BaseAdminService;
  19. use core\exception\PayException;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. /**
  24. * 支付配置服务层
  25. */
  26. class PayChannelService extends BaseAdminService
  27. {
  28. public $core_pay_channel_service;
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. $this->model = new PayChannel();
  33. $this->core_pay_channel_service = new CorePayChannelService();
  34. }
  35. /**
  36. * 添加模板
  37. * @param string $channel
  38. * @param string $type
  39. * @param array $data
  40. * @return true
  41. */
  42. public function set(string $channel, string $type, array $data)
  43. {
  44. $where = [
  45. 'type' => $type,
  46. 'channel' => $channel
  47. ];
  48. if (!array_key_exists($type, PayDict::getPayType())) throw new PayException('PATMENT_METHOD_INVALID');
  49. if ($channel != 'transfer') {
  50. if (!array_key_exists($channel, ChannelDict::getType())) throw new PayException('CHANNEL_MARK_INVALID');
  51. }
  52. $pay_channel = $this->core_pay_channel_service->find($where);
  53. if ($pay_channel->isEmpty()) {
  54. $data['channel'] = $channel;
  55. $data['type'] = $type;
  56. $data['site_id'] = $this->request->defaultSiteId();
  57. $data['config'] = $this->getConfigByPayType($data['config'], $type);
  58. $res = $this->model->create($data);
  59. } else {
  60. $config = $pay_channel->config;
  61. $data['config'] = $this->getConfigByPayType($data['config'], $type);
  62. foreach ($data['config'] as $config_k => $config_v) {
  63. if ($config_v == CommonDict::ENCRYPT_STR && isset($config[$config_k])) {
  64. $data['config'][$config_k] = $config[$config_k];
  65. }
  66. }
  67. $pay_channel->save($data);
  68. }
  69. return true;
  70. }
  71. /**
  72. * 用于后端支付渠道
  73. * @return array
  74. * @throws DataNotFoundException
  75. * @throws DbException
  76. * @throws ModelNotFoundException
  77. */
  78. public function getChannelList()
  79. {
  80. $channel_list = PayChannelDict::getPayChannel();
  81. $pay_channel_list_temp = $this->model->field('type, channel, config, sort, status')->select()->toArray();
  82. $pay_channel_list = [];
  83. foreach ($pay_channel_list_temp as $v) {
  84. $pay_channel_list[$v['channel']][$v['type']] = $v;
  85. }
  86. $pay_type_list = PayDict::getPayType();
  87. foreach ($channel_list as $k => $v) {
  88. $temp_item = $pay_channel_list[$k] ?? [];
  89. foreach ($v['pay_type'] as $item_k => $item_v) {
  90. if (isset($temp_item[ $item_k ])) {
  91. $temp_v_item = $temp_item[ $item_k ];
  92. $encrypt_params = $pay_type_list[$item_k]['encrypt_params'] ?? [];
  93. foreach ($temp_v_item['config'] as $config_k => $config_v) {
  94. if ($config_v !== '' && in_array($config_k, $encrypt_params)) $temp_v_item['config'][$config_k] = CommonDict::ENCRYPT_STR;
  95. }
  96. } else {
  97. $temp_v_item = [ 'status' => 0, 'config' => $this->getConfigByPayType([], $item_k), 'sort' => 0 ];
  98. }
  99. $item_v['config'] = $temp_v_item['config'];
  100. $item_v['status'] = $temp_v_item['status'];
  101. $item_v['sort'] = $temp_v_item['sort'];
  102. if ($item_k == PayDict::FRIENDSPAY) {
  103. $item_v[ 'name' ] = $temp_v_item[ 'config' ][ 'pay_type_name' ] ?? get_lang('dict_pay.type_friendspay');
  104. }
  105. $channel_list[$k]['pay_type'][$item_k] = $item_v;
  106. }
  107. $temp_pay_type = array_values($channel_list[$k]['pay_type']);
  108. $sort = array_column($temp_pay_type, 'sort');
  109. array_multisort($sort, SORT_ASC, $temp_pay_type);
  110. $channel_list[$k]['pay_type'] = $temp_pay_type;
  111. }
  112. return $channel_list;
  113. }
  114. /**
  115. * 通过渠道获取配置
  116. * @param string $channel
  117. * @return array
  118. * @throws DataNotFoundException
  119. * @throws DbException
  120. * @throws ModelNotFoundException
  121. */
  122. public function getListByChannel(string $channel)
  123. {
  124. $where = [
  125. 'channel' => $channel
  126. ];
  127. return $this->model->where($where)->field('type, channel, config, sort, status')->select()->toArray();
  128. }
  129. /**
  130. * 通过支付方式获取配置格式
  131. * @param $data
  132. * @param $type
  133. * @return array
  134. */
  135. public function getConfigByPayType($data, $type)
  136. {
  137. $config = [];
  138. switch ($type) {
  139. case PayDict::WECHATPAY:
  140. $config = [
  141. 'mch_id' => $data[ 'mch_id' ] ?? '',//商户号
  142. 'mch_secret_key' => $data[ 'mch_secret_key' ] ?? '',//商户秘钥 现在默认认为是v3版
  143. 'mch_secret_cert' => $data[ 'mch_secret_cert' ] ?? '',//商户私钥 字符串或路径
  144. 'mch_public_cert_path' => $data[ 'mch_public_cert_path' ] ?? '',//商户公钥证书路径
  145. 'wechat_public_cert_path' => $data['wechat_public_cert_path'] ?? '', // 微信支付公钥
  146. 'wechat_public_cert_id' => $data['wechat_public_cert_id'] ?? '' // 微信支付公钥id
  147. ];
  148. break;
  149. case PayDict::ALIPAY:
  150. $config = [
  151. 'app_id' => $data[ 'app_id' ] ?? '',// 必填-支付宝分配的 app_id
  152. 'app_secret_cert' => $data[ 'app_secret_cert' ] ?? '',// 必填-应用私钥 字符串或路径
  153. 'app_public_cert_path' => $data[ 'app_public_cert_path' ] ?? '',//必填-应用公钥证书 路径
  154. 'alipay_public_cert_path' => $data[ 'alipay_public_cert_path' ] ?? '',//必填-支付宝公钥证书 路径
  155. 'alipay_root_cert_path' => $data[ 'alipay_root_cert_path' ] ?? '',// 必填-支付宝根证书 路径
  156. ];
  157. break;
  158. case PayDict::OFFLINEPAY:
  159. $config = [
  160. 'collection_name' => $data[ 'collection_name' ] ?? '',// 必填-收款账户名称
  161. 'collection_bank' => $data[ 'collection_bank' ] ?? '',//必填-收款银行
  162. 'collection_account' => $data[ 'collection_account' ] ?? '',//必填-收款账号
  163. 'collection_desc' => $data[ 'collection_desc' ] ?? '',// 必填-转账说明
  164. ];
  165. break;
  166. case PayDict::FRIENDSPAY:
  167. $config = [
  168. 'pay_type_name' => $data[ 'pay_type_name' ] ?? get_lang('dict_pay.type_friendspay'),// 支付方式名称
  169. 'pay_leave_message' => $data[ 'pay_leave_message' ] ?? get_lang('dict_pay_config.pay_leave_message'),// 发起帮付默认留言
  170. 'pay_button_name' => $data[ 'pay_button_name' ] ?? get_lang('dict_pay_config.pay_button_name'),// 支付按钮名称
  171. 'pay_page_name' => $data[ 'pay_page_name' ] ?? get_lang('dict_pay_config.pay_page_name'),// 帮付页面名称
  172. 'pay_explain_switch' => $data[ 'pay_explain_switch' ] ?? 1,// 帮付说明开关, 1开启,0关闭
  173. 'pay_explain_title' => $data[ 'pay_explain_title' ] ?? get_lang('dict_pay_config.pay_explain_title'),// 帮付说明标题
  174. 'pay_explain_content' => $data[ 'pay_explain_content' ] ?? get_lang('dict_pay_config.pay_explain_content'),// 帮付说明内容
  175. 'pay_info_switch' => $data[ 'pay_info_switch' ] ?? 1,// 订单信息, 1开启,0关闭
  176. 'pay_wechat_share_image' => $data[ 'pay_wechat_share_image' ] ?? '/static/resource/images/pay/pay_wechat_share_image.png',// 默认分享图片(公众号)
  177. 'pay_weapp_share_image' => $data[ 'pay_weapp_share_image' ] ?? '/static/resource/images/pay/pay_weapp_share_image.png'// 默认分享图片(小程序)
  178. ];
  179. break;
  180. default:
  181. $config = $data;
  182. }
  183. return $config;
  184. }
  185. /**
  186. * 设置打款设置
  187. * @param $data
  188. * @return true
  189. */
  190. public function setTransfer($data)
  191. {
  192. $wechatpay_config = $data['wechatpay_config'];
  193. $alipay_config = $data['alipay_config'];
  194. $this->set('transfer', PayDict::WECHATPAY, [
  195. 'config' => $wechatpay_config,
  196. 'status' => 1,
  197. ]);
  198. $this->set('transfer', PayDict::ALIPAY, [
  199. 'config' => $alipay_config,
  200. 'status' => 1,
  201. ]);
  202. return true;
  203. }
  204. public function setAll($data){
  205. foreach($data as $k => $v){
  206. $temp_v = $v['pay_type'];
  207. foreach($temp_v as $item_k => $item){
  208. $this->set($k, $item['key'], [
  209. 'config' => $item['config'] ?? [],
  210. 'status' => $item['status'] ?? 0,
  211. 'sort' => $item['sort'] ?? 0,
  212. ]);
  213. }
  214. }
  215. return true;
  216. }
  217. /**
  218. * 查询信息
  219. * @param array $where
  220. * @return mixed
  221. */
  222. public function getInfo($where = [])
  223. {
  224. $where[ 'site_id' ] = $this->site_id;
  225. $res = $this->model->where($where)->field('type, channel, config, sort, status')->findOrEmpty()->toArray();
  226. return $res;
  227. }
  228. }