| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace common\components\lakala;
- use Lakala\OpenAPISDK\V3\Api\LakalaApi;
- use Lakala\OpenAPISDK\V3\Configuration;
- use Lakala\OpenAPISDK\V3\Model\ModelRequest;
- use Yii;
- /**
- * 拉卡拉进件客户端
- */
- class LakalaOnboardingClient
- {
- private $config;
- private $orgCode;
- private $userNo;
- public function __construct()
- {
- $params = Yii::$app->params['lakalaOnboarding'] ?? [];
- $production = getenv('YII_ENV') === 'production';
- $defaults = [
- 'app_id' => 'OP00000003',
- 'serial_no' => '00dfba8194c41b84cf',
- 'sm4_key' => 'LHo55AjrT4aDhAIBZhb5KQ==',
- 'org_code' => '1951582',
- 'user_no' => '29153396',
- 'merchant_private_key_path' => Yii::getAlias('@vendor/lakalasdk/openapi/example/RSAKeys/DEV/OP00000003_private_key.pem'),
- 'lkl_certificate_path' => Yii::getAlias('@vendor/lakalasdk/openapi/example/RSAKeys/DEV/lkl-apigw-v2.cer'),
- ];
- if (!$production) {
- $params = array_merge($defaults, $params);
- }
- if ($production) { // 生产环境才使用配置环境变量
- $params = array_merge($params, array_filter([
- 'app_id' => getenv('LAKALA_APP_ID'),
- 'serial_no' => getenv('LAKALA_SERIAL_NO'),
- 'sm4_key' => getenv('LAKALA_SM4_KEY'),
- 'org_code' => getenv('LAKALA_ORG_CODE'),
- 'user_no' => getenv('LAKALA_USER_NO'),
- 'merchant_private_key_path' => Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem',
- 'lkl_certificate_path' => Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer',
- ]));
- }
- foreach (['app_id', 'serial_no', 'sm4_key', 'org_code', 'user_no', 'merchant_private_key_path', 'lkl_certificate_path'] as $key) {
- if (empty($params[$key])) {
- throw new \RuntimeException('缺少拉卡拉进件生产配置:' . $key);
- }
- }
- $this->orgCode = (string)$params['org_code'];
- $this->userNo = (string)$params['user_no'];
- $this->config = new Configuration([
- 'app_debug' => !$production,
- 'host_test' => 'https://test.wsmsd.cn/sit',
- 'host_pro' => 'https://s2.lakala.com',
- 'app_id' => $params['app_id'],
- 'serial_no' => $params['serial_no'],
- 'sm4_key' => $params['sm4_key'],
- 'merchant_private_key_path' => $params['merchant_private_key_path'],
- 'lkl_certificate_path' => $params['lkl_certificate_path'],
- ]);
- }
- public function orgCode()
- {
- return $this->orgCode;
- }
- public function userNo()
- {
- return $this->userNo;
- }
- public function call($path, array $data, $mode = 'none')
- {
- $api = new LakalaApi($this->config, $mode);
- $request = new ModelRequest();
- $request->setReqData($data);
- try {
- $response = $api->tradeApi($path, $request);
- $raw = json_decode($response->getOriginalText(), true);
- if ($response->getCode() !== '000000' && $response->getCode() !== 'BBS00000') {
- throw new \RuntimeException($response->getMsg() ?: '拉卡拉接口请求失败');
- }
- return [
- 'code' => $response->getCode(),
- 'msg' => $response->getMsg(),
- 'data' => json_decode(json_encode($response->getRespData()), true),
- 'raw' => $raw,
- ];
- } catch (\Throwable $e) {
- Yii::error('Lakala onboarding ' . $path . ': ' . $e->getMessage());
- //检测 $e 有没有getResponseHeaders方法
- if (method_exists($e, 'getResponseHeaders')) {
- Yii::error('Lakala onboarding ' . $path . ' headers error: ' . json_encode($e->getResponseHeaders()));
- }
- throw new \RuntimeException('拉卡拉接口请求失败:' . $e->getMessage());
- }
- }
- public function upload($base64, $imgType, $ocr = false)
- {
- $base = $base64;
-
- return $this->call('/api/v3/tkbs/customer/file/upload', [
- 'file_base64' => $base,
- 'img_type' => $imgType,
- //'prefix' => 'reg',
- 'sourcechnl' => 0,
- 'is_ocr' => $ocr ? 'true' : 'false',
- ]);
- }
- public function ocr($batchNo, $imgType)
- {
- $data = ['batch_no' => $batchNo, 'img_type' => $imgType];
- Yii::info(json_encode($data));
- return $this->call('/api/v3/tkbs/ocr_result', ['batch_no' => $batchNo, 'img_type' => $imgType]);
- }
- public function query($path, array $data, $mode = 'none')
- {
- $data['org_code'] = $data['org_code'] ?? $this->orgCode;
- return $this->call($path, $data, $mode);
- }
- }
|