LakalaOnboardingClient.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace common\components\lakala;
  3. use Lakala\OpenAPISDK\V3\Api\LakalaApi;
  4. use Lakala\OpenAPISDK\V3\Configuration;
  5. use Lakala\OpenAPISDK\V3\Model\ModelRequest;
  6. use Yii;
  7. /**
  8. * 拉卡拉进件客户端
  9. */
  10. class LakalaOnboardingClient
  11. {
  12. private $config;
  13. private $orgCode;
  14. private $userNo;
  15. public function __construct()
  16. {
  17. $params = Yii::$app->params['lakalaOnboarding'] ?? [];
  18. $production = getenv('YII_ENV') === 'production';
  19. $defaults = [
  20. 'app_id' => 'OP00000003',
  21. 'serial_no' => '00dfba8194c41b84cf',
  22. 'sm4_key' => 'LHo55AjrT4aDhAIBZhb5KQ==',
  23. 'org_code' => '1951582',
  24. 'user_no' => '29153396',
  25. 'merchant_private_key_path' => Yii::getAlias('@vendor/lakalasdk/openapi/example/RSAKeys/DEV/OP00000003_private_key.pem'),
  26. 'lkl_certificate_path' => Yii::getAlias('@vendor/lakalasdk/openapi/example/RSAKeys/DEV/lkl-apigw-v2.cer'),
  27. ];
  28. if (!$production) {
  29. $params = array_merge($defaults, $params);
  30. }
  31. if ($production) { // 生产环境才使用配置环境变量
  32. $params = array_merge($params, array_filter([
  33. 'app_id' => getenv('LAKALA_APP_ID'),
  34. 'serial_no' => getenv('LAKALA_SERIAL_NO'),
  35. 'sm4_key' => getenv('LAKALA_SM4_KEY'),
  36. 'org_code' => getenv('LAKALA_ORG_CODE'),
  37. 'user_no' => getenv('LAKALA_USER_NO'),
  38. 'merchant_private_key_path' => Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem',
  39. 'lkl_certificate_path' => Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer',
  40. ]));
  41. }
  42. foreach (['app_id', 'serial_no', 'sm4_key', 'org_code', 'user_no', 'merchant_private_key_path', 'lkl_certificate_path'] as $key) {
  43. if (empty($params[$key])) {
  44. throw new \RuntimeException('缺少拉卡拉进件生产配置:' . $key);
  45. }
  46. }
  47. $this->orgCode = (string)$params['org_code'];
  48. $this->userNo = (string)$params['user_no'];
  49. $this->config = new Configuration([
  50. 'app_debug' => !$production,
  51. 'host_test' => 'https://test.wsmsd.cn/sit',
  52. 'host_pro' => 'https://s2.lakala.com',
  53. 'app_id' => $params['app_id'],
  54. 'serial_no' => $params['serial_no'],
  55. 'sm4_key' => $params['sm4_key'],
  56. 'merchant_private_key_path' => $params['merchant_private_key_path'],
  57. 'lkl_certificate_path' => $params['lkl_certificate_path'],
  58. ]);
  59. }
  60. public function orgCode()
  61. {
  62. return $this->orgCode;
  63. }
  64. public function userNo()
  65. {
  66. return $this->userNo;
  67. }
  68. public function call($path, array $data, $mode = 'none')
  69. {
  70. $api = new LakalaApi($this->config, $mode);
  71. $request = new ModelRequest();
  72. $request->setReqData($data);
  73. try {
  74. $response = $api->tradeApi($path, $request);
  75. $raw = json_decode($response->getOriginalText(), true);
  76. if ($response->getCode() !== '000000' && $response->getCode() !== 'BBS00000') {
  77. throw new \RuntimeException($response->getMsg() ?: '拉卡拉接口请求失败');
  78. }
  79. return [
  80. 'code' => $response->getCode(),
  81. 'msg' => $response->getMsg(),
  82. 'data' => json_decode(json_encode($response->getRespData()), true),
  83. 'raw' => $raw,
  84. ];
  85. } catch (\Throwable $e) {
  86. Yii::error('Lakala onboarding ' . $path . ': ' . $e->getMessage());
  87. //检测 $e 有没有getResponseHeaders方法
  88. if (method_exists($e, 'getResponseHeaders')) {
  89. Yii::error('Lakala onboarding ' . $path . ' headers error: ' . json_encode($e->getResponseHeaders()));
  90. }
  91. throw new \RuntimeException('拉卡拉接口请求失败:' . $e->getMessage());
  92. }
  93. }
  94. public function upload($base64, $imgType, $ocr = false)
  95. {
  96. $base = $base64;
  97. return $this->call('/api/v3/tkbs/customer/file/upload', [
  98. 'file_base64' => $base,
  99. 'img_type' => $imgType,
  100. //'prefix' => 'reg',
  101. 'sourcechnl' => 0,
  102. 'is_ocr' => $ocr ? 'true' : 'false',
  103. ]);
  104. }
  105. public function ocr($batchNo, $imgType)
  106. {
  107. $data = ['batch_no' => $batchNo, 'img_type' => $imgType];
  108. Yii::info(json_encode($data));
  109. return $this->call('/api/v3/tkbs/ocr_result', ['batch_no' => $batchNo, 'img_type' => $imgType]);
  110. }
  111. public function query($path, array $data, $mode = 'none')
  112. {
  113. $data['org_code'] = $data['org_code'] ?? $this->orgCode;
  114. return $this->call($path, $data, $mode);
  115. }
  116. }