WechatPayMiddlewareBuilder.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * WechatPayMiddlewareBuilder
  4. * PHP version 5
  5. *
  6. * @category Class
  7. * @package WechatPay
  8. * @author WeChat Pay Team
  9. * @link https://pay.weixin.qq.com
  10. */
  11. namespace WechatPay\GuzzleMiddleware;
  12. use WechatPay\GuzzleMiddleware\Credentials;
  13. use WechatPay\GuzzleMiddleware\Validator;
  14. use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
  15. use WechatPay\GuzzleMiddleware\Auth\PrivateKeySigner;
  16. use WechatPay\GuzzleMiddleware\Auth\CertificateVerifier;
  17. use WechatPay\GuzzleMiddleware\Auth\WechatPay2Credentials;
  18. use WechatPay\GuzzleMiddleware\Auth\WechatPay2Validator;
  19. /**
  20. * WechatPayMiddlewareBuilder
  21. *
  22. * @category Class
  23. * @package WechatPay
  24. * @author WeChat Pay Team
  25. * @link https://pay.weixin.qq.com
  26. */
  27. class WechatPayMiddlewareBuilder
  28. {
  29. /**
  30. * Merchant credentials
  31. *
  32. * @var Credentials
  33. */
  34. protected $credentials;
  35. /**
  36. * Response Validator
  37. *
  38. * @var Validator
  39. */
  40. protected $validator;
  41. /**
  42. * Constructor
  43. */
  44. public function __construct()
  45. {
  46. }
  47. /**
  48. * Set Merchant Infomation
  49. *
  50. * @param string $merchantId Merchant Id
  51. * @param string $serialNo Merchant Certificate Serial Number
  52. * @param string|resource $privateKey Merchant Certificate Private Key (string - PEM formatted key, or resource - key returned by openssl_get_privatekey)
  53. *
  54. * @return $this
  55. */
  56. public function withMerchant($merchantId, $serialNo, $privateKey)
  57. {
  58. $this->credentials = new WechatPay2Credentials($merchantId,
  59. new PrivateKeySigner($serialNo, $privateKey));
  60. return $this;
  61. }
  62. /**
  63. * Set Merchant Credentials
  64. *
  65. * @param Credentials $credentials Merchant Certificate Credentials
  66. *
  67. * @return $this
  68. */
  69. public function withCredentials(Credentials $credentials)
  70. {
  71. $this->credentials = $credentials;
  72. return $this;
  73. }
  74. /**
  75. * Set WechatPay Certificates Infomation
  76. *
  77. * @param array of string|resource $certifcates WechatPay Certificates (string - PEM formatted \
  78. * certificate, or resource - X.509 certificate resource returned by openssl_x509_read)
  79. *
  80. * @return $this
  81. */
  82. public function withWechatPay(array $certificates)
  83. {
  84. $this->validator = new WechatPay2Validator(new CertificateVerifier($certificates));
  85. return $this;
  86. }
  87. /**
  88. * Set WechatPay Validator
  89. *
  90. * @param Validator $Validator WechatPay Validator
  91. *
  92. * @return $this
  93. */
  94. public function withValidator(Validator $validator)
  95. {
  96. $this->validator = $validator;
  97. return $this;
  98. }
  99. /**
  100. * Build WechatPayMiddleware
  101. *
  102. * @return WechatPayMiddleware
  103. */
  104. public function build()
  105. {
  106. if (!isset($this->credentials)) {
  107. throw new \InvalidArgumentException('商户认证信息(credentials)未设置');
  108. }
  109. if (!isset($this->validator)) {
  110. throw new \InvalidArgumentException('微信支付平台签名验证(validator)未设置');
  111. }
  112. return new WechatPayMiddleware($this->credentials, $this->validator);
  113. }
  114. }