Config.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace SixShop\Lakala;
  3. use SixShop\Lakala\Extension;
  4. use SixShop\Lakala\OpenAPISDK\V2\V2Configuration;
  5. use SixShop\Lakala\OpenAPISDK\V3\Configuration;
  6. use SixShop\System\Trait\ConfigTrait;
  7. /**
  8. * @property string $merchant_no 商户ID
  9. * @property string $term_no 终端号
  10. * @property string $notify_url 商户通知地址
  11. * @property string $complete_notify_url 收货确认通知地址
  12. * @property string $environment 环境
  13. * @property string $org_code 机构代码
  14. * @property string $sub_appid 子商户公众账号ID
  15. * @property string $receiver_agreement_file 默认合作协议文件
  16. */
  17. class Config
  18. {
  19. use ConfigTrait;
  20. public function getExtensionID(): string
  21. {
  22. return Extension::EXTENSION_ID;
  23. }
  24. protected function getOptions(): array
  25. {
  26. return [
  27. 'gateway' => [
  28. 'product' => 'https://s2.lakala.com', // 生产网关地址
  29. 'test' => 'https://test.wsmsd.cn/sit',
  30. ],
  31. ];
  32. }
  33. /**
  34. * 获取V3配置
  35. * @return Configuration
  36. */
  37. public function getV3Config(): Configuration
  38. {
  39. return new Configuration($this->getCommonConfig());
  40. }
  41. /**
  42. * 获取V2配置
  43. * @return V2Configuration
  44. */
  45. public function getV2Config(): V2Configuration
  46. {
  47. return new V2Configuration($this->getCommonConfig());
  48. }
  49. private function getCommonConfig(): array
  50. {
  51. $signCert = $this->getConfig('sign_cert');
  52. $verifyCert = $this->getConfig('verify_cert');
  53. $gateway = $this->getConfig('gateway');
  54. // 校验必要字段
  55. if (!is_array($signCert) || !isset($signCert[0])) {
  56. throw new \InvalidArgumentException("sign_cert must be an array and contain at least one element.");
  57. }
  58. if (!is_array($verifyCert) || !isset($verifyCert[0])) {
  59. throw new \InvalidArgumentException("verify_cert must be an array and contain at least one element.");
  60. }
  61. if (!is_array($gateway) || !isset($gateway['test'], $gateway['product'])) {
  62. throw new \InvalidArgumentException("gateway config must contain both 'test' and 'product' keys.");
  63. }
  64. $environment = $this->getConfig('environment');
  65. return [
  66. 'app_id' => $this->getConfig('appid'),
  67. 'serial_no' => $this->getConfig('serial_no'),
  68. 'sm4_key' => null,
  69. 'merchant_private_key_path' => $this->buildPath($signCert[0]),
  70. 'lkl_certificate_path' => $this->buildPath($verifyCert[0]),
  71. 'app_debug' => $environment === 'test',
  72. 'host_test' => $gateway['test'],
  73. 'host_pro' => $gateway['product'],
  74. ];
  75. }
  76. private function buildPath(string $relativePath): string
  77. {
  78. return rtrim(public_path(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . ltrim($relativePath, DIRECTORY_SEPARATOR);
  79. }
  80. }