PayChannelService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\pay\PayChannelDict;
  14. use app\dict\pay\PayDict;
  15. use app\model\pay\PayChannel;
  16. use app\service\core\pay\CorePayChannelService;
  17. use core\base\BaseAdminService;
  18. use core\exception\PayException;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. /**
  23. * 支付配置服务层
  24. */
  25. class PayChannelService extends BaseAdminService
  26. {
  27. public $core_pay_channel_service;
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. $this->model = new PayChannel();
  32. $this->core_pay_channel_service = new CorePayChannelService();
  33. }
  34. /**
  35. * 添加模板
  36. * @param string $channel
  37. * @param string $type
  38. * @param array $data
  39. * @return true
  40. */
  41. public function set(string $channel, string $type, array $data)
  42. {
  43. $where = [
  44. 'type' => $type,
  45. 'channel' => $channel
  46. ];
  47. if (!array_key_exists($type, PayDict::getPayType())) throw new PayException('PATMENT_METHOD_INVALID');
  48. if ($channel != 'transfer') {
  49. if (!array_key_exists($channel, ChannelDict::getType())) throw new PayException('CHANNEL_MARK_INVALID');
  50. }
  51. $pay_channel = $this->core_pay_channel_service->find($where);
  52. if ($pay_channel->isEmpty()) {
  53. $data['channel'] = $channel;
  54. $data['type'] = $type;
  55. $data['site_id'] = $this->request->defaultSiteId();
  56. $data['config'] = $this->getConfigByPayType($data['config'], $type);
  57. $res = $this->model->create($data);
  58. } else {
  59. $data['config'] = $this->getConfigByPayType($data['config'], $type);
  60. $pay_channel->save($data);
  61. }
  62. return true;
  63. }
  64. /**
  65. * 用于后端支付渠道
  66. * @return array
  67. * @throws DataNotFoundException
  68. * @throws DbException
  69. * @throws ModelNotFoundException
  70. */
  71. public function getChannelList()
  72. {
  73. $channel_list = PayChannelDict::getPayChannel();
  74. $pay_channel_list_temp = $this->model->field('type, channel, config, sort, status')->select()->toArray();
  75. $pay_channel_list = [];
  76. foreach ($pay_channel_list_temp as $v) {
  77. $pay_channel_list[$v['channel']][$v['type']] = $v;
  78. }
  79. foreach ($channel_list as $k => $v) {
  80. $temp_item = $pay_channel_list[$k] ?? [];
  81. foreach ($v['pay_type'] as $item_k => $item_v) {
  82. $temp_v_item = $temp_item[$item_k] ?? ['status' => 0, 'config' => ['name' => ''], 'sort' => 0];
  83. $item_v['config'] = $temp_v_item['config'];
  84. $item_v['status'] = $temp_v_item['status'];
  85. $item_v['sort'] = $temp_v_item['sort'];
  86. $channel_list[$k]['pay_type'][$item_k] = $item_v;
  87. }
  88. $temp_pay_type = array_values($channel_list[$k]['pay_type']);
  89. $sort = array_column($temp_pay_type, 'sort');
  90. array_multisort($sort, SORT_ASC, $temp_pay_type);
  91. $channel_list[$k]['pay_type'] = $temp_pay_type;
  92. }
  93. return $channel_list;
  94. }
  95. /**
  96. * 通过渠道获取配置
  97. * @param string $channel
  98. * @return array
  99. * @throws DataNotFoundException
  100. * @throws DbException
  101. * @throws ModelNotFoundException
  102. */
  103. public function getListByChannel(string $channel)
  104. {
  105. $where = [
  106. 'channel' => $channel
  107. ];
  108. return $this->model->where($where)->field('type, channel, config, sort, status')->select()->toArray();
  109. }
  110. /**
  111. * 通过支付方式获取配置格式
  112. * @param $data
  113. * @param $type
  114. * @return array
  115. */
  116. public function getConfigByPayType($data, $type)
  117. {
  118. $config = [];
  119. switch ($type) {
  120. case PayDict::WECHATPAY:
  121. $config = [
  122. 'mch_id' => $data['mch_id'] ?? '',//商户号
  123. 'mch_secret_key' => $data['mch_secret_key'] ?? '',//商户秘钥 现在默认认为是v3版
  124. 'mch_secret_cert' => $data['mch_secret_cert'] ?? '',//商户私钥 字符串或路径
  125. 'mch_public_cert_path' => $data['mch_public_cert_path'] ?? '',//商户公钥证书路径
  126. ];
  127. break;
  128. case PayDict::ALIPAY:
  129. $config = [
  130. 'app_id' => $data['app_id'] ?? '',// 必填-支付宝分配的 app_id
  131. 'app_secret_cert' => $data['app_secret_cert'] ?? '',// 必填-应用私钥 字符串或路径
  132. 'app_public_cert_path' => $data['app_public_cert_path'] ?? '',//必填-应用公钥证书 路径
  133. 'alipay_public_cert_path' => $data['alipay_public_cert_path'] ?? '',//必填-支付宝公钥证书 路径
  134. 'alipay_root_cert_path' => $data['alipay_root_cert_path'] ?? '',// 必填-支付宝根证书 路径
  135. ];
  136. break;
  137. case PayDict::OFFLINEPAY:
  138. $config = [
  139. 'collection_name' => $data['collection_name'] ?? '',// 必填-收款账户名称
  140. 'collection_bank' => $data['collection_bank'] ?? '',//必填-收款银行
  141. 'collection_account' => $data['collection_account'] ?? '',//必填-收款账号
  142. 'collection_desc' => $data['collection_desc'] ?? '',// 必填-转账说明
  143. ];
  144. break;
  145. default:
  146. $config = $data;
  147. }
  148. return $config;
  149. }
  150. /**
  151. * 设置打款设置
  152. * @param $data
  153. * @return true
  154. */
  155. public function setTransfer($data)
  156. {
  157. $wechatpay_config = $data['wechatpay_config'];
  158. $alipay_config = $data['alipay_config'];
  159. $this->set('transfer', PayDict::WECHATPAY, [
  160. 'config' => $wechatpay_config,
  161. 'status' => 1,
  162. ]);
  163. $this->set('transfer', PayDict::ALIPAY, [
  164. 'config' => $alipay_config,
  165. 'status' => 1,
  166. ]);
  167. return true;
  168. }
  169. public function setAll($data){
  170. foreach($data as $k => $v){
  171. $temp_v = $v['pay_type'];
  172. foreach($temp_v as $item_k => $item){
  173. $this->set($k, $item['key'], [
  174. 'config' => $item['config'] ?? [],
  175. 'status' => $item['status'] ?? 0,
  176. 'sort' => $item['sort'] ?? 0,
  177. ]);
  178. }
  179. }
  180. return true;
  181. }
  182. /**
  183. * 查询信息
  184. * @param array $where
  185. * @return mixed
  186. */
  187. public function getInfo($where = [])
  188. {
  189. $where[ 'site_id' ] = $this->site_id;
  190. $res = $this->model->where($where)->field('type, channel, config, sort, status')->findOrEmpty()->toArray();
  191. return $res;
  192. }
  193. }