146 lines
5.2 KiB
PHP
146 lines
5.2 KiB
PHP
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\miniapi\controller;
|
|
|
|
use api\Httpcurl;
|
|
use app\miniapi\service\Context\MiniAppContext;
|
|
use app\miniapi\service\Payment\MiniPayService;
|
|
use app\miniapi\service\Product\ProductService;
|
|
|
|
class Evus extends Base
|
|
{
|
|
public function lookup()
|
|
{
|
|
$data = $this->postData();
|
|
if ($error = $this->requireFields($data, [
|
|
'passport_number',
|
|
'visa_foil_number',
|
|
'last_name',
|
|
'first_name',
|
|
'birth_date',
|
|
'country_birth',
|
|
])) {
|
|
return $error;
|
|
}
|
|
|
|
$birth = date_parse($data['birth_date']);
|
|
$params = [
|
|
'passportNum' => $data['passport_number'],
|
|
'b1b2Num' => $data['visa_foil_number'],
|
|
'surname' => $data['last_name'],
|
|
'givenname' => $data['first_name'],
|
|
'year_birthday' => (string)($birth['year'] ?: ''),
|
|
'month_birthday' => (string)($birth['month'] ?: ''),
|
|
'day_birthday' => (string)($birth['day'] ?: ''),
|
|
'country_birth' => $data['country_birth'],
|
|
];
|
|
|
|
$response = Httpcurl::request(config('app.LOOKUP_URL'), 'post', json_encode($params, JSON_UNESCAPED_UNICODE), [
|
|
'Content-Type: application/json',
|
|
]);
|
|
$result = $this->decodeResponse($response, 'miniapi evus lookup');
|
|
if (empty($result)) {
|
|
return $this->fail('服务异常,请稍后重试');
|
|
}
|
|
|
|
return $this->ok($result);
|
|
}
|
|
|
|
public function apply()
|
|
{
|
|
$userId = $this->requireLogin();
|
|
if (!is_int($userId)) {
|
|
return $userId;
|
|
}
|
|
|
|
$data = $this->postData();
|
|
if ($error = $this->requireFields($data, ['total_price'])) {
|
|
return $error;
|
|
}
|
|
|
|
$context = $this->currentMiniAppContext('evus');
|
|
$product = (new ProductService())->findByValue($context, $data['total_price']);
|
|
if (empty($product)) {
|
|
return $this->fail('价格错误');
|
|
}
|
|
$totalPrice = $product['value'];
|
|
|
|
$orderSn = getOrderNumber();
|
|
$payload = $this->buildOrderPayload($data, $orderSn, $product, $userId);
|
|
$response = Httpcurl::request(CREATE_EVUS_ORDER_URL, 'post', $payload);
|
|
$result = $this->decodeResponse($response, 'miniapi evus apply');
|
|
if (empty($result) || intval($result['code'] ?? 0) !== 1) {
|
|
return $this->fail($result['msg'] ?? '提交失败,请联系管理员');
|
|
}
|
|
|
|
if (!empty($data['openid'])) {
|
|
$pay = $this->requestMiniPay($context, $orderSn, $totalPrice, $data['openid']);
|
|
if ($pay['code'] !== 1) {
|
|
return $this->fail($pay['msg']);
|
|
}
|
|
return $this->ok([
|
|
'order_sn' => $orderSn,
|
|
'pay' => $pay['data'],
|
|
], '提交成功');
|
|
}
|
|
|
|
return $this->ok([
|
|
'order_sn' => $orderSn,
|
|
'pay' => null,
|
|
], '提交成功');
|
|
}
|
|
|
|
private function buildOrderPayload(array $data, string $orderSn, array $product, int $userId): array
|
|
{
|
|
$payload = $data;
|
|
unset(
|
|
$payload['token'],
|
|
$payload['openid'],
|
|
$payload['last_name_zn'],
|
|
$payload['first_name_zn'],
|
|
$payload['passport_issue_place'],
|
|
$payload['product_value'],
|
|
$payload['product_name'],
|
|
$payload['product_desc']
|
|
);
|
|
$totalPrice = (string)$product['value'];
|
|
$payload['user_id'] = $userId;
|
|
$payload['order_sn'] = $orderSn;
|
|
$payload['source'] = 'qlwxmini';
|
|
$payload['is_mobile'] = 1;
|
|
$payload['total_price'] = $totalPrice;
|
|
$payload['origin_price'] = $totalPrice;
|
|
$payload['ip'] = getIP();
|
|
$payload['coupon_code'] = $payload['coupon_code'] ?? '';
|
|
$payload['model'] = MODEL;
|
|
$payload['token'] = md5(MODEL);
|
|
|
|
foreach ([
|
|
'birth_date' => 'birth_date',
|
|
'passport_expedition_date' => 'passport_expedition_date',
|
|
'passport_expiration_date' => 'passport_expiration_date',
|
|
'us_visa_passport_issue_date' => 'us_visa_passport_issue_date',
|
|
'us_visa_passport_expiration_date' => 'us_visa_passport_expiration_date',
|
|
'us_visa_birth_date' => 'us_visa_birth_date',
|
|
] as $field => $prefix) {
|
|
$date = date_parse($payload[$field] ?? '');
|
|
$payload[$prefix . '_day'] = (string)($date['day'] ?: '');
|
|
$payload[$prefix . '_month'] = (string)($date['month'] ?: '');
|
|
$payload[$prefix . '_year'] = (string)($date['year'] ?: '');
|
|
}
|
|
|
|
if (isset($payload['vist_country']) && is_array($payload['vist_country'])) {
|
|
$payload = formDataToJson(['vist_country', 'vist_from_date', 'vist_end_date', 'vist_country_des'], $payload, 'vistitem', 'question_i');
|
|
}
|
|
|
|
$payload['other_country_acquired_other'] = $payload['other_country_acquired_other'] ?? '';
|
|
return $payload;
|
|
}
|
|
|
|
private function requestMiniPay(MiniAppContext $context, string $orderSn, $money, string $openid): array
|
|
{
|
|
return (new MiniPayService())->request($context, $orderSn, $money, $openid, 'miniapi evus mini pay');
|
|
}
|
|
}
|