WechatPayBuilder.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\WechatPay;
  4. use SixShop\Wechat\Service\MiniApp;
  5. use think\Facade;
  6. use WeChatPay\Builder;
  7. use WeChatPay\BuilderChainable;
  8. /**
  9. * @method static BuilderChainable getBuilderChainable()
  10. */
  11. class WechatPayBuilder extends Facade
  12. {
  13. public function __construct(
  14. private readonly Config $config,
  15. private ?BuilderChainable $builderChainable = null,
  16. private ?MiniApp $miniApp = null
  17. )
  18. {
  19. }
  20. protected static function getFacadeAccessor(): string
  21. {
  22. return self::class;
  23. }
  24. public function getBuilderChainable(): BuilderChainable
  25. {
  26. if ($this->builderChainable === null) {
  27. $config = [
  28. 'mchid' => $this->config->mchid,
  29. 'serial' => $this->config->serial_no,
  30. 'privateKey' => $this->config->apiclient_key,
  31. 'certs' => [
  32. $this->config->public_key_id => $this->config->public_key,
  33. ],
  34. ];
  35. if ($this->config->platform_no) {
  36. $config['certs'][$this->config->platform_no] = $this->config->platform_cert;
  37. }
  38. $this->builderChainable = Builder::factory($config);
  39. }
  40. return $this->builderChainable;
  41. }
  42. public function getConfig(): Config
  43. {
  44. return $this->config;
  45. }
  46. public function getMiniApp(): MiniApp
  47. {
  48. if ($this->miniApp === null) {
  49. $this->miniApp = app()->make(MiniApp::class, [
  50. $this->config->app_id,
  51. ]);
  52. }
  53. return $this->miniApp;
  54. }
  55. }