BillingPortal
2 years ago
Checkout
2 years ago
FinancialConnections
2 years ago
Identity
2 years ago
Issuing
2 years ago
Radar
2 years ago
Reporting
2 years ago
Sigma
2 years ago
Terminal
2 years ago
TestHelpers
2 years ago
AbstractService.php
2 years ago
AbstractServiceFactory.php
4 years ago
AccountLinkService.php
4 years ago
AccountService.php
2 years ago
ApplePayDomainService.php
2 years ago
ApplicationFeeService.php
2 years ago
BalanceService.php
4 years ago
BalanceTransactionService.php
2 years ago
ChargeService.php
2 years ago
CoreServiceFactory.php
2 years ago
CountrySpecService.php
2 years ago
CouponService.php
2 years ago
CreditNoteService.php
2 years ago
CustomerService.php
2 years ago
DisputeService.php
2 years ago
EphemeralKeyService.php
4 years ago
EventService.php
2 years ago
ExchangeRateService.php
2 years ago
FileLinkService.php
2 years ago
FileService.php
2 years ago
InvoiceItemService.php
2 years ago
InvoiceService.php
2 years ago
MandateService.php
4 years ago
OAuthService.php
4 years ago
OrderReturnService.php
2 years ago
OrderService.php
2 years ago
PaymentIntentService.php
2 years ago
PaymentLinkService.php
2 years ago
PaymentMethodService.php
2 years ago
PayoutService.php
2 years ago
PlanService.php
2 years ago
PriceService.php
2 years ago
ProductService.php
2 years ago
PromotionCodeService.php
2 years ago
QuoteService.php
2 years ago
RefundService.php
2 years ago
ReviewService.php
2 years ago
SetupAttemptService.php
2 years ago
SetupIntentService.php
2 years ago
ShippingRateService.php
2 years ago
SkuService.php
2 years ago
SourceService.php
4 years ago
SubscriptionItemService.php
2 years ago
SubscriptionScheduleService.php
2 years ago
SubscriptionService.php
2 years ago
TaxCodeService.php
2 years ago
TaxRateService.php
2 years ago
TokenService.php
4 years ago
TopupService.php
2 years ago
TransferService.php
2 years ago
WebhookEndpointService.php
2 years ago
AbstractService.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Stripe\Service; |
| 4 | |
| 5 | /** |
| 6 | * Abstract base class for all services. |
| 7 | */ |
| 8 | abstract class AbstractService |
| 9 | { |
| 10 | /** |
| 11 | * @var \Stripe\StripeClientInterface |
| 12 | */ |
| 13 | protected $client; |
| 14 | |
| 15 | /** |
| 16 | * @var \Stripe\StripeStreamingClientInterface |
| 17 | */ |
| 18 | protected $streamingClient; |
| 19 | |
| 20 | /** |
| 21 | * Initializes a new instance of the {@link AbstractService} class. |
| 22 | * |
| 23 | * @param \Stripe\StripeClientInterface $client |
| 24 | */ |
| 25 | public function __construct($client) |
| 26 | { |
| 27 | $this->client = $client; |
| 28 | $this->streamingClient = $client; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Gets the client used by this service to send requests. |
| 33 | * |
| 34 | * @return \Stripe\StripeClientInterface |
| 35 | */ |
| 36 | public function getClient() |
| 37 | { |
| 38 | return $this->client; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Gets the client used by this service to send requests. |
| 43 | * |
| 44 | * @return \Stripe\StripeStreamingClientInterface |
| 45 | */ |
| 46 | public function getStreamingClient() |
| 47 | { |
| 48 | return $this->streamingClient; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Translate null values to empty strings. For service methods, |
| 53 | * we interpret null as a request to unset the field, which |
| 54 | * corresponds to sending an empty string for the field to the |
| 55 | * API. |
| 56 | * |
| 57 | * @param null|array $params |
| 58 | */ |
| 59 | private static function formatParams($params) |
| 60 | { |
| 61 | if (null === $params) { |
| 62 | return null; |
| 63 | } |
| 64 | \array_walk_recursive($params, function (&$value, $key) { |
| 65 | if (null === $value) { |
| 66 | $value = ''; |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | return $params; |
| 71 | } |
| 72 | |
| 73 | protected function request($method, $path, $params, $opts) |
| 74 | { |
| 75 | return $this->getClient()->request($method, $path, static::formatParams($params), $opts); |
| 76 | } |
| 77 | |
| 78 | protected function requestStream($method, $path, $readBodyChunkCallable, $params, $opts) |
| 79 | { |
| 80 | return $this->getStreamingClient()->requestStream($method, $path, $readBodyChunkCallable, static::formatParams($params), $opts); |
| 81 | } |
| 82 | |
| 83 | protected function requestCollection($method, $path, $params, $opts) |
| 84 | { |
| 85 | return $this->getClient()->requestCollection($method, $path, static::formatParams($params), $opts); |
| 86 | } |
| 87 | |
| 88 | protected function requestSearchResult($method, $path, $params, $opts) |
| 89 | { |
| 90 | return $this->getClient()->requestSearchResult($method, $path, static::formatParams($params), $opts); |
| 91 | } |
| 92 | |
| 93 | protected function buildPath($basePath, ...$ids) |
| 94 | { |
| 95 | foreach ($ids as $id) { |
| 96 | if (null === $id || '' === \trim($id)) { |
| 97 | $msg = 'The resource ID cannot be null or whitespace.'; |
| 98 | |
| 99 | throw new \Stripe\Exception\InvalidArgumentException($msg); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return \sprintf($basePath, ...\array_map('\urlencode', $ids)); |
| 104 | } |
| 105 | } |
| 106 |