108 lines
3.9 KiB
PHP
108 lines
3.9 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 Esta extends Base
|
|
{
|
|
public function apply()
|
|
{
|
|
$userId = $this->requireLogin();
|
|
if (!is_int($userId)) {
|
|
return $userId;
|
|
}
|
|
|
|
$data = $this->postData();
|
|
if (empty($data['passport_img'])) {
|
|
return $this->fail('请上传护照照片');
|
|
}
|
|
if (empty($data['person_img'])) {
|
|
return $this->fail('请上传个人照片');
|
|
}
|
|
if ($error = $this->requireFields($data, ['total_price'])) {
|
|
return $error;
|
|
}
|
|
|
|
$context = $this->currentMiniAppContext('esta');
|
|
$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_ORDER_URL, 'post', $payload);
|
|
$result = $this->decodeResponse($response, 'miniapi esta 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['last_name_zn'], $payload['first_name_zn']);
|
|
|
|
$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['product_value'] = $totalPrice;
|
|
$payload['product_name'] = (string)($product['name'] ?? '');
|
|
$payload['product_desc'] = (string)($product['desc'] ?? '');
|
|
$payload['ip'] = getIP();
|
|
$payload['coupon_code'] = $payload['coupon_code'] ?? '';
|
|
$payload['model'] = ESTA_MODEL;
|
|
$payload['token'] = md5(ESTA_MODEL);
|
|
$payload['other_country_acquired_other'] = $payload['other_country_acquired_other'] ?? '';
|
|
|
|
foreach ([
|
|
'birth_date' => 'birth_date',
|
|
'passport_expedition_date' => 'passport_expedition_date',
|
|
'passport_expiration_date' => 'passport_expiration_date',
|
|
'other_country_date' => 'other_country_date',
|
|
'other_country_edate' => 'other_country_edate',
|
|
] 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'] ?: '');
|
|
}
|
|
|
|
unset($payload['other_country_date'], $payload['other_country_edate']);
|
|
|
|
return $payload;
|
|
}
|
|
|
|
private function requestMiniPay(MiniAppContext $context, string $orderSn, $money, string $openid): array
|
|
{
|
|
return (new MiniPayService())->request($context, $orderSn, $money, $openid, 'miniapi esta mini pay');
|
|
}
|
|
}
|