ApiOperations
6 months ago
Apps
6 months ago
Billing
6 months ago
BillingPortal
6 months ago
Checkout
6 months ago
Climate
6 months ago
Entitlements
6 months ago
EventData
6 months ago
Events
6 months ago
Exception
6 months ago
FinancialConnections
6 months ago
Forwarding
6 months ago
HttpClient
6 months ago
Identity
6 months ago
Issuing
6 months ago
Radar
6 months ago
Reporting
6 months ago
Service
6 months ago
Sigma
6 months ago
Tax
6 months ago
Terminal
6 months ago
TestHelpers
6 months ago
Treasury
6 months ago
Util
6 months ago
V2
6 months ago
Account.php
6 months ago
AccountLink.php
6 months ago
AccountSession.php
6 months ago
ApiRequestor.php
6 months ago
ApiResource.php
6 months ago
ApiResponse.php
6 months ago
ApplePayDomain.php
6 months ago
Application.php
6 months ago
ApplicationFee.php
6 months ago
ApplicationFeeRefund.php
6 months ago
Balance.php
6 months ago
BalanceTransaction.php
6 months ago
BankAccount.php
6 months ago
BaseStripeClient.php
6 months ago
BaseStripeClientInterface.php
6 months ago
Capability.php
6 months ago
Card.php
6 months ago
CashBalance.php
6 months ago
Charge.php
6 months ago
Collection.php
6 months ago
ConfirmationToken.php
6 months ago
ConnectCollectionTransfer.php
6 months ago
CountrySpec.php
6 months ago
Coupon.php
6 months ago
CreditNote.php
6 months ago
CreditNoteLineItem.php
6 months ago
Customer.php
6 months ago
CustomerBalanceTransaction.php
6 months ago
CustomerCashBalanceTransaction.php
6 months ago
CustomerSession.php
6 months ago
Discount.php
6 months ago
Dispute.php
6 months ago
EphemeralKey.php
6 months ago
ErrorObject.php
6 months ago
Event.php
6 months ago
ExchangeRate.php
6 months ago
File.php
6 months ago
FileLink.php
6 months ago
FundingInstructions.php
6 months ago
Invoice.php
6 months ago
InvoiceItem.php
6 months ago
InvoiceLineItem.php
6 months ago
InvoicePayment.php
6 months ago
InvoiceRenderingTemplate.php
6 months ago
LineItem.php
6 months ago
LoginLink.php
6 months ago
Mandate.php
6 months ago
OAuth.php
6 months ago
OAuthErrorObject.php
6 months ago
PaymentIntent.php
6 months ago
PaymentLink.php
6 months ago
PaymentMethod.php
6 months ago
PaymentMethodConfiguration.php
6 months ago
PaymentMethodDomain.php
6 months ago
Payout.php
6 months ago
Person.php
6 months ago
Plan.php
6 months ago
Price.php
6 months ago
Product.php
6 months ago
ProductFeature.php
6 months ago
PromotionCode.php
6 months ago
Quote.php
6 months ago
Reason.php
6 months ago
RecipientTransfer.php
6 months ago
Refund.php
6 months ago
RelatedObject.php
6 months ago
RequestTelemetry.php
6 months ago
ReserveTransaction.php
6 months ago
Review.php
6 months ago
SearchResult.php
6 months ago
SetupAttempt.php
6 months ago
SetupIntent.php
6 months ago
ShippingRate.php
6 months ago
SingletonApiResource.php
6 months ago
Source.php
6 months ago
SourceMandateNotification.php
6 months ago
SourceTransaction.php
6 months ago
Stripe.php
6 months ago
StripeClient.php
6 months ago
StripeClientInterface.php
6 months ago
StripeObject.php
6 months ago
StripeStreamingClientInterface.php
6 months ago
Subscription.php
6 months ago
SubscriptionItem.php
6 months ago
SubscriptionSchedule.php
6 months ago
TaxCode.php
6 months ago
TaxDeductedAtSource.php
6 months ago
TaxId.php
6 months ago
TaxRate.php
6 months ago
ThinEvent.php
6 months ago
Token.php
6 months ago
Topup.php
6 months ago
Transfer.php
6 months ago
TransferReversal.php
6 months ago
Webhook.php
6 months ago
WebhookEndpoint.php
6 months ago
WebhookSignature.php
6 months ago
ApiResource.php
101 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaVendor\Stripe; |
| 4 | |
| 5 | /** |
| 6 | * Class ApiResource. |
| 7 | * |
| 8 | * */ |
| 9 | abstract class ApiResource extends StripeObject |
| 10 | { |
| 11 | use \AmeliaVendor\Stripe\ApiOperations\Request; |
| 12 | /** |
| 13 | * @return Util\Set A list of fields that can be their own type of |
| 14 | * API resource (say a nested card under an account for example), and if |
| 15 | * that resource is set, it should be transmitted to the API on a create or |
| 16 | * update. Doing so is not the default behavior because API resources |
| 17 | * should normally be persisted on their own RESTful endpoints. |
| 18 | */ |
| 19 | public static function getSavedNestedResources() |
| 20 | { |
| 21 | static $savedNestedResources = null; |
| 22 | if (null === $savedNestedResources) { |
| 23 | $savedNestedResources = new Util\Set(); |
| 24 | } |
| 25 | return $savedNestedResources; |
| 26 | } |
| 27 | /** |
| 28 | * @var bool A flag that can be set a behavior that will cause this |
| 29 | * resource to be encoded and sent up along with an update of its parent |
| 30 | * resource. This is usually not desirable because resources are updated |
| 31 | * individually on their own endpoints, but there are certain cases, |
| 32 | * replacing a customer's source for example, where this is allowed. |
| 33 | */ |
| 34 | public $saveWithParent = false; |
| 35 | public function __set($k, $v) |
| 36 | { |
| 37 | parent::__set($k, $v); |
| 38 | $v = $this->{$k}; |
| 39 | if (static::getSavedNestedResources()->includes($k) && $v instanceof ApiResource) { |
| 40 | $v->saveWithParent = true; |
| 41 | } |
| 42 | } |
| 43 | /** |
| 44 | * @return ApiResource the refreshed resource |
| 45 | * |
| 46 | * @throws Exception\ApiErrorException |
| 47 | */ |
| 48 | public function refresh() |
| 49 | { |
| 50 | $requestor = new ApiRequestor($this->_opts->apiKey, static::baseUrl()); |
| 51 | $url = $this->instanceUrl(); |
| 52 | list($response, $this->_opts->apiKey) = $requestor->request('get', $url, $this->_retrieveOptions, $this->_opts->headers); |
| 53 | $this->setLastResponse($response); |
| 54 | $this->refreshFrom($response->json, $this->_opts); |
| 55 | return $this; |
| 56 | } |
| 57 | /** |
| 58 | * @return string the base URL for the given class |
| 59 | */ |
| 60 | public static function baseUrl() |
| 61 | { |
| 62 | return Stripe::$apiBase; |
| 63 | } |
| 64 | /** |
| 65 | * @return string the endpoint URL for the given class |
| 66 | */ |
| 67 | public static function classUrl() |
| 68 | { |
| 69 | // Replace dots with slashes for namespaced resources, e.g. if the object's name is |
| 70 | // "foo.bar", then its URL will be "/v1/foo/bars". |
| 71 | /** @phpstan-ignore-next-line */ |
| 72 | $base = \str_replace('.', '/', static::OBJECT_NAME); |
| 73 | return "/v1/{$base}s"; |
| 74 | } |
| 75 | /** |
| 76 | * @param null|string $id the ID of the resource |
| 77 | * |
| 78 | * @return string the instance endpoint URL for the given class |
| 79 | * |
| 80 | * @throws Exception\UnexpectedValueException if $id is null |
| 81 | */ |
| 82 | public static function resourceUrl($id) |
| 83 | { |
| 84 | if (null === $id) { |
| 85 | $class = static::class; |
| 86 | $message = 'Could not determine which URL to request: ' . "{$class} instance has invalid ID: {$id}"; |
| 87 | throw new Exception\UnexpectedValueException($message); |
| 88 | } |
| 89 | $id = \AmeliaVendor\Stripe\Util\Util::utf8($id); |
| 90 | $base = static::classUrl(); |
| 91 | $extn = \urlencode($id); |
| 92 | return "{$base}/{$extn}"; |
| 93 | } |
| 94 | /** |
| 95 | * @return string the full API URL for this API resource |
| 96 | */ |
| 97 | public function instanceUrl() |
| 98 | { |
| 99 | return static::resourceUrl($this['id']); |
| 100 | } |
| 101 | } |