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. protected static function getFacadeAccessor(): string
  20. {
  21. return self::class;
  22. }
  23. public function getBuilderChainable(): BuilderChainable
  24. {
  25. if ($this->builderChainable === null) {
  26. $config = [
  27. 'mchid' => $this->config->mchid,
  28. 'serial' => $this->config->serial_no,
  29. 'privateKey' => $this->config->apiclient_key,
  30. 'certs' => [
  31. $this->config->public_key_id => $this->config->public_key,
  32. ],
  33. ];
  34. if ($this->config->platform_no) {
  35. $config['certs'][$this->config->platform_no] = $this->config->platform_cert;
  36. }
  37. $this->builderChainable = Builder::factory($config);
  38. }
  39. return $this->builderChainable;
  40. }
  41. public function getConfig(): Config
  42. {
  43. return $this->config;
  44. }
  45. public function getMiniApp(): MiniApp
  46. {
  47. if ($this->miniApp === null) {
  48. $this->miniApp = app()->make(MiniApp::class, [
  49. $this->config->app_id,
  50. ]);
  51. }
  52. return $this->miniApp;
  53. }
  54. }