245 行
8.9 KiB
PHP
245 行
8.9 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro_huifu\library\onboard;
|
|
|
|
use addons\shopro_huifu\model\Onboard;
|
|
use think\Db;
|
|
use think\Log;
|
|
|
|
class HuifuOnboardService
|
|
{
|
|
protected $sdk;
|
|
protected $picture;
|
|
protected $basic;
|
|
protected $basicEnt;
|
|
protected $busi;
|
|
protected $query;
|
|
|
|
protected function sdk()
|
|
{
|
|
if (!$this->sdk) {
|
|
$this->sdk = new HuifuOnboardSdk();
|
|
$this->picture = new HuifuOnboardPicture($this->sdk);
|
|
$this->basic = new HuifuOnboardBasicIndv($this->sdk);
|
|
$this->basicEnt = new HuifuOnboardBasicEnt($this->sdk);
|
|
$this->busi = new HuifuOnboardBusiOpen($this->sdk);
|
|
$this->query = new HuifuOnboardQuery($this->sdk);
|
|
}
|
|
return $this->sdk;
|
|
}
|
|
|
|
/**
|
|
* 用户申请时仅保存进件资料,待后台业务审核通过后再提交汇付
|
|
*/
|
|
public function saveDraft($userId, $roleType, $roleRefId, array $formData)
|
|
{
|
|
if (!HuifuOnboardGate::isOnboardRequired()) {
|
|
return null;
|
|
}
|
|
if (!in_array($roleType, ['promoter', 'regional_agent'], true)) {
|
|
throw new \InvalidArgumentException('无效角色类型');
|
|
}
|
|
|
|
$form = HuifuOnboardValidator::validateSubmit($formData);
|
|
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
|
if ($row && $row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
|
return $row;
|
|
}
|
|
if ($row && in_array($row['onboard_status'], [
|
|
Onboard::STATUS_SUBMITTING,
|
|
Onboard::STATUS_BASIC_OK,
|
|
Onboard::STATUS_BUSI_PENDING,
|
|
], true)) {
|
|
throw new \RuntimeException('汇付进件处理中,请耐心等待');
|
|
}
|
|
|
|
if (!$row) {
|
|
$row = Onboard::create([
|
|
'user_id' => $userId,
|
|
'role_type' => $roleType,
|
|
'role_ref_id' => $roleRefId,
|
|
'onboard_status' => Onboard::STATUS_DRAFT,
|
|
]);
|
|
}
|
|
|
|
$row->onboard_status = Onboard::STATUS_DRAFT;
|
|
$row->payload = json_encode($form, JSON_UNESCAPED_UNICODE);
|
|
$row->reject_reason = '';
|
|
$row->save();
|
|
|
|
HuifuOnboardReceiverSync::bindOnboardId($roleType, $roleRefId, $row['id']);
|
|
return $row;
|
|
}
|
|
|
|
/**
|
|
* 后台业务审核通过后,读取 draft 资料提交汇付
|
|
*/
|
|
public function submitFromDraft($userId, $roleType, $roleRefId)
|
|
{
|
|
if (!HuifuOnboardGate::isOnboardRequired()) {
|
|
return null;
|
|
}
|
|
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
|
if (!$row || $row['onboard_status'] !== Onboard::STATUS_DRAFT) {
|
|
return null;
|
|
}
|
|
$payload = json_decode($row['payload'] ?? '', true);
|
|
if (!$payload || !is_array($payload)) {
|
|
throw new \RuntimeException('缺少汇付进件资料,请用户在小程序重新提交');
|
|
}
|
|
return $this->submit($userId, $roleType, $roleRefId, $payload);
|
|
}
|
|
|
|
public function submit($userId, $roleType, $roleRefId, array $formData)
|
|
{
|
|
if (!HuifuOnboardGate::isOnboardRequired()) {
|
|
return null;
|
|
}
|
|
if (!in_array($roleType, ['promoter', 'regional_agent'], true)) {
|
|
throw new \InvalidArgumentException('无效角色类型');
|
|
}
|
|
|
|
$form = HuifuOnboardValidator::validateSubmit($formData);
|
|
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
|
if ($row && $row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
|
return $row;
|
|
}
|
|
if ($row && in_array($row['onboard_status'], [Onboard::STATUS_SUBMITTING, Onboard::STATUS_BUSI_PENDING], true)) {
|
|
throw new \RuntimeException('进件审核中,请耐心等待');
|
|
}
|
|
|
|
if (!$row) {
|
|
$row = Onboard::create([
|
|
'user_id' => $userId,
|
|
'role_type' => $roleType,
|
|
'role_ref_id' => $roleRefId,
|
|
'onboard_status' => Onboard::STATUS_DRAFT,
|
|
]);
|
|
}
|
|
|
|
$row->onboard_status = Onboard::STATUS_SUBMITTING;
|
|
$row->payload = json_encode($form, JSON_UNESCAPED_UNICODE);
|
|
$row->reject_reason = '';
|
|
$row->save();
|
|
|
|
try {
|
|
$this->sdk();
|
|
$fileMap = $this->uploadFormPictures($form);
|
|
$row->file_map = json_encode($fileMap, JSON_UNESCAPED_UNICODE);
|
|
|
|
$accountType = $form['account_type'] ?? 'indv';
|
|
$basicResult = $accountType === 'ent'
|
|
? $this->basicEnt->submit($form, $fileMap)
|
|
: $this->basic->submit($form, $fileMap);
|
|
$row->huifu_id = $basicResult['huifu_id'];
|
|
$row->basic_req_seq_id = $basicResult['req_seq_id'];
|
|
$row->req_date = $basicResult['req_date'];
|
|
$row->req_seq_id = $basicResult['req_seq_id'];
|
|
$row->onboard_status = Onboard::STATUS_BASIC_OK;
|
|
$row->save();
|
|
|
|
$busiResult = $this->busi->submit($row['huifu_id'], $form, $fileMap);
|
|
$row->busi_req_seq_id = $busiResult['req_seq_id'];
|
|
$row->req_seq_id = $busiResult['req_seq_id'];
|
|
$row->apply_no = $busiResult['apply_no'] ?? '';
|
|
$row->onboard_status = Onboard::STATUS_BUSI_PENDING;
|
|
$row->resp_snapshot = json_encode($busiResult['resp'], JSON_UNESCAPED_UNICODE);
|
|
$row->save();
|
|
|
|
HuifuOnboardReceiverSync::bindOnboardId($roleType, $roleRefId, $row['id']);
|
|
$row = $this->query->pollAndUpdate($row);
|
|
|
|
return $row;
|
|
} catch (\Throwable $e) {
|
|
$friendly = HuifuOnboardError::translate($e->getMessage());
|
|
Log::error('[shopro_huifu] onboard submit fail uid=' . $userId . ' ' . $e->getMessage());
|
|
$row->onboard_status = Onboard::STATUS_FAILED;
|
|
$row->reject_reason = $friendly;
|
|
$row->save();
|
|
throw new \RuntimeException($friendly, 0, $e);
|
|
}
|
|
}
|
|
|
|
public function resubmit($userId, $roleType, $roleRefId, array $formData)
|
|
{
|
|
$row = Onboard::findByRole($userId, $roleType, $roleRefId);
|
|
if ($row && $row['onboard_status'] === Onboard::STATUS_APPROVED) {
|
|
return $row;
|
|
}
|
|
if ($row && in_array($row['onboard_status'], [Onboard::STATUS_REJECTED, Onboard::STATUS_FAILED], true)) {
|
|
$row->onboard_status = Onboard::STATUS_DRAFT;
|
|
$row->save();
|
|
}
|
|
return $this->submit($userId, $roleType, $roleRefId, $formData);
|
|
}
|
|
|
|
public function pollPending($limit = 50)
|
|
{
|
|
$list = Onboard::where('onboard_status', 'in', [
|
|
Onboard::STATUS_BUSI_PENDING,
|
|
Onboard::STATUS_BASIC_OK,
|
|
])->limit($limit)->select();
|
|
if (!$list || count($list) === 0) {
|
|
return 0;
|
|
}
|
|
$this->sdk();
|
|
$count = 0;
|
|
foreach ($list as $row) {
|
|
try {
|
|
$this->query->pollAndUpdate($row);
|
|
$count++;
|
|
} catch (\Throwable $e) {
|
|
Log::error('[shopro_huifu] onboard poll fail id=' . $row['id'] . ' ' . $e->getMessage());
|
|
}
|
|
}
|
|
return $count;
|
|
}
|
|
|
|
protected function uploadFormPictures(array $form)
|
|
{
|
|
$fileMap = [];
|
|
$accountType = $form['account_type'] ?? 'indv';
|
|
if ($accountType === 'ent') {
|
|
if (!empty($form['license_url'])) {
|
|
$fileMap['F01'] = $this->picture->uploadByUrl('F01', $form['license_url']);
|
|
}
|
|
if (!empty($form['legal_id_front_url'])) {
|
|
$fileMap['F02'] = $this->picture->uploadByUrl('F02', $form['legal_id_front_url']);
|
|
}
|
|
if (!empty($form['legal_id_back_url'])) {
|
|
$fileMap['F03'] = $this->picture->uploadByUrl('F03', $form['legal_id_back_url']);
|
|
}
|
|
} else {
|
|
$fileMap['F02'] = $this->picture->uploadByUrl('F02', $form['id_card_front_url']);
|
|
$fileMap['F03'] = $this->picture->uploadByUrl('F03', $form['id_card_back_url']);
|
|
}
|
|
if (!empty($form['bank_card_url'])) {
|
|
$fileMap['F13'] = $this->picture->uploadByUrl('F13', $form['bank_card_url']);
|
|
}
|
|
if (($form['bank_account_type'] ?? '0') === '1' && !empty($form['account_permit_url'])) {
|
|
$fileMap['F08'] = $this->picture->uploadByUrl('F08', $form['account_permit_url']);
|
|
}
|
|
return $fileMap;
|
|
}
|
|
|
|
public function handleNotify(array $data)
|
|
{
|
|
$huifuId = trim((string) ($data['huifu_id'] ?? ''));
|
|
$applyNo = trim((string) ($data['apply_no'] ?? ''));
|
|
if ($huifuId === '') {
|
|
return false;
|
|
}
|
|
$query = Onboard::where('huifu_id', $huifuId);
|
|
if ($applyNo !== '') {
|
|
$query->where('apply_no', $applyNo);
|
|
}
|
|
$row = $query->order('id desc')->find();
|
|
if (!$row) {
|
|
return false;
|
|
}
|
|
$this->sdk();
|
|
$this->query->pollAndUpdate($row);
|
|
return true;
|
|
}
|
|
}
|