文件

156 行
5.3 KiB
PHP

<?php
namespace addons\shopro_huifu\controller;
use addons\shopro_huifu\library\Bridge;
use addons\shopro_huifu\library\HuifuNotifyParser;
use addons\shopro_huifu\library\onboard\HuifuOnboardGate;
use addons\shopro_huifu\library\onboard\HuifuOnboardPicture;
use addons\shopro_huifu\library\onboard\HuifuOnboardService;
use addons\shopro_huifu\model\Onboard as OnboardModel;
use think\addons\Controller;
class Onboard extends Controller
{
protected $noNeedLogin = ['notify'];
protected $noNeedRight = ['*'];
protected function ensureEnabled()
{
if (!Bridge::isEnabled()) {
$this->error('汇付插件未启用');
}
}
public function config()
{
$this->ensureEnabled();
$this->success('ok', [
'required' => HuifuOnboardGate::isOnboardRequired(),
'account_types' => [
['value' => 'indv', 'label' => '个人'],
['value' => 'ent', 'label' => '个体户/企业'],
],
'bank_account_types' => [
['value' => '0', 'label' => '对私'],
['value' => '1', 'label' => '对公'],
],
]);
}
public function searchBranch()
{
$this->ensureEnabled();
$keyword = $this->request->get('keyword', $this->request->post('keyword', ''));
$cityCode = $this->request->get('city_code', $this->request->post('city_code', ''));
$limit = intval($this->request->get('limit', 20));
$list = \addons\shopro_huifu\library\onboard\HuifuOnboardBankBranch::search($keyword, $cityCode, $limit);
$this->success('ok', ['list' => $list]);
}
public function status()
{
$this->ensureEnabled();
$userId = $this->auth->id;
$roleType = $this->request->get('role_type', 'promoter');
$roleRefId = intval($this->request->get('role_ref_id', $userId));
if ($roleType === 'promoter') {
$roleRefId = $userId;
}
$row = OnboardModel::findByRole($userId, $roleType, $roleRefId);
$this->success('ok', HuifuOnboardGate::publicStatus($row));
}
public function uploadPicture()
{
$this->ensureEnabled();
if (!HuifuOnboardGate::isOnboardRequired()) {
$this->error('当前未开启汇付进件');
}
$fileType = $this->request->post('file_type', 'F02');
$imageUrl = $this->request->post('image_url', '');
if ($imageUrl === '') {
$this->error('请上传图片');
}
try {
$picture = new HuifuOnboardPicture();
$fileId = $picture->uploadByUrl($fileType, $imageUrl);
$this->success('上传成功', ['file_id' => $fileId, 'file_type' => $fileType]);
} catch (\Throwable $e) {
$this->error($e->getMessage());
}
}
public function submit()
{
$this->ensureEnabled();
$userId = $this->auth->id;
$roleType = $this->request->post('role_type', 'promoter');
$roleRefId = intval($this->request->post('role_ref_id', $userId));
if ($roleType === 'promoter') {
$roleRefId = $userId;
}
$form = $this->request->post('huifu_onboard/a', []);
if (!$form) {
$form = $this->request->post('form/a', []);
}
try {
$service = new HuifuOnboardService();
$row = $service->submit($userId, $roleType, $roleRefId, $form);
$this->success('进件已提交', HuifuOnboardGate::publicStatus($row));
} catch (\Throwable $e) {
$this->error($e->getMessage());
}
}
public function resubmit()
{
$this->ensureEnabled();
$userId = $this->auth->id;
$roleType = $this->request->post('role_type', 'promoter');
$roleRefId = intval($this->request->post('role_ref_id', $userId));
if ($roleType === 'promoter') {
$roleRefId = $userId;
}
$form = $this->request->post('huifu_onboard/a', []);
if (!$form) {
$form = $this->request->post('form/a', []);
}
try {
$service = new HuifuOnboardService();
$row = $service->resubmit($userId, $roleType, $roleRefId, $form);
$this->success('进件已重新提交', HuifuOnboardGate::publicStatus($row));
} catch (\Throwable $e) {
$this->error($e->getMessage());
}
}
public function notify()
{
$raw = HuifuNotifyParser::parse($this->request);
try {
$client = new \addons\shopro_huifu\library\HuifuClient();
$sign = HuifuNotifyParser::normalizeSign((string) ($raw['sign'] ?? ''));
$respData = (string) ($raw['resp_data'] ?? '');
if ($respData === '' || $sign === '' || !$client->verifyNotify($respData, $sign)) {
http_response_code(400);
echo '';
return;
}
$data = json_decode($respData, true);
if (!is_array($data)) {
http_response_code(400);
echo '';
return;
}
$service = new HuifuOnboardService();
$service->handleNotify($data);
http_response_code(200);
echo 'success';
} catch (\Throwable $e) {
http_response_code(500);
echo '';
}
}
}