├── .gitignore ├── package.json ├── src ├── Exceptions │ ├── SlowDownException.php │ ├── AccessDeniedException.php │ ├── BaseDeviceFlowException.php │ └── AuthorizationPendingException.php ├── Resource │ ├── Me.php │ ├── ResourceInterface.php │ ├── Users.php │ ├── Bidding.php │ ├── Billing.php │ ├── Sale │ │ ├── Badges │ │ │ ├── Offers.php │ │ │ └── Offers │ │ │ │ └── Campaigns.php │ │ ├── DeliveryMethods.php │ │ ├── SizeTablesTemplates.php │ │ ├── OfferPromotionPackages.php │ │ ├── Offers │ │ │ ├── Rating.php │ │ │ ├── PromoOptionsCommands │ │ │ │ └── Tasks.php │ │ │ ├── PromoOptions.php │ │ │ ├── ShippingRates.php │ │ │ ├── UnfilledParameters.php │ │ │ ├── PromoOptionsModification.php │ │ │ ├── Tags.php │ │ │ └── PromoOptionsCommands.php │ │ ├── TaxSettings.php │ │ ├── AllegroPricesAccountEligibility.php │ │ ├── Categories │ │ │ ├── Parameters.php │ │ │ └── ProductParameters.php │ │ ├── CompatibilityList │ │ │ └── SupportedCategories.php │ │ ├── BadgeCampaigns.php │ │ ├── CompatibilityList.php │ │ ├── CategoryEvents.php │ │ ├── MatchingCategories.php │ │ ├── OfferEvents.php │ │ ├── ProductProposals.php │ │ ├── BadgeOperations.php │ │ ├── CategoryParametersScheduledChanges.php │ │ ├── OfferAdditionalServices │ │ │ ├── Definitions.php │ │ │ └── Groups.php │ │ ├── OfferAdditionalServices.php │ │ ├── UserRatings │ │ │ ├── Answer.php │ │ │ └── Removal.php │ │ ├── CompatibilityListSuggestions.php │ │ ├── AllegroPricesAccountConsent.php │ │ ├── Products.php │ │ ├── ProductOffers │ │ │ └── Operations.php │ │ ├── OfferModificationCommands │ │ │ └── Tasks.php │ │ ├── OfferPriceChangeCommands │ │ │ └── Tasks.php │ │ ├── OfferPublicationCommands │ │ │ └── Tasks.php │ │ ├── ClassifiedsPackages.php │ │ ├── DeliverySettings.php │ │ ├── OfferQuantityChangeCommands │ │ │ └── Tasks.php │ │ ├── Loyalty.php │ │ ├── Loyalty │ │ │ ├── PromotionCampaignApplications.php │ │ │ ├── PromotionCampaigns.php │ │ │ └── Promotions.php │ │ ├── OfferClassifiedsPackages.php │ │ ├── BadgeApplications.php │ │ ├── UserRatings.php │ │ ├── Disputes.php │ │ ├── AllegroPricesOfferConsents.php │ │ ├── Disputes │ │ │ └── Messages.php │ │ ├── SizeTables.php │ │ ├── BlacklistedUsers.php │ │ ├── Categories.php │ │ ├── Badges.php │ │ ├── OfferPriceChangeCommands.php │ │ ├── OfferPublicationCommands.php │ │ ├── OfferAttachments.php │ │ ├── OfferModificationCommands.php │ │ ├── OfferQuantityChangeCommands.php │ │ ├── CompatibleProducts │ │ │ └── Groups.php │ │ ├── OfferContacts.php │ │ ├── ShippingRates.php │ │ ├── ResponsiblePersons.php │ │ ├── ResponsibleProducers.php │ │ ├── OfferTags.php │ │ ├── CompatibleProducts.php │ │ ├── OfferVariants.php │ │ ├── DisputeAttachments.php │ │ ├── Images.php │ │ ├── ProductOffers.php │ │ └── Offers.php │ ├── Account.php │ ├── Charity.php │ ├── Order │ │ ├── EventStats.php │ │ ├── Carriers │ │ │ ├── Allegro.php │ │ │ └── Allegro │ │ │ │ ├── Tracking.php │ │ │ │ └── Points.php │ │ ├── Events.php │ │ ├── CustomerReturns │ │ │ └── Rejection.php │ │ ├── CheckoutForms │ │ │ ├── Fulfillment.php │ │ │ ├── Shipments.php │ │ │ ├── Invoices.php │ │ │ └── Invoices │ │ │ │ └── File.php │ │ ├── LineItemIdMappings.php │ │ ├── Carriers.php │ │ ├── CustomerReturns.php │ │ ├── CheckoutForms.php │ │ └── RefundClaims.php │ ├── Bidding │ │ ├── Offers.php │ │ └── Offers │ │ │ └── Bid.php │ ├── Users │ │ └── RatingsSummary.php │ ├── ParcelManagement │ │ ├── DeliveryServices.php │ │ ├── Parcels │ │ │ ├── Label.php │ │ │ └── Protocol.php │ │ ├── PickupDateProposals.php │ │ ├── Parcels.php │ │ ├── ParcelCancelCommands.php │ │ ├── ParcelCreateCommands.php │ │ └── ParcelPickupRequestCommands.php │ ├── Offers.php │ ├── Messaging │ │ ├── Threads │ │ │ ├── Read.php │ │ │ └── Messages.php │ │ ├── Messages.php │ │ ├── Threads.php │ │ └── MessageAttachments.php │ ├── Messaging.php │ ├── Offers │ │ ├── Listing.php │ │ └── ChangePriceCommands.php │ ├── Pricing │ │ ├── OfferQuotes.php │ │ ├── OfferFeePreview.php │ │ └── FeePreview.php │ ├── Billing │ │ └── BillingEntries.php │ ├── Payments │ │ ├── PaymentIdMappings.php │ │ ├── PaymentOperations.php │ │ └── Refunds.php │ ├── Pricing.php │ ├── Payments.php │ ├── ShipmentManagement │ │ ├── Protocol.php │ │ ├── Shipment.php │ │ ├── DeliveryServices.php │ │ ├── PickupProposals.php │ │ ├── Label.php │ │ ├── PickupCreateCommands.php │ │ ├── ShipmentCancelCommands.php │ │ └── ShipmentCreateCommands.php │ ├── Charity │ │ └── FundraisingCampaigns.php │ ├── AfterSalesServiceConditions.php │ ├── Account │ │ └── AdditionalEmails.php │ ├── ParcelManagement.php │ ├── Order.php │ ├── AfterSalesServiceConditions │ │ ├── Attachments.php │ │ ├── Warranties.php │ │ ├── ReturnPolicies.php │ │ └── ImpliedWarranties.php │ ├── ShipmentManagement.php │ ├── PointsOfService.php │ ├── Sale.php │ └── AbstractResource.php ├── Oauth │ ├── TokenRepositoryInterface.php │ ├── TokenFactoryInterface.php │ ├── TokenFactory.php │ ├── FileTokenRepository.php │ ├── OauthClientInterface.php │ └── OauthClient.php ├── Enum │ ├── EndpointHost.php │ ├── GrantType.php │ └── ContentType.php ├── Model │ ├── CredentialsInterface.php │ ├── DeviceFlowAuthSessionInterface.php │ ├── TokenInterface.php │ ├── Credentials.php │ ├── Token.php │ └── DeviceFlowAuthSession.php ├── Plugin │ ├── OauthUriPlugin.php │ ├── SandboxUriPlugin.php │ ├── AcceptLanguagePlugin.php │ ├── ContentTypePlugin.php │ ├── UriPlugin.php │ └── AuthenticationPlugin.php ├── AllegroApiInterface.php └── AllegroApi.php ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /.idea 3 | composer.lock -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "prettier": { 3 | "phpVersion": "7.1", 4 | "printWidth": 100, 5 | "singleQuote": true 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/Exceptions/SlowDownException.php: -------------------------------------------------------------------------------- 1 | apiGet('/me'); 14 | } 15 | } -------------------------------------------------------------------------------- /src/Resource/ResourceInterface.php: -------------------------------------------------------------------------------- 1 | 4 | * Github: https://github.com/imper86 5 | * Date: 20.10.2019 6 | * Time: 13:30 7 | */ 8 | 9 | namespace Imper86\PhpAllegroApi\Resource; 10 | 11 | interface ResourceInterface 12 | { 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/Oauth/TokenRepositoryInterface.php: -------------------------------------------------------------------------------- 1 | apiGet('/order/event-stats'); 15 | } 16 | } -------------------------------------------------------------------------------- /src/Resource/Bidding/Offers.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/delivery-methods'); 15 | } 16 | } -------------------------------------------------------------------------------- /src/Resource/Sale/SizeTablesTemplates.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/size-tables-templates'); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferPromotionPackages.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/offer-promotion-packages'); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Resource/Sale/Offers/Rating.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/offers/{$offerId}/rating"); 15 | } 16 | } -------------------------------------------------------------------------------- /src/Resource/Users/RatingsSummary.php: -------------------------------------------------------------------------------- 1 | apiGet("/users/{$userId}/ratings-summary"); 15 | } 16 | } -------------------------------------------------------------------------------- /src/Resource/ParcelManagement/DeliveryServices.php: -------------------------------------------------------------------------------- 1 | apiGet('/parcel-management/delivery-services'); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Resource/Sale/TaxSettings.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/tax-settings', ['category.id' => $categoryId]); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Resource/Offers.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/allegro-prices-account-eligibility'); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Resource/Sale/Categories/Parameters.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/categories/{$categoryId}/parameters"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Resource/Messaging/Threads/Read.php: -------------------------------------------------------------------------------- 1 | apiPut(sprintf('/messaging/threads/%s/read', $threadId), $body); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Resource/Order/Carriers/Allegro.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/categories/{$categoryId}/product-parameters"); 15 | } 16 | } -------------------------------------------------------------------------------- /src/Resource/Sale/CompatibilityList/SupportedCategories.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/compatibility-list/supported-categories'); 15 | } 16 | } -------------------------------------------------------------------------------- /src/Resource/Sale/BadgeCampaigns.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/badge-campaigns', null, ContentType::VND_BETA_V1); 16 | } 17 | } -------------------------------------------------------------------------------- /src/Resource/Sale/CompatibilityList.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/offers/promo-options-commands/%s/tasks', $commandId)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Resource/Sale/Offers/PromoOptions.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/offers/%s', $offerId ? "{$offerId}/" : '') . 'promo-options', $query); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Resource/Offers/Listing.php: -------------------------------------------------------------------------------- 1 | apiGet('/offers/listing', $query); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/Order/Events.php: -------------------------------------------------------------------------------- 1 | apiGet('/order/events', $query); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/Pricing/OfferQuotes.php: -------------------------------------------------------------------------------- 1 | apiGet('/pricing/offer-quotes', $query); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/Sale/CategoryEvents.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/category-events', $query); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Resource/Sale/MatchingCategories.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/matching-categories', $query); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferEvents.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/offer-events', $query); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/Sale/Offers/ShippingRates.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/offers/{$offerId}/shipping-rates", null, ContentType::VND_BETA_V1); 16 | } 17 | } -------------------------------------------------------------------------------- /src/Resource/Sale/ProductProposals.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/product-proposals', $body); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/Pricing/OfferFeePreview.php: -------------------------------------------------------------------------------- 1 | apiPost('/pricing/offer-fee-preview', $body); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/Billing/BillingEntries.php: -------------------------------------------------------------------------------- 1 | apiGet('/billing/billing-entries', $query); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/ParcelManagement/Parcels/Label.php: -------------------------------------------------------------------------------- 1 | apiGet('/parcel-management/parcels/label', $query); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Resource/Payments/PaymentIdMappings.php: -------------------------------------------------------------------------------- 1 | apiGet('/payments/payment-id-mappings', $query); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/ParcelManagement/Parcels/Protocol.php: -------------------------------------------------------------------------------- 1 | apiGet('/parcel-management/parcels/protocol', $query); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Resource/Payments/PaymentOperations.php: -------------------------------------------------------------------------------- 1 | apiGet('/payments/payment-operations', $query); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/Pricing.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/offers/unfilled-parameters', $query); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Resource/ParcelManagement/PickupDateProposals.php: -------------------------------------------------------------------------------- 1 | apiGet('/parcel-management/pickup-date-proposals', $query); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Resource/Payments.php: -------------------------------------------------------------------------------- 1 | apiPost('/pricing/fee-preview', $body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Resource/Sale/BadgeOperations.php: -------------------------------------------------------------------------------- 1 | apiGet( 14 | sprintf('/sale/badge-operations/%s', $operationId), 15 | null, 16 | ContentType::VND_BETA_V1 17 | ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Resource/Sale/CategoryParametersScheduledChanges.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/category-parameters-scheduled-changes', $query); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferAdditionalServices/Definitions.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/offer-additional-services/definitions', $query); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferAdditionalServices.php: -------------------------------------------------------------------------------- 1 | apiPut("/sale/user-ratings/{$ratingId}/answer", $body); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Sale/UserRatings/Removal.php: -------------------------------------------------------------------------------- 1 | apiPut("/sale/user-ratings/{$ratingId}/removal", $body); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Plugin/OauthUriPlugin.php: -------------------------------------------------------------------------------- 1 | getUri(); 17 | $uri = $uri->withScheme('https')->withHost(EndpointHost::OAUTH); 18 | 19 | return $next($request->withUri($uri)); 20 | } 21 | } -------------------------------------------------------------------------------- /src/Resource/Sale/CompatibilityListSuggestions.php: -------------------------------------------------------------------------------- 1 | apiGet( 17 | '/sale/compatibility-list-suggestions', 18 | $query 19 | ); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Plugin/SandboxUriPlugin.php: -------------------------------------------------------------------------------- 1 | getUri(); 17 | $uri = $uri->withHost($uri->getHost() . EndpointHost::SANDBOX_SUFFIX); 18 | 19 | return $next($request->withUri($uri)); 20 | } 21 | } -------------------------------------------------------------------------------- /src/Resource/Sale/AllegroPricesAccountConsent.php: -------------------------------------------------------------------------------- 1 | apiPut('/sale/allegro-prices-account-consent', $body); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Order/CustomerReturns/Rejection.php: -------------------------------------------------------------------------------- 1 | apiPost( 14 | sprintf('/order/customer-returns/%s/rejection', $returnId), 15 | $body, 16 | ContentType::VND_BETA_V1 17 | ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Resource/Sale/Products.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/products%s', $productId ? "/{$productId}" : ''), $query); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Order/CheckoutForms/Fulfillment.php: -------------------------------------------------------------------------------- 1 | apiPut("/order/checkout-forms/{$checkoutFormId}/fulfillment", $body); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Sale/ProductOffers/Operations.php: -------------------------------------------------------------------------------- 1 | apiGet( 14 | sprintf('/sale/product-offers/%s/operations/%s', $offerId, $operationId), 15 | null, 16 | ContentType::VND_BETA_V2 17 | ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Resource/Sale/Badges/Offers/Campaigns.php: -------------------------------------------------------------------------------- 1 | apiPatch( 14 | sprintf('/sale/badges/offers/%s/campaigns/%s', $offerId, $campaignId), 15 | $body, 16 | ContentType::VND_BETA_V1 17 | ); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferModificationCommands/Tasks.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/offer-modification-commands/{$commandId}/tasks", $query); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferPriceChangeCommands/Tasks.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/offer-price-change-commands/{$commandId}/tasks", $query); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferPublicationCommands/Tasks.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/offer-publication-commands/{$commandId}/tasks", $query); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Sale/ClassifiedsPackages.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/classifieds-packages%s', $packageId ? "/{$packageId}" : ''), $query); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Sale/DeliverySettings.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/delivery-settings'); 15 | } 16 | 17 | /** 18 | * @param mixed[] $body 19 | * @return ResponseInterface 20 | */ 21 | public function put(array $body): ResponseInterface 22 | { 23 | return $this->apiPut('/sale/delivery-settings', $body); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferQuantityChangeCommands/Tasks.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/offer-quantity-change-commands/{$commandId}/tasks", $query); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Resource/Offers/ChangePriceCommands.php: -------------------------------------------------------------------------------- 1 | apiPut("/offers/{$offerId}/change-price-commands/{$commandId}", $body); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Resource/ParcelManagement/Parcels.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/parcel-management/parcels/%s', $parcelId)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Resource/Messaging/Threads/Messages.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/messaging/threads/%s/messages', $threadId), $query); 13 | } 14 | 15 | public function post(string $threadId, array $body): ResponseInterface 16 | { 17 | return $this->apiPost(sprintf('/messaging/threads/%s/messages', $threadId), $body); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Resource/Sale/Offers/PromoOptionsModification.php: -------------------------------------------------------------------------------- 1 | apiPost( 18 | sprintf('/sale/offers/%s/promo-options-modification', $offerId), 19 | $body 20 | ); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Resource/Order/LineItemIdMappings.php: -------------------------------------------------------------------------------- 1 | apiGet('/order/line-item-id-mappings', $query); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Resource/Order/Carriers.php: -------------------------------------------------------------------------------- 1 | apiGet('/order/carriers'); 20 | } 21 | 22 | public function tracking(): Tracking 23 | { 24 | return new Tracking($this->client); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Enum/ContentType.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/loyalty/promotion-campaign-applications/{$applicationId}"); 15 | } 16 | 17 | public function delete(string $applicationId): ResponseInterface 18 | { 19 | return $this->apiDelete("/sale/loyalty/promotion-campaign-applications/{$applicationId}"); 20 | } 21 | } -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement/Protocol.php: -------------------------------------------------------------------------------- 1 | apiPost('/shipment-management/protocol', $body); 22 | } 23 | } -------------------------------------------------------------------------------- /src/Resource/Sale/Offers/Tags.php: -------------------------------------------------------------------------------- 1 | apiPost("/sale/offers/{$offerId}/tags", $body); 20 | } 21 | 22 | public function get(string $offerId): ResponseInterface 23 | { 24 | return $this->apiGet("/sale/offers/{$offerId}/tags"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Resource/Bidding/Offers/Bid.php: -------------------------------------------------------------------------------- 1 | apiPut("/bidding/offers/{$offerId}/bid", $body); 20 | } 21 | 22 | public function get(string $offerId): ResponseInterface 23 | { 24 | return $this->apiGet("/bidding/offers/{$offerId}/bid"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Plugin/AcceptLanguagePlugin.php: -------------------------------------------------------------------------------- 1 | locale = $locale; 16 | } 17 | 18 | public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise 19 | { 20 | if (!$request->hasHeader('Accept-Language')) { 21 | $request = $request->withHeader('Accept-Language', $this->locale); 22 | } 23 | 24 | return $next($request); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Resource/Order/CustomerReturns.php: -------------------------------------------------------------------------------- 1 | apiGet( 18 | sprintf('/order/customer-returns%s', $returnId ? "/$returnId" : ''), 19 | $query, 20 | ContentType::VND_BETA_V1 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Resource/Messaging/Messages.php: -------------------------------------------------------------------------------- 1 | apiPost('/messaging/messages', $body); 13 | } 14 | 15 | public function get(string $messageId): ResponseInterface 16 | { 17 | return $this->apiGet(sprintf('/messaging/messages/%s', $messageId)); 18 | } 19 | 20 | public function delete(string $messageId): ResponseInterface 21 | { 22 | return $this->apiDelete(sprintf('/messaging/messages/%s', $messageId)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Resource/Messaging/Threads.php: -------------------------------------------------------------------------------- 1 | apiGet( 19 | sprintf('/messaging/threads%s', $threadId ? '/' . $threadId : ''), 20 | $query 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Resource/Payments/Refunds.php: -------------------------------------------------------------------------------- 1 | apiPost('/payments/refunds', $body); 19 | } 20 | 21 | /** 22 | * @param string[]|null $query 23 | * @return ResponseInterface 24 | */ 25 | public function get(?array $query = null): ResponseInterface 26 | { 27 | return $this->apiGet('/payments/refunds', $query); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Resource/Charity/FundraisingCampaigns.php: -------------------------------------------------------------------------------- 1 | apiGet( 19 | sprintf('/charity/fundraising-campaigns%s', $id ? "/{$id}" : ''), 20 | $query, 21 | ContentType::VND_BETA_V1 22 | ); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement/Shipment.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/shipment-management/shipments/%s', $shipmentId)); 22 | } 23 | } -------------------------------------------------------------------------------- /src/Resource/Sale/OfferClassifiedsPackages.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/offer-classifieds-packages/{$offerId}"); 15 | } 16 | 17 | /** 18 | * @param string $offerId 19 | * @param mixed[] $body 20 | * @return ResponseInterface 21 | */ 22 | public function put(string $offerId, array $body): ResponseInterface 23 | { 24 | return $this->apiPut("/sale/offer-classifieds-packages/{$offerId}", $body); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Model/DeviceFlowAuthSessionInterface.php: -------------------------------------------------------------------------------- 1 | hasHeader('Content-Type')) { 17 | $request = $request->withHeader('Content-Type', ContentType::VND_PUBLIC_V1); 18 | } 19 | 20 | if (!$request->hasHeader('Accept')) { 21 | $request = $request->withHeader('Accept', ContentType::VND_PUBLIC_V1); 22 | } 23 | 24 | return $next($request); 25 | } 26 | } -------------------------------------------------------------------------------- /src/Model/TokenInterface.php: -------------------------------------------------------------------------------- 1 | apiGet( 20 | sprintf('/order/carriers/%s/tracking', $carrier), 21 | ['waybill' => $waybills], 22 | ContentType::VND_PUBLIC_V1 23 | ); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Resource/Sale/BadgeApplications.php: -------------------------------------------------------------------------------- 1 | apiGet( 21 | sprintf('/sale/badge-applications%s', $applicationId ? "/{$applicationId}" : ''), 22 | $query, 23 | ContentType::VND_BETA_V1 24 | ); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Resource/AfterSalesServiceConditions.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/user-ratings', $query); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Resource/Order/CheckoutForms/Shipments.php: -------------------------------------------------------------------------------- 1 | apiGet("/order/checkout-forms/{$checkoutFormId}/shipments"); 15 | } 16 | 17 | /** 18 | * @param string $checkoutFormId 19 | * @param mixed[] $body 20 | * @return ResponseInterface 21 | */ 22 | public function post(string $checkoutFormId, array $body): ResponseInterface 23 | { 24 | return $this->apiPost("/order/checkout-forms/{$checkoutFormId}/shipments", $body); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Resource/Sale/Disputes.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/disputes%s', $disputeId ? "/{$disputeId}" : ''), $query); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement/DeliveryServices.php: -------------------------------------------------------------------------------- 1 | apiGet('/shipment-management/delivery-services'); 21 | } 22 | } -------------------------------------------------------------------------------- /src/Resource/Sale/AllegroPricesOfferConsents.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/allegro-prices-offer-consents/%s', $offerId)); 16 | } 17 | 18 | /** 19 | * @param mixed[] $body 20 | * @throws ClientExceptionInterface 21 | */ 22 | public function put(string $offerId, array $body): ResponseInterface 23 | { 24 | return $this->apiPut(sprintf('/sale/allegro-prices-offer-consents/%s', $offerId), $body); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement/PickupProposals.php: -------------------------------------------------------------------------------- 1 | apiPost('/shipment-management/pickup-proposals', $body); 24 | } 25 | } -------------------------------------------------------------------------------- /src/Resource/ParcelManagement/ParcelCancelCommands.php: -------------------------------------------------------------------------------- 1 | apiPut( 18 | sprintf('/parcel-management/parcel-cancel-commands/%s', $commandId), 19 | $body 20 | ); 21 | } 22 | 23 | public function get(string $commandId): ResponseInterface 24 | { 25 | return $this->apiGet(sprintf('/parcel-management/parcel-cancel-commands/%s', $commandId)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Resource/ParcelManagement/ParcelCreateCommands.php: -------------------------------------------------------------------------------- 1 | apiPut( 18 | sprintf('/parcel-management/parcel-create-commands/%s', $commandId), 19 | $body 20 | ); 21 | } 22 | 23 | public function get(string $commandId): ResponseInterface 24 | { 25 | return $this->apiGet(sprintf('/parcel-management/parcel-create-commands/%s', $commandId)); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Resource/Order/Carriers/Allegro/Points.php: -------------------------------------------------------------------------------- 1 | uriFactory->createUri('/order/carriers/ALLEGRO/points'); 16 | $request = $this->requestFactory->createRequest('GET', $uri) 17 | ->withHeader('Content-Type', ContentType::VND_PUBLIC_V1); 18 | 19 | if ($ifModifiedSince) { 20 | $request = $request->withHeader('If-Modified-Since', $ifModifiedSince->format('r')); 21 | } 22 | 23 | return $this->httpClient->sendRequest($request); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Resource/Account/AdditionalEmails.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/account/additional-emails%s', $emailId ? "/{$emailId}" : '')); 15 | } 16 | 17 | /** 18 | * @param mixed[] $body 19 | * @return ResponseInterface 20 | */ 21 | public function post(array $body): ResponseInterface 22 | { 23 | return $this->apiPost('/account/additional-emails', $body); 24 | } 25 | 26 | public function delete(string $emailId): ResponseInterface 27 | { 28 | return $this->apiDelete("/account/additional-emails/{$emailId}"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Resource/ParcelManagement.php: -------------------------------------------------------------------------------- 1 | apiGet("/sale/disputes/{$disputeId}/messages", $query); 20 | } 21 | 22 | /** 23 | * @param string $disputeId 24 | * @param mixed[] $body 25 | * @return ResponseInterface 26 | */ 27 | public function post(string $disputeId, array $body): ResponseInterface 28 | { 29 | return $this->apiPost("/sale/disputes/{$disputeId}/messages", $body); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Resource/Order.php: -------------------------------------------------------------------------------- 1 | apiPut( 18 | sprintf('/parcel-management/parcel-pickup-request-commands/%s', $commandId), 19 | $body 20 | ); 21 | } 22 | 23 | public function get(string $commandId): ResponseInterface 24 | { 25 | return $this->apiGet( 26 | sprintf('/parcel-management/parcel-pickup-request-commands/%s', $commandId) 27 | ); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Resource/Sale/SizeTables.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/size-tables%s', $tableId ? "/{$tableId}" : ''), $query); 18 | } 19 | 20 | public function put(string $tableId, array $body): ResponseInterface 21 | { 22 | return $this->apiPut(sprintf('/sale/size-tables/%s', $tableId), $body); 23 | } 24 | 25 | public function post(array $body): ResponseInterface 26 | { 27 | return $this->apiPost('/sale/size-tables', $body); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement/Label.php: -------------------------------------------------------------------------------- 1 | apiPost( 24 | '/shipment-management/label', 25 | $body, 26 | ContentType::VND_PUBLIC_V1, 27 | ContentType::OCTET_STREAM, 28 | ); 29 | } 30 | } -------------------------------------------------------------------------------- /src/Resource/Sale/BlacklistedUsers.php: -------------------------------------------------------------------------------- 1 | apiGet('/sale/blacklisted-users', $query); 19 | } 20 | 21 | /** 22 | * @param mixed[] $body 23 | * @return ResponseInterface 24 | */ 25 | public function post(array $body): ResponseInterface 26 | { 27 | return $this->apiPost('/sale/blacklisted-users', $body); 28 | } 29 | 30 | public function delete(string $excludedUserId): ResponseInterface 31 | { 32 | return $this->apiDelete("/sale/blacklisted-users/{$excludedUserId}"); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Resource/Sale/Categories.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/categories%s', $categoryId ? "/{$categoryId}" : ''), $query); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Resource/Sale/Badges.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/badges', $body, ContentType::VND_BETA_V1); 24 | } 25 | 26 | /** 27 | * @param string[]|null $query 28 | * @return ResponseInterface 29 | */ 30 | public function get(?array $query = null): ResponseInterface 31 | { 32 | return $this->apiGet('/sale/badges', $query, ContentType::VND_BETA_V1); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "imper86/php-allegro-api", 3 | "description": "PHP SDK for Allegro.pl REST API", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "imper", 9 | "email": "me@imper.info" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=7.4", 14 | "ext-json": "*", 15 | "lcobucci/jwt": "^4.0", 16 | "imper86/http-client-builder": "^3.0" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Imper86\\PhpAllegroApi\\": "src" 21 | } 22 | }, 23 | "require-dev": { 24 | "symfony/var-dumper": "^4.3", 25 | "php-http/guzzle6-adapter": "^2.0", 26 | "http-interop/http-factory-guzzle": "^1.0", 27 | "imper86/vbump": "^1.0", 28 | "phpstan/phpstan": "^0.12.86" 29 | }, 30 | "config": { 31 | "allow-plugins": { 32 | "php-http/discovery": false 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Resource/AfterSalesServiceConditions/Attachments.php: -------------------------------------------------------------------------------- 1 | apiPost('/after-sales-service-conditions/attachments', ['name' => $fileName]); 16 | } 17 | 18 | public function put(string $url, StreamInterface $fileStream, string $contentType = 'application/pdf'): ResponseInterface 19 | { 20 | $uri = $this->uriFactory->createUri($url); 21 | $request = $this->requestFactory->createRequest('PUT', $uri) 22 | ->withBody($fileStream) 23 | ->withHeader('Content-Type', $contentType); 24 | 25 | return $this->httpClient->sendRequest($request); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferPriceChangeCommands.php: -------------------------------------------------------------------------------- 1 | apiPut("/sale/offer-price-change-commands/{$commandId}", $body); 27 | } 28 | 29 | public function get(string $commandId): ResponseInterface 30 | { 31 | return $this->apiGet("/sale/offer-price-change-commands/{$commandId}"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferPublicationCommands.php: -------------------------------------------------------------------------------- 1 | apiPut("/sale/offer-publication-commands/{$commandId}", $body); 27 | } 28 | 29 | public function get(string $commandId): ResponseInterface 30 | { 31 | return $this->apiGet("/sale/offer-publication-commands/{$commandId}"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferAttachments.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/offer-attachments', $body); 20 | } 21 | 22 | public function put(string $attachmentId, StreamInterface $stream): ResponseInterface 23 | { 24 | $uri = $this->uriFactory->createUri("/sale/offer-attachments/{$attachmentId}"); 25 | $request = $this->requestFactory->createRequest('PUT', $uri) 26 | ->withBody($stream) 27 | ->withHeader('Content-Type', 'application/pdf'); 28 | 29 | return $this->httpClient->sendRequest($request); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferModificationCommands.php: -------------------------------------------------------------------------------- 1 | apiPut("/sale/offer-modification-commands/{$commandId}", $body); 27 | } 28 | 29 | public function get(string $commandId): ResponseInterface 30 | { 31 | return $this->apiGet("/sale/offer-modification-commands/{$commandId}"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferQuantityChangeCommands.php: -------------------------------------------------------------------------------- 1 | apiPut("/sale/offer-quantity-change-commands/{$commandId}", $body); 27 | } 28 | 29 | public function get(string $commandId): ResponseInterface 30 | { 31 | return $this->apiGet("/sale/offer-quantity-change-commands/{$commandId}"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Resource/Order/CheckoutForms/Invoices.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/order/checkout-forms/%s/invoices', $checkoutFormId)); 20 | } 21 | 22 | /** 23 | * @param string $checkoutFormId 24 | * @param mixed[] $body 25 | * @return ResponseInterface 26 | */ 27 | public function post(string $checkoutFormId, array $body): ResponseInterface 28 | { 29 | return $this->apiPost(sprintf('/order/checkout-forms/%s/invoices', $checkoutFormId), $body); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Resource/Sale/Offers/PromoOptionsCommands.php: -------------------------------------------------------------------------------- 1 | apiPut(sprintf('/sale/offers/promo-options-commands/%s', $commandId), $body); 25 | } 26 | 27 | public function get(string $commandId): ResponseInterface 28 | { 29 | return $this->apiGet(sprintf('/sale/offers/promo-options-commands/%s', $commandId)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Resource/Order/CheckoutForms.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/order/checkout-forms%s', $id ? "/{$id}" : ''), $query); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Resource/Order/RefundClaims.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/order/refund-claims%s', $claimId ? "/{$claimId}" : ''), $query); 20 | } 21 | 22 | public function delete(string $claimId): ResponseInterface 23 | { 24 | return $this->apiDelete("/order/refund-claims/{$claimId}"); 25 | } 26 | 27 | /** 28 | * @param mixed[] $body 29 | * @return ResponseInterface 30 | */ 31 | public function post(array $body): ResponseInterface 32 | { 33 | return $this->apiPost('/order/refund-claims', $body); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Resource/Sale/CompatibleProducts/Groups.php: -------------------------------------------------------------------------------- 1 | uriFactory->createUri('/sale/compatible-products/groups') 21 | ->withQuery(http_build_query($query)); 22 | $request = $this->requestFactory->createRequest('GET', $uri); 23 | 24 | if ($headers) { 25 | foreach ($headers as $headerName => $headerValue) { 26 | $request = $request->withHeader($headerName, $headerValue); 27 | } 28 | } 29 | 30 | return $this->httpClient->sendRequest($request); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Resource/Sale/Loyalty/PromotionCampaigns.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/loyalty/promotion-campaigns', $body); 19 | } 20 | 21 | /** 22 | * @param string[]|null $query 23 | * @return ResponseInterface 24 | */ 25 | public function get(?array $query = null): ResponseInterface 26 | { 27 | return $this->apiGet('/sale/loyalty/promotion-campaigns', $query); 28 | } 29 | 30 | /** 31 | * @param string[] $query 32 | * @return ResponseInterface 33 | */ 34 | public function delete(array $query): ResponseInterface 35 | { 36 | return $this->apiDelete('/sale/loyalty/promotion-campaigns', $query); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Resource/Sale/Loyalty/Promotions.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/loyalty/promotions', $body); 19 | } 20 | 21 | /** 22 | * @param string|null $promotionId 23 | * @param string[]|null $query 24 | * @return ResponseInterface 25 | */ 26 | public function get(?string $promotionId, ?array $query = null): ResponseInterface 27 | { 28 | return $this->apiGet( 29 | sprintf('/sale/loyalty/promotions%s', $promotionId ? "/{$promotionId}" : ''), 30 | $query 31 | ); 32 | } 33 | 34 | public function delete(string $promotionId): ResponseInterface 35 | { 36 | return $this->apiDelete("/sale/loyalty/promotions/{$promotionId}"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Adrian Szuszkiewicz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferContacts.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/offer-contacts', $body); 19 | } 20 | 21 | /** 22 | * @param string|null $id 23 | * @param string[]|null $query 24 | * @return ResponseInterface 25 | */ 26 | public function get(?string $id, ?array $query = null): ResponseInterface 27 | { 28 | return $this->apiGet(sprintf('/sale/offer-contacts%s', $id ? "/{$id}" : ''), $query); 29 | } 30 | 31 | /** 32 | * @param string $id 33 | * @param mixed[] $body 34 | * @return ResponseInterface 35 | */ 36 | public function put(string $id, array $body): ResponseInterface 37 | { 38 | return $this->apiPut("/sale/offer-contacts/{$id}", $body); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Resource/Sale/ShippingRates.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/shipping-rates%s', $id ? "/{$id}" : ''), $query); 20 | } 21 | 22 | /** 23 | * @param mixed[] $body 24 | * @return ResponseInterface 25 | */ 26 | public function post(array $body): ResponseInterface 27 | { 28 | return $this->apiPost('/sale/shipping-rates', $body); 29 | } 30 | 31 | /** 32 | * @param string $id 33 | * @param mixed[] $body 34 | * @return ResponseInterface 35 | */ 36 | public function put(string $id, array $body): ResponseInterface 37 | { 38 | return $this->apiPut("/sale/shipping-rates/{$id}", $body); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Resource/Sale/ResponsiblePersons.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/responsible-persons', $body); 19 | } 20 | 21 | /** 22 | * @param string[] $query 23 | * @return ResponseInterface 24 | */ 25 | public function get(array $query = []): ResponseInterface 26 | { 27 | return $this->apiGet('/sale/responsible-persons', $query); 28 | } 29 | 30 | /** 31 | * @param string $responsiblePersonId 32 | * @param mixed[] $body 33 | * @return ResponseInterface 34 | */ 35 | public function put(string $responsiblePersonId, array $body): ResponseInterface 36 | { 37 | return $this->apiPut("/sale/responsible-persons/{$responsiblePersonId}", $body); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Resource/Sale/ResponsibleProducers.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/responsible-producers', $body); 19 | } 20 | 21 | /** 22 | * @param string[] $query 23 | * @return ResponseInterface 24 | */ 25 | public function get(array $query = []): ResponseInterface 26 | { 27 | return $this->apiGet('/sale/responsible-producers', $query); 28 | } 29 | 30 | /** 31 | * @param string $responsibleProducerId 32 | * @param mixed[] $body 33 | * @return ResponseInterface 34 | */ 35 | public function put(string $responsibleProducerId, array $body): ResponseInterface 36 | { 37 | return $this->apiPut("/sale/responsible-producers/{$responsibleProducerId}", $body); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferTags.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/offer-tags', $body); 19 | } 20 | 21 | /** 22 | * @param string[] $query 23 | * @return ResponseInterface 24 | */ 25 | public function get(array $query): ResponseInterface 26 | { 27 | return $this->apiGet('/sale/offer-tags', $query); 28 | } 29 | 30 | public function delete(string $tagId): ResponseInterface 31 | { 32 | return $this->apiDelete("/sale/offer-tags/{$tagId}"); 33 | } 34 | 35 | /** 36 | * @param string $tagId 37 | * @param mixed[] $body 38 | * @return ResponseInterface 39 | */ 40 | public function put(string $tagId, array $body): ResponseInterface 41 | { 42 | return $this->apiPut("/sale/offer-tags/{$tagId}", $body); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement.php: -------------------------------------------------------------------------------- 1 | apiPost('/points-of-service', $body); 18 | } 19 | 20 | /** 21 | * @param string|null $id 22 | * @param string[]|null $query 23 | * @return ResponseInterface 24 | */ 25 | public function get(?string $id, ?array $query = null): ResponseInterface 26 | { 27 | return $this->apiGet(sprintf('/points-of-service%s', $id ? "/{$id}" : ''), $query); 28 | } 29 | 30 | /** 31 | * @param string $id 32 | * @param mixed[] $body 33 | * @return ResponseInterface 34 | */ 35 | public function put(string $id, array $body): ResponseInterface 36 | { 37 | return $this->apiPut("/points-of-service/{$id}", $body); 38 | } 39 | 40 | public function delete(string $id): ResponseInterface 41 | { 42 | return $this->apiDelete("/points-of-service/{$id}"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Resource/AfterSalesServiceConditions/Warranties.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/after-sales-service-conditions/warranties%s', $id ? "/{$id}" : ''), $query); 20 | } 21 | 22 | /** 23 | * @param mixed[] $body 24 | * @return ResponseInterface 25 | */ 26 | public function post(array $body): ResponseInterface 27 | { 28 | return $this->apiPost('/after-sales-service-conditions/warranties', $body); 29 | } 30 | 31 | /** 32 | * @param string $id 33 | * @param mixed[] $body 34 | * @return ResponseInterface 35 | */ 36 | public function put(string $id, array $body): ResponseInterface 37 | { 38 | return $this->apiPut("/after-sales-service-conditions/warranties/{$id}", $body); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement/PickupCreateCommands.php: -------------------------------------------------------------------------------- 1 | apiPost('/shipment-management/pickups/create-commands', $body); 22 | } 23 | 24 | /** 25 | * Create pickup command status 26 | * Use this resource to get pickup request status. 27 | * 28 | * @param string $commandId Unique identifier of shipment pickup request command 29 | * @return ResponseInterface 30 | * @throws ClientExceptionInterface 31 | */ 32 | public function get(string $commandId): ResponseInterface 33 | { 34 | return $this->apiGet(sprintf('/shipment-management/pickups/create-commands/%s', $commandId)); 35 | } 36 | } -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement/ShipmentCancelCommands.php: -------------------------------------------------------------------------------- 1 | apiPost('/shipment-management/shipments/cancel-commands/', $body); 23 | } 24 | 25 | 26 | /** 27 | * Get shipment cancellation status 28 | * Use this resource to get parcel cancellation status. 29 | * 30 | * @param string $commandId Unique identifier of shipment cancel command 31 | * @return ResponseInterface 32 | * @throws ClientExceptionInterface 33 | */ 34 | public function get(string $commandId): ResponseInterface 35 | { 36 | return $this->apiGet(sprintf('/shipment-management/shipments/cancel-commands/%s', $commandId)); 37 | } 38 | } -------------------------------------------------------------------------------- /src/Resource/ShipmentManagement/ShipmentCreateCommands.php: -------------------------------------------------------------------------------- 1 | apiPost('/shipment-management/shipments/create-commands', $body); 23 | } 24 | 25 | 26 | /** 27 | * Get shipment creation command status 28 | * Use this resource to get shipment creation status. 29 | * 30 | * @param string $commandId Unique identifier of shipment create command 31 | * @return ResponseInterface 32 | * @throws ClientExceptionInterface 33 | */ 34 | public function get(string $commandId): ResponseInterface 35 | { 36 | return $this->apiGet(sprintf('/shipment-management/shipments/create-commands/%s', $commandId)); 37 | } 38 | } -------------------------------------------------------------------------------- /src/Resource/Sale/CompatibleProducts.php: -------------------------------------------------------------------------------- 1 | uriFactory->createUri('/sale/compatible-products') 29 | ->withQuery(http_build_query($query)); 30 | $request = $this->requestFactory->createRequest('GET', $uri); 31 | 32 | if ($headers) { 33 | foreach ($headers as $headerName => $headerValue) { 34 | $request = $request->withHeader($headerName, $headerValue); 35 | } 36 | } 37 | 38 | return $this->httpClient->sendRequest($request); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferAdditionalServices/Groups.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/offer-additional-services/groups', $body); 19 | } 20 | 21 | /** 22 | * @param string|null $groupId 23 | * @param string[]|null $query 24 | * @return ResponseInterface 25 | */ 26 | public function get(?string $groupId, ?array $query = null): ResponseInterface 27 | { 28 | return $this->apiGet( 29 | sprintf('/sale/offer-additional-services/groups%s', $groupId ? "/{$groupId}" : ''), 30 | $query 31 | ); 32 | } 33 | 34 | /** 35 | * @param string $groupId 36 | * @param mixed[] $body 37 | * @return ResponseInterface 38 | */ 39 | public function put(string $groupId, array $body): ResponseInterface 40 | { 41 | return $this->apiPut("/sale/offer-additional-services/groups/{$groupId}", $body); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Plugin/UriPlugin.php: -------------------------------------------------------------------------------- 1 | 4 | * Github: https://github.com/imper86 5 | * Date: 25.10.2019 6 | * Time: 17:29 7 | */ 8 | 9 | namespace Imper86\PhpAllegroApi\Plugin; 10 | 11 | use Http\Client\Common\Plugin; 12 | use Http\Promise\Promise; 13 | use Imper86\PhpAllegroApi\Enum\EndpointHost; 14 | use Imper86\PhpAllegroApi\Model\CredentialsInterface; 15 | use Psr\Http\Message\RequestInterface; 16 | use Psr\Http\Message\UriFactoryInterface; 17 | 18 | class UriPlugin implements Plugin 19 | { 20 | /** 21 | * @var CredentialsInterface 22 | */ 23 | private $credentials; 24 | /** 25 | * @var UriFactoryInterface 26 | */ 27 | private $uriFactory; 28 | 29 | public function __construct(CredentialsInterface $credentials, UriFactoryInterface $uriFactory) 30 | { 31 | $this->credentials = $credentials; 32 | $this->uriFactory = $uriFactory; 33 | } 34 | 35 | public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise 36 | { 37 | $uri = $request->getUri(); 38 | 39 | if (empty($uri->getHost())) { 40 | $uri = $uri->withHost(EndpointHost::API); 41 | } 42 | 43 | return $next($request->withUri($uri->withScheme('https'))); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Resource/AfterSalesServiceConditions/ReturnPolicies.php: -------------------------------------------------------------------------------- 1 | apiGet( 20 | sprintf('/after-sales-service-conditions/return-policies%s', $id ? "/{$id}" : ''), 21 | $query 22 | ); 23 | } 24 | 25 | /** 26 | * @param mixed[] $body 27 | * @return ResponseInterface 28 | */ 29 | public function post(array $body): ResponseInterface 30 | { 31 | return $this->apiPost('/after-sales-service-conditions/return-policies', $body); 32 | } 33 | 34 | /** 35 | * @param string $id 36 | * @param mixed[] $body 37 | * @return ResponseInterface 38 | */ 39 | public function put(string $id, array $body): ResponseInterface 40 | { 41 | return $this->apiPut("/after-sales-service-conditions/return-policies/{$id}", $body); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Resource/Sale/OfferVariants.php: -------------------------------------------------------------------------------- 1 | apiPut("/sale/offer-variants/{$setId}", $body); 20 | } 21 | 22 | /** 23 | * @param string|null $setId 24 | * @param string[]|null $query 25 | * @return ResponseInterface 26 | */ 27 | public function get(?string $setId, ?array $query = null): ResponseInterface 28 | { 29 | return $this->apiGet(sprintf('/sale/offer-variants%s', $setId ? "/{$setId}" : ''), $query); 30 | } 31 | 32 | public function delete(string $setId): ResponseInterface 33 | { 34 | return $this->apiDelete("/sale/offer-variants/{$setId}"); 35 | } 36 | 37 | /** 38 | * @param mixed[] $body 39 | * @return ResponseInterface 40 | */ 41 | public function post(array $body): ResponseInterface 42 | { 43 | return $this->apiPost('/sale/offer-variants', $body); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Resource/AfterSalesServiceConditions/ImpliedWarranties.php: -------------------------------------------------------------------------------- 1 | apiGet( 20 | sprintf('/after-sales-service-conditions/implied-warranties%s', $id ? "/{$id}" : ''), 21 | $query 22 | ); 23 | } 24 | 25 | /** 26 | * @param mixed[] $body 27 | * @return ResponseInterface 28 | */ 29 | public function post(array $body): ResponseInterface 30 | { 31 | return $this->apiPost('/after-sales-service-conditions/implied-warranties', $body); 32 | } 33 | 34 | /** 35 | * @param string $id 36 | * @param mixed[] $body 37 | * @return ResponseInterface 38 | */ 39 | public function put(string $id, array $body): ResponseInterface 40 | { 41 | return $this->apiPut("/after-sales-service-conditions/implied-warranties/{$id}", $body); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Resource/Messaging/MessageAttachments.php: -------------------------------------------------------------------------------- 1 | apiPost('/messaging/message-attachments', $body); 14 | } 15 | 16 | public function put(string $attachmentId, string $contentType, StreamInterface $body): ResponseInterface 17 | { 18 | $uri = $this->uriFactory->createUri(sprintf('/messaging/message-attachments/%s', $attachmentId)); 19 | $request = $this->requestFactory->createRequest('PUT', $uri) 20 | ->withHeader('Content-Type', $contentType) 21 | ->withBody($body); 22 | 23 | return $this->httpClient->sendRequest($request); 24 | } 25 | 26 | public function get(string $attachmentId): ResponseInterface 27 | { 28 | $uri = $this->uriFactory->createUri(sprintf('/messaging/message-attachments/%s', $attachmentId)); 29 | $request = $this->requestFactory->createRequest('GET', $uri) 30 | ->withHeader('Accept', '*/*'); 31 | 32 | return $this->httpClient->sendRequest($request); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Resource/Sale/DisputeAttachments.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/dispute-attachments', $body); 20 | } 21 | 22 | public function put(string $attachmentId, StreamInterface $stream, string $contentType): ResponseInterface 23 | { 24 | $uri = $this->uriFactory->createUri("/sale/dispute-attachments/{$attachmentId}"); 25 | $request = $this->requestFactory->createRequest('PUT', $uri) 26 | ->withBody($stream) 27 | ->withHeader('Content-Type', $contentType); 28 | 29 | return $this->httpClient->sendRequest($request); 30 | } 31 | 32 | public function get(string $attachmentId): ResponseInterface 33 | { 34 | $uri = $this->uriFactory->createUri("/sale/dispute-attachments/{$attachmentId}"); 35 | $request = $this->requestFactory->createRequest('GET', $uri) 36 | ->withHeader('Accept', '*/*'); 37 | 38 | return $this->httpClient->sendRequest($request); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Resource/Sale/Images.php: -------------------------------------------------------------------------------- 1 | postWithStream( 28 | $this->streamFactory->createStream($encodedBody), 29 | ContentType::VND_PUBLIC_V1 30 | ); 31 | } 32 | 33 | public function postWithStream(StreamInterface $stream, string $contentType): ResponseInterface 34 | { 35 | $uri = $this->uriFactory->createUri('/sale/images') 36 | ->withHost(EndpointHost::UPLOAD); 37 | $request = $this->requestFactory->createRequest('POST', $uri) 38 | ->withBody($stream) 39 | ->withHeader('Content-Type', $contentType); 40 | 41 | return $this->httpClient->sendRequest($request); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Model/Credentials.php: -------------------------------------------------------------------------------- 1 | clientId = $clientId; 29 | $this->clientSecret = $clientSecret; 30 | $this->redirectUri = $redirectUri; 31 | $this->isSandbox = $isSandbox; 32 | } 33 | 34 | /** 35 | * @return string 36 | */ 37 | public function getClientId(): string 38 | { 39 | return $this->clientId; 40 | } 41 | 42 | /** 43 | * @return string 44 | */ 45 | public function getClientSecret(): string 46 | { 47 | return $this->clientSecret; 48 | } 49 | 50 | /** 51 | * @return string 52 | */ 53 | public function getRedirectUri(): string 54 | { 55 | return $this->redirectUri; 56 | } 57 | 58 | /** 59 | * @return bool 60 | */ 61 | public function isSandbox(): bool 62 | { 63 | return $this->isSandbox; 64 | } 65 | } -------------------------------------------------------------------------------- /src/Resource/Sale/ProductOffers.php: -------------------------------------------------------------------------------- 1 | apiPost('/sale/product-offers', $body, $contentType); 25 | } 26 | 27 | /** 28 | * @param string $offerId 29 | * @param mixed[] $body 30 | * @param string $contentType 31 | * @return ResponseInterface 32 | */ 33 | public function patch( 34 | string $offerId, 35 | array $body, 36 | string $contentType = ContentType::VND_PUBLIC_V1 37 | ): ResponseInterface { 38 | return $this->apiPatch(sprintf('/sale/product-offers/%s', $offerId), $body, $contentType); 39 | } 40 | 41 | public function get(string $offerId, string $contentType = ContentType::VND_PUBLIC_V1): ResponseInterface 42 | { 43 | return $this->apiGet( 44 | sprintf('/sale/product-offers/%s', $offerId), 45 | null, 46 | $contentType 47 | ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Oauth/TokenFactory.php: -------------------------------------------------------------------------------- 1 | getBody()->__toString(), true); 18 | $jwtConfig = Configuration::forUnsecuredSigner(); 19 | 20 | $parsed = $jwtConfig->parser()->parse($body['access_token']); 21 | 22 | if (!$parsed instanceof Plain) { 23 | throw new \RuntimeException('Could not parse token'); 24 | } 25 | 26 | $expiry = $parsed->claims()->get('exp'); 27 | 28 | if ($expiry instanceof \DateTimeInterface) { 29 | $expiry = $expiry->getTimestamp(); 30 | } 31 | 32 | return new Token( 33 | [ 34 | 'access_token' => $body['access_token'], 35 | 'refresh_token' => $body['refresh_token'] ?? null, 36 | 'grant_type' => $grantType, 37 | 'expiry' => $expiry, 38 | 'user_id' => $parsed->claims()->has('user_name') ? $parsed->claims()->get('user_name') : null, 39 | 'scope' => $parsed->claims()->has('scope') ? $parsed->claims()->get('scope') : ($body['scope'] ?? null), 40 | ] 41 | ); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Oauth/FileTokenRepository.php: -------------------------------------------------------------------------------- 1 | identifier = $identifier; 18 | $this->workDir = $workDir; 19 | 20 | if (!is_dir($this->workDir)) { 21 | mkdir($this->workDir, 0755, true); 22 | } 23 | } 24 | 25 | public function load(): ?TokenInterface 26 | { 27 | if (!file_exists($this->getPath())) { 28 | return null; 29 | } 30 | 31 | $raw = file_get_contents($this->getPath()); 32 | $json = $raw ? json_decode($raw, true) : null; 33 | 34 | if (!$json) { 35 | if (json_last_error() !== JSON_ERROR_NONE) { 36 | throw new \RuntimeException(json_last_error_msg(), json_last_error()); 37 | } 38 | 39 | throw new \RuntimeException('Couldn\'t fetch token from file'); 40 | } 41 | 42 | return new Token($json); 43 | } 44 | 45 | public function save(TokenInterface $token): void 46 | { 47 | file_put_contents($this->getPath(), json_encode($token->serialize())); 48 | } 49 | 50 | private function getPath(): string 51 | { 52 | return sprintf('%s/%s.json', $this->workDir, $this->identifier); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Resource/Order/CheckoutForms/Invoices/File.php: -------------------------------------------------------------------------------- 1 | requestFactory 22 | ->createRequest( 23 | 'PUT', 24 | sprintf('/order/checkout-forms/%s/invoices/%s/file', $checkoutFormId, $invoiceId) 25 | ) 26 | ->withHeader('Content-Type', 'application/pdf'); 27 | 28 | if ($body instanceof StreamInterface) { 29 | $request = $request->withBody($body); 30 | } elseif (is_resource($body)) { 31 | $request = $request->withBody($this->streamFactory->createStreamFromResource($body)); 32 | } elseif (is_string($body)) { 33 | $request = $request->withBody($this->streamFactory->createStream($body)); 34 | } else { 35 | throw new \InvalidArgumentException( 36 | sprintf('Body must be instance of %s or resource or string', StreamInterface::class) 37 | ); 38 | } 39 | 40 | return $this->httpClient->sendRequest($request); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/Oauth/OauthClientInterface.php: -------------------------------------------------------------------------------- 1 | tokenRepository = $tokenRepository; 24 | $this->oauthClient = $oauthClient; 25 | } 26 | 27 | public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise 28 | { 29 | $token = $this->tokenRepository->load(); 30 | 31 | if ($request->hasHeader('Authorization') || !$token) { 32 | return $next($request); 33 | } 34 | 35 | if ($token->isExpired()) { 36 | $token = $this->handleExpired($token); 37 | $this->tokenRepository->save($token); 38 | } 39 | 40 | return $next($request->withHeader('Authorization', sprintf('Bearer %s', $token->__toString()))); 41 | } 42 | 43 | private function handleExpired(TokenInterface $token): TokenInterface 44 | { 45 | $refreshToken = $token->getRefreshToken(); 46 | 47 | if ($refreshToken) { 48 | return $this->oauthClient->fetchTokenWithRefreshToken($refreshToken); 49 | } 50 | 51 | if (GrantType::CLIENT_CREDENTIALS === $token->getGrantType()) { 52 | return $this->oauthClient->fetchTokenWithClientCredentials(); 53 | } 54 | 55 | throw new RuntimeException("Can't find a way to refresh expired token"); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Resource/Sale/Offers.php: -------------------------------------------------------------------------------- 1 | apiGet(sprintf('/sale/offers%s', $offerId ? "/{$offerId}" : ''), $query); 37 | } 38 | 39 | /** 40 | * @param mixed[] $body 41 | * @return ResponseInterface 42 | */ 43 | public function post(array $body): ResponseInterface 44 | { 45 | return $this->apiPost('/sale/offers', $body); 46 | } 47 | 48 | /** 49 | * @param string $offerId 50 | * @param mixed[] $body 51 | * @return ResponseInterface 52 | */ 53 | public function put(string $offerId, array $body): ResponseInterface 54 | { 55 | return $this->apiPut("/sale/offers/{$offerId}", $body); 56 | } 57 | 58 | public function delete(string $offerId): ResponseInterface 59 | { 60 | return $this->apiDelete("/sale/offers/{$offerId}"); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Model/Token.php: -------------------------------------------------------------------------------- 1 | setRequired(['access_token', 'grant_type', 'expiry']); 25 | $resolver->setDefault('refresh_token', null); 26 | $resolver->setDefault('user_id', null); 27 | $resolver->setDefault('scope', null); 28 | $resolver->setAllowedTypes('access_token', 'string'); 29 | $resolver->setAllowedTypes('grant_type', 'string'); 30 | $resolver->setAllowedTypes('expiry', 'int'); 31 | $resolver->setAllowedTypes('refresh_token', ['null', 'string']); 32 | $resolver->setAllowedTypes('user_id', ['null', 'string']); 33 | 34 | $this->params = $resolver->resolve($params); 35 | } 36 | 37 | public function __toString() 38 | { 39 | return $this->getAccessToken(); 40 | } 41 | 42 | public function getAccessToken(): string 43 | { 44 | return $this->params['access_token']; 45 | } 46 | 47 | public function getRefreshToken(): ?string 48 | { 49 | return $this->params['refresh_token']; 50 | } 51 | 52 | public function getGrantType(): string 53 | { 54 | return $this->params['grant_type']; 55 | } 56 | 57 | public function getUserId(): ?string 58 | { 59 | return $this->params['user_id']; 60 | } 61 | 62 | public function getExpiry(): DateTimeImmutable 63 | { 64 | return new DateTimeImmutable('@' . $this->params['expiry']); 65 | } 66 | 67 | public function isExpired(?DateTimeInterface $now = null): bool 68 | { 69 | if (!$now) { 70 | $now = new DateTimeImmutable(); 71 | } 72 | 73 | return $now > $this->getExpiry(); 74 | } 75 | 76 | /** 77 | * @return string[]|null 78 | */ 79 | public function getScope(): ?array 80 | { 81 | return $this->params['scope'] ? explode(' ', $this->params['scope']) : null; 82 | } 83 | 84 | public function serialize(): array 85 | { 86 | return $this->params; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Model/DeviceFlowAuthSession.php: -------------------------------------------------------------------------------- 1 | deviceCode = $deviceCode; 54 | $this->userCode = $userCode; 55 | $this->verificationUri = $verificationUri; 56 | $this->expiresIn = $expiresIn; 57 | $this->interval = $interval; 58 | $this->verificationUriComplete = $verificationUriComplete; 59 | } 60 | 61 | /** 62 | * @return string 63 | */ 64 | public function getDeviceCode(): string 65 | { 66 | return $this->deviceCode; 67 | } 68 | 69 | /** 70 | * @return string 71 | */ 72 | public function getUserCode(): string 73 | { 74 | return $this->userCode; 75 | } 76 | 77 | /** 78 | * @return string 79 | */ 80 | public function getVerificationUri(): string 81 | { 82 | return $this->verificationUri; 83 | } 84 | 85 | /** 86 | * @return int 87 | */ 88 | public function getExpiresIn(): int 89 | { 90 | return $this->expiresIn; 91 | } 92 | 93 | /** 94 | * @return int 95 | */ 96 | public function getInterval(): int 97 | { 98 | return $this->interval; 99 | } 100 | 101 | /** 102 | * @return string 103 | */ 104 | public function getVerificationUriComplete(): string 105 | { 106 | return $this->verificationUriComplete; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/AllegroApiInterface.php: -------------------------------------------------------------------------------- 1 | 4 | * Github: https://github.com/imper86 5 | * Date: 25.10.2019 6 | * Time: 16:37 7 | */ 8 | 9 | namespace Imper86\PhpAllegroApi; 10 | 11 | use Http\Client\Common\Plugin; 12 | use Imper86\HttpClientBuilder\BuilderInterface; 13 | use Imper86\PhpAllegroApi\Oauth\OauthClientInterface; 14 | use Imper86\PhpAllegroApi\Resource\Account; 15 | use Imper86\PhpAllegroApi\Resource\AfterSalesServiceConditions; 16 | use Imper86\PhpAllegroApi\Resource\Bidding; 17 | use Imper86\PhpAllegroApi\Resource\Billing; 18 | use Imper86\PhpAllegroApi\Resource\Charity; 19 | use Imper86\PhpAllegroApi\Resource\Me; 20 | use Imper86\PhpAllegroApi\Resource\Messaging; 21 | use Imper86\PhpAllegroApi\Resource\Offers; 22 | use Imper86\PhpAllegroApi\Resource\Order; 23 | use Imper86\PhpAllegroApi\Resource\ParcelManagement; 24 | use Imper86\PhpAllegroApi\Resource\Payments; 25 | use Imper86\PhpAllegroApi\Resource\PointsOfService; 26 | use Imper86\PhpAllegroApi\Resource\Pricing; 27 | use Imper86\PhpAllegroApi\Resource\ResourceInterface; 28 | use Imper86\PhpAllegroApi\Resource\Sale; 29 | use Imper86\PhpAllegroApi\Resource\ShipmentManagement; 30 | use Imper86\PhpAllegroApi\Resource\Users; 31 | use Psr\Cache\CacheItemPoolInterface; 32 | 33 | /** 34 | * Interface AsanaInterface 35 | * @package Imper86\AllegroApi 36 | * 37 | * @method Account account() 38 | * @method AfterSalesServiceConditions afterSalesServiceConditions() 39 | * @method Bidding bidding() 40 | * @method Billing billing() 41 | * @method Charity charity() 42 | * @method Me me() 43 | * @method Messaging messaging() 44 | * @method Offers offers() 45 | * @method Order order() 46 | * @method ParcelManagement parcelManagement() 47 | * @method ShipmentManagement shipmentManagement() 48 | * @method Payments payments() 49 | * @method PointsOfService pointsOfService() 50 | * @method Pricing pricing() 51 | * @method Sale sale() 52 | * @method Users users() 53 | */ 54 | interface AllegroApiInterface 55 | { 56 | /** 57 | * @param string $resource 58 | * @return ResourceInterface 59 | */ 60 | public function api(string $resource): ResourceInterface; 61 | 62 | /** 63 | * @return OauthClientInterface 64 | */ 65 | public function oauth(): OauthClientInterface; 66 | 67 | /** 68 | * @return BuilderInterface 69 | */ 70 | public function getBuilder(): BuilderInterface; 71 | 72 | /** 73 | * @param Plugin $plugin 74 | */ 75 | public function addPlugin(Plugin $plugin): void; 76 | 77 | /** 78 | * Fully qualified class name of plugin 79 | * 80 | * @param string $fqcn 81 | */ 82 | public function removePlugin(string $fqcn): void; 83 | 84 | /** 85 | * @param CacheItemPoolInterface $cacheItemPool 86 | * @param mixed[] $config 87 | */ 88 | public function addCache(CacheItemPoolInterface $cacheItemPool, array $config = []): void; 89 | 90 | /** 91 | * Removes cache plugin from http client 92 | */ 93 | public function removeCache(): void; 94 | } 95 | -------------------------------------------------------------------------------- /src/AllegroApi.php: -------------------------------------------------------------------------------- 1 | 4 | * Github: https://github.com/imper86 5 | * Date: 25.10.2019 6 | * Time: 16:42 7 | */ 8 | 9 | namespace Imper86\PhpAllegroApi; 10 | 11 | use Http\Client\Common\Plugin; 12 | use Imper86\HttpClientBuilder\Builder; 13 | use Imper86\HttpClientBuilder\BuilderInterface; 14 | use Imper86\PhpAllegroApi\Model\CredentialsInterface; 15 | use Imper86\PhpAllegroApi\Oauth\OauthClient; 16 | use Imper86\PhpAllegroApi\Oauth\OauthClientInterface; 17 | use Imper86\PhpAllegroApi\Plugin\ContentTypePlugin; 18 | use Imper86\PhpAllegroApi\Plugin\SandboxUriPlugin; 19 | use Imper86\PhpAllegroApi\Plugin\UriPlugin; 20 | use Imper86\PhpAllegroApi\Resource\Account; 21 | use Imper86\PhpAllegroApi\Resource\AfterSalesServiceConditions; 22 | use Imper86\PhpAllegroApi\Resource\Bidding; 23 | use Imper86\PhpAllegroApi\Resource\Billing; 24 | use Imper86\PhpAllegroApi\Resource\Charity; 25 | use Imper86\PhpAllegroApi\Resource\Me; 26 | use Imper86\PhpAllegroApi\Resource\Messaging; 27 | use Imper86\PhpAllegroApi\Resource\Offers; 28 | use Imper86\PhpAllegroApi\Resource\Order; 29 | use Imper86\PhpAllegroApi\Resource\ParcelManagement; 30 | use Imper86\PhpAllegroApi\Resource\Payments; 31 | use Imper86\PhpAllegroApi\Resource\PointsOfService; 32 | use Imper86\PhpAllegroApi\Resource\Pricing; 33 | use Imper86\PhpAllegroApi\Resource\ResourceInterface; 34 | use Imper86\PhpAllegroApi\Resource\Sale; 35 | use Imper86\PhpAllegroApi\Resource\ShipmentManagement; 36 | use Imper86\PhpAllegroApi\Resource\Users; 37 | use InvalidArgumentException; 38 | use Psr\Cache\CacheItemPoolInterface; 39 | 40 | /** 41 | * Class Asana 42 | * @package Imper86\AllegroApi 43 | * 44 | * @method Account account() 45 | * @method AfterSalesServiceConditions afterSalesServiceConditions() 46 | * @method Bidding bidding() 47 | * @method Billing billing() 48 | * @method Charity charity() 49 | * @method Me me() 50 | * @method Messaging messaging() 51 | * @method Offers offers() 52 | * @method Order order() 53 | * @method ParcelManagement parcelManagement() 54 | * @method ShipmentManagement shipmentManagement() 55 | * @method Payments payments() 56 | * @method PointsOfService pointsOfService() 57 | * @method Pricing pricing() 58 | * @method Sale sale() 59 | * @method Users users() 60 | */ 61 | class AllegroApi implements AllegroApiInterface 62 | { 63 | /** 64 | * @var BuilderInterface 65 | */ 66 | private $builder; 67 | /** 68 | * @var CredentialsInterface 69 | */ 70 | private $credentials; 71 | /** 72 | * @var OauthClientInterface|null 73 | */ 74 | private $oauthClient; 75 | 76 | public function __construct(CredentialsInterface $credentials, ?BuilderInterface $builder = null) 77 | { 78 | $this->credentials = $credentials; 79 | $this->builder = $builder ?: new Builder(); 80 | $this->builder->addPlugin(new UriPlugin($credentials, $this->builder->getUriFactory())); 81 | $this->builder->addPlugin(new ContentTypePlugin()); 82 | 83 | if ($credentials->isSandbox()) { 84 | $this->builder->addPlugin(new SandboxUriPlugin()); 85 | } 86 | } 87 | 88 | /** 89 | * @param string $name 90 | * @param mixed[] $arguments 91 | * @return ResourceInterface 92 | */ 93 | public function __call(string $name, array $arguments): ResourceInterface 94 | { 95 | return $this->api($name); 96 | } 97 | 98 | public function api(string $resource): ResourceInterface 99 | { 100 | $className = 'Imper86\\PhpAllegroApi\\Resource\\' . ucfirst($resource); 101 | 102 | if (class_exists($className) && is_subclass_of($className, ResourceInterface::class)) { 103 | return new $className($this); 104 | } 105 | 106 | throw new InvalidArgumentException(sprintf('%s resource not found', $resource)); 107 | } 108 | 109 | public function getBuilder(): BuilderInterface 110 | { 111 | return $this->builder; 112 | } 113 | 114 | public function oauth(): OauthClientInterface 115 | { 116 | if (null === $this->oauthClient) { 117 | $this->oauthClient = new OauthClient($this->credentials); 118 | } 119 | 120 | return $this->oauthClient; 121 | } 122 | 123 | public function addPlugin(Plugin $plugin): void 124 | { 125 | $this->builder->addPlugin($plugin); 126 | } 127 | 128 | public function removePlugin(string $fqcn): void 129 | { 130 | $this->builder->removePlugin($fqcn); 131 | } 132 | 133 | public function addCache(CacheItemPoolInterface $cacheItemPool, array $config = []): void 134 | { 135 | $this->builder->addCache($cacheItemPool, $config); 136 | } 137 | 138 | public function removeCache(): void 139 | { 140 | $this->builder->removeCache(); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Allegro.pl REST API PHP SDK 2 | 3 | ## Upgrading v1.x.x -> v2.x.x -> v3.x.x 4 | 5 | V2 introduced PHP >=7.4 support (8.0 is also supported). 6 | All you need to do is change version in ```composer.json``` 7 | file. All resources and methods are the same as in V1, so 8 | you don't need to update your code. 9 | 10 | V1 will not be maintained anymore, so please upgrade as 11 | fast as you can. 12 | 13 | ## Installation 14 | 15 | Just use composer: 16 | ```sh 17 | composer require imper86/php-allegro-api 18 | ``` 19 | 20 | ### HTTPlug note 21 | This lib uses [HTTPlug](https://github.com/php-http/httplug) 22 | so it doesn't depend on any http client. In order to use this 23 | lib you must have some [PSR-18 http client](https://www.php-fig.org/psr/psr-18) 24 | and [PSR-17 http factories](https://www.php-fig.org/psr/psr-17). 25 | If you don't know which one you shoud install you can require 26 | these: 27 | 28 | ```sh 29 | composer require php-http/guzzle6-adapter http-interop/http-factory-guzzle 30 | ``` 31 | 32 | ## Authentication & usage 33 | Library has a bunch of mechanisms that allows you to forget about 34 | tokens, expirations etc. But in order to start using it you must 35 | authorize user using Oauth flow. 36 | 37 | ```php 38 | use Imper86\PhpAllegroApi\AllegroApi; 39 | use Imper86\PhpAllegroApi\Model\Credentials; 40 | use Imper86\PhpAllegroApi\Oauth\FileTokenRepository; 41 | use Imper86\PhpAllegroApi\Plugin\AuthenticationPlugin; 42 | 43 | // first, create Credentials object 44 | $credentials = new Credentials( 45 | 'your-client-id', 46 | 'your-client-secret', 47 | 'your-redirect-uri', 48 | true //is sandbox 49 | ); 50 | 51 | // create api client 52 | $api = new AllegroApi($credentials); 53 | 54 | // get the authorization URL, and redirect your user: 55 | $state = 'your-random-secret-state'; 56 | header(sprintf('Location: %s', $api->oauth()->getAuthorizationUri(true, $state))); 57 | 58 | /* 59 | * after successfull authorization, user will be refirected to your 60 | * redirect_uri with state and code as query parameters 61 | */ 62 | 63 | // verify the state and fetch token 64 | if ($state !== $_GET['state'] ?? null) { 65 | throw new Exception('CSRF?!'); 66 | } 67 | 68 | $token = $api->oauth()->fetchTokenWithCode($_GET['code']); 69 | 70 | // create TokenRepository object 71 | $tokenRepository = new FileTokenRepository( 72 | $token->getUserId(), 73 | __DIR__ . '/tokens' 74 | ); 75 | $tokenRepository->save($token); 76 | 77 | /* 78 | * You can invent your own TokenRepository, just implement 79 | * Imper86\PhpAllegroApi\Oauth\TokenRepositoryInterface 80 | * You can use your DB, Redis, or anything you want. 81 | */ 82 | 83 | // now you can add AuthenticationPlugin, which will take care 84 | // of maintaining your tokens 85 | 86 | $api->addPlugin(new AuthenticationPlugin($tokenRepository, $api->oauth())); 87 | 88 | // * note: of course you can use your own plugin, or AuthenticationPlugin from HTTPlug library 89 | 90 | // from now you can use these methods on AllegroApi object: 91 | $api->account()->(...); 92 | $api->afterSalesServiceConditions()->(...); 93 | $api->bidding()->(...); 94 | $api->billing()->(...); 95 | $api->me()->(...); 96 | $api->offers()->(...); 97 | $api->order()->(...); 98 | $api->payments()->(...); 99 | $api->pointsOfService()->(...); 100 | $api->pricing()->(...); 101 | $api->sale()->(...); 102 | $api->users()->(...); 103 | 104 | // fast example: 105 | var_dump($api->sale()->offers()->tags()->get('123456')); 106 | ``` 107 | 108 | If you use IDE with typehinting such as PHPStorm, you'll easily 109 | figure it out. If not, please 110 | [take a look in Resource directory](src/Resource) 111 | 112 | ## Device Flow 113 | 114 | ```php 115 | use Imper86\PhpAllegroApi\AllegroApi; 116 | use Imper86\PhpAllegroApi\Model\Credentials; 117 | use Imper86\PhpAllegroApi\Oauth\FileTokenRepository; 118 | use Imper86\PhpAllegroApi\Plugin\AuthenticationPlugin; 119 | 120 | // first, create Credentials object 121 | $credentials = new Credentials( 122 | 'your-client-id', 123 | 'your-client-secret', 124 | 'your-redirect-uri', 125 | true //is sandbox 126 | ); 127 | 128 | // create api client 129 | $api = new AllegroApi($credentials); 130 | 131 | // Create authorization session 132 | $session = $api->oauth()->getDeviceCode(); 133 | 134 | // Provide device code and/or url to user 135 | echo 'Please visit: ' . $session->getVerificationUriComplete(); 136 | 137 | // Poll for authorization result 138 | $interval = $session->getInterval(); 139 | $token = false; 140 | do { 141 | sleep($interval); 142 | $device_code = $session->getDeviceCode(); 143 | try{ 144 | $token = $api->oauth()->fetchTokenWithDeviceCode($device_code); 145 | } catch (AuthorizationPendingException) { 146 | continue; 147 | } catch (SlowDownException) { 148 | $interval++; 149 | continue; 150 | } 151 | } while ($token == false); 152 | 153 | // create TokenRepository object 154 | $tokenRepository = new FileTokenRepository( 155 | $token->getUserId(), 156 | __DIR__ . '/tokens' 157 | ); 158 | $tokenRepository->save($token); 159 | ``` 160 | 161 | ## Contributing 162 | Any help will be very appreciated :) 163 | -------------------------------------------------------------------------------- /src/Resource/Sale.php: -------------------------------------------------------------------------------- 1 | 4 | * Github: https://github.com/imper86 5 | * Date: 20.10.2019 6 | * Time: 14:40 7 | */ 8 | 9 | namespace Imper86\PhpAllegroApi\Resource; 10 | 11 | use Imper86\PhpAllegroApi\AllegroApiInterface; 12 | use Psr\Http\Client\ClientExceptionInterface; 13 | use Psr\Http\Client\ClientInterface as HttpClientInterface; 14 | use Psr\Http\Message\RequestFactoryInterface; 15 | use Psr\Http\Message\RequestInterface; 16 | use Psr\Http\Message\ResponseInterface; 17 | use Psr\Http\Message\StreamFactoryInterface; 18 | use Psr\Http\Message\UriFactoryInterface; 19 | 20 | abstract class AbstractResource implements ResourceInterface 21 | { 22 | protected AllegroApiInterface $client; 23 | protected RequestFactoryInterface $requestFactory; 24 | protected UriFactoryInterface $uriFactory; 25 | protected HttpClientInterface $httpClient; 26 | protected StreamFactoryInterface $streamFactory; 27 | /** 28 | * @var \ReflectionClass 29 | */ 30 | protected \ReflectionClass $reflection; 31 | 32 | public function __construct(AllegroApiInterface $client) 33 | { 34 | $this->client = $client; 35 | $this->requestFactory = $client->getBuilder()->getRequestFactory(); 36 | $this->uriFactory = $client->getBuilder()->getUriFactory(); 37 | $this->streamFactory = $client->getBuilder()->getStreamFactory(); 38 | $this->httpClient = $client->getBuilder()->getHttpClient(); 39 | $this->reflection = new \ReflectionClass($this); 40 | } 41 | 42 | /** 43 | * @param string $name 44 | * @param mixed[] $arguments 45 | * @return ResourceInterface 46 | * @throws \InvalidArgumentException 47 | */ 48 | public function __call(string $name, array $arguments): ResourceInterface 49 | { 50 | $className = $this->reflection->getName() . '\\' . ucfirst($name); 51 | 52 | if (class_exists($className) && is_a($className, ResourceInterface::class, true)) { 53 | return new $className($this->client); 54 | } 55 | 56 | throw new \InvalidArgumentException(sprintf('%s resource not found', $name)); 57 | } 58 | 59 | /** 60 | * @param string $uri 61 | * @param mixed[]|null $query 62 | * @param string|null $contentType 63 | * @return ResponseInterface 64 | * @throws ClientExceptionInterface 65 | */ 66 | protected function apiGet( 67 | string $uri, 68 | ?array $query = null, 69 | ?string $contentType = null 70 | ): ResponseInterface { 71 | $uri = $this->uriFactory->createUri($uri); 72 | 73 | if ( 74 | $query && 75 | ($queryString = preg_replace( 76 | '/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', 77 | '=', 78 | http_build_query($query), 79 | )) 80 | ) { 81 | $uri = $uri->withQuery($queryString); 82 | } 83 | 84 | $request = $this->requestFactory->createRequest('GET', $uri); 85 | 86 | if ($contentType) { 87 | $request = $this->addContentHeaders($request, $contentType); 88 | } 89 | 90 | return $this->httpClient->sendRequest($request); 91 | } 92 | 93 | /** 94 | * @param string $uri 95 | * @param mixed[]|null $body 96 | * @param string|null $contentType 97 | * @return ResponseInterface 98 | * @throws ClientExceptionInterface 99 | */ 100 | protected function apiPost( 101 | string $uri, 102 | ?array $body = null, 103 | ?string $contentType = null, 104 | ?string $accept = null 105 | ): ResponseInterface { 106 | $request = $this->requestFactory->createRequest('POST', $uri); 107 | $encodedBody = json_encode($body); 108 | 109 | if (!$encodedBody) { 110 | throw new \RuntimeException(json_last_error_msg(), json_last_error()); 111 | } 112 | 113 | if ($body) { 114 | $stream = $this->streamFactory->createStream($encodedBody); 115 | $request = $request->withBody($stream); 116 | } 117 | 118 | if ($contentType) { 119 | $request = $this->addContentHeaders($request, $contentType); 120 | } 121 | 122 | if ($accept) { 123 | $request = $this->addAcceptHeaders($request, $accept); 124 | } 125 | 126 | return $this->httpClient->sendRequest($request); 127 | } 128 | 129 | /** 130 | * @param string $uri 131 | * @param mixed[]|null $body 132 | * @param string|null $contentType 133 | * @return ResponseInterface 134 | * @throws ClientExceptionInterface 135 | */ 136 | protected function apiPut( 137 | string $uri, 138 | ?array $body = null, 139 | ?string $contentType = null 140 | ): ResponseInterface { 141 | $request = $this->requestFactory->createRequest('PUT', $uri); 142 | $encodedBody = json_encode($body); 143 | 144 | if (!$encodedBody) { 145 | throw new \RuntimeException(json_last_error_msg(), json_last_error()); 146 | } 147 | 148 | if ($body) { 149 | $stream = $this->streamFactory->createStream($encodedBody); 150 | $request = $request->withBody($stream); 151 | } 152 | 153 | if ($contentType) { 154 | $request = $this->addContentHeaders($request, $contentType); 155 | } 156 | 157 | return $this->httpClient->sendRequest($request); 158 | } 159 | 160 | /** 161 | * @param string $uri 162 | * @param mixed[]|null $body 163 | * @param string|null $contentType 164 | * @return ResponseInterface 165 | * @throws ClientExceptionInterface 166 | */ 167 | protected function apiPatch( 168 | string $uri, 169 | ?array $body = null, 170 | ?string $contentType = null 171 | ): ResponseInterface { 172 | $request = $this->requestFactory->createRequest('PATCH', $uri); 173 | $encodedBody = json_encode($body); 174 | 175 | if (!$encodedBody) { 176 | throw new \RuntimeException(json_last_error_msg(), json_last_error()); 177 | } 178 | 179 | if ($body) { 180 | $stream = $this->streamFactory->createStream($encodedBody); 181 | $request = $request->withBody($stream); 182 | } 183 | 184 | if ($contentType) { 185 | $request = $this->addContentHeaders($request, $contentType); 186 | } 187 | 188 | return $this->httpClient->sendRequest($request); 189 | } 190 | 191 | /** 192 | * @param string $uri 193 | * @param string[]|null $query 194 | * @param string|null $contentType 195 | * @return ResponseInterface 196 | * @throws ClientExceptionInterface 197 | */ 198 | protected function apiDelete( 199 | string $uri, 200 | ?array $query = null, 201 | ?string $contentType = null 202 | ): ResponseInterface { 203 | $uri = $this->uriFactory->createUri($uri); 204 | 205 | if ($query) { 206 | $uri = $uri->withQuery(http_build_query($query)); 207 | } 208 | 209 | $request = $this->requestFactory->createRequest('DELETE', $uri); 210 | 211 | if ($contentType) { 212 | $request = $this->addContentHeaders($request, $contentType); 213 | } 214 | 215 | return $this->httpClient->sendRequest($request); 216 | } 217 | 218 | private function addContentHeaders( 219 | RequestInterface $request, 220 | string $contentType 221 | ): RequestInterface { 222 | return $request 223 | ->withHeader('Content-Type', $contentType) 224 | ->withHeader('Accept', $contentType); 225 | } 226 | 227 | private function addAcceptHeaders( 228 | RequestInterface $request, 229 | string $acceptType 230 | ): RequestInterface { 231 | return $request 232 | ->withHeader('Accept', $acceptType); 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /src/Oauth/OauthClient.php: -------------------------------------------------------------------------------- 1 | 4 | * Github: https://github.com/imper86 5 | * Date: 25.10.2019 6 | * Time: 16:44 7 | */ 8 | 9 | namespace Imper86\PhpAllegroApi\Oauth; 10 | 11 | use Http\Client\Common\Exception\ClientErrorException; 12 | use Http\Client\Common\Plugin; 13 | use Http\Client\Common\Plugin\ErrorPlugin; 14 | use Imper86\HttpClientBuilder\Builder; 15 | use Imper86\HttpClientBuilder\BuilderInterface; 16 | use Imper86\PhpAllegroApi\Enum\ContentType; 17 | use Imper86\PhpAllegroApi\Enum\EndpointHost; 18 | use Imper86\PhpAllegroApi\Enum\GrantType; 19 | use Imper86\PhpAllegroApi\Exceptions\AccessDeniedException; 20 | use Imper86\PhpAllegroApi\Exceptions\AuthorizationPendingException; 21 | use Imper86\PhpAllegroApi\Exceptions\SlowDownException; 22 | use Imper86\PhpAllegroApi\Model\CredentialsInterface; 23 | use Imper86\PhpAllegroApi\Model\DeviceFlowAuthSession; 24 | use Imper86\PhpAllegroApi\Model\TokenInterface; 25 | use Imper86\PhpAllegroApi\Plugin\OauthUriPlugin; 26 | use Imper86\PhpAllegroApi\Plugin\SandboxUriPlugin; 27 | use Psr\Http\Client\ClientExceptionInterface; 28 | use Psr\Http\Message\RequestInterface; 29 | use Psr\Http\Message\UriInterface; 30 | 31 | use function http_build_query; 32 | 33 | class OauthClient implements OauthClientInterface 34 | { 35 | private CredentialsInterface $credentials; 36 | private BuilderInterface $builder; 37 | private TokenFactoryInterface $tokenFactory; 38 | 39 | public function __construct(CredentialsInterface $credentials) 40 | { 41 | $this->credentials = $credentials; 42 | $this->tokenFactory = new TokenFactory(); 43 | $this->builder = new Builder(); 44 | $this->builder->addPlugin(new ErrorPlugin()); 45 | $this->builder->addPlugin(new OauthUriPlugin()); 46 | 47 | if ($credentials->isSandbox()) { 48 | $this->builder->addPlugin(new SandboxUriPlugin()); 49 | } 50 | } 51 | 52 | /** 53 | * @param bool $prompt 54 | * @param string|null $state 55 | * @param string[]|null $scope 56 | * @return UriInterface 57 | */ 58 | public function getAuthorizationUri( 59 | bool $prompt = true, 60 | ?string $state = null, 61 | ?array $scope = null 62 | ): UriInterface { 63 | $query = [ 64 | 'client_id' => $this->credentials->getClientId(), 65 | 'redirect_uri' => $this->credentials->getRedirectUri(), 66 | 'response_type' => 'code', 67 | ]; 68 | 69 | if ($state) { 70 | $query['state'] = $state; 71 | } 72 | 73 | if ($prompt) { 74 | $query['prompt'] = 'confirm'; 75 | } 76 | 77 | if ($scope) { 78 | $query['scope'] = implode(' ', $scope); 79 | } 80 | 81 | $uri = $this->builder 82 | ->getUriFactory() 83 | ->createUri('/auth/oauth/authorize') 84 | ->withScheme('https') 85 | ->withHost(EndpointHost::OAUTH) 86 | ->withQuery(http_build_query($query)); 87 | 88 | if ($this->credentials->isSandbox()) { 89 | $uri = $uri->withHost($uri->getHost() . EndpointHost::SANDBOX_SUFFIX); 90 | } 91 | 92 | return $uri; 93 | } 94 | 95 | public function getDeviceCode( 96 | ?array $scope = null 97 | ): DeviceFlowAuthSession { 98 | $query = [ 99 | 'client_id' => $this->credentials->getClientId(), 100 | ]; 101 | 102 | if ($scope) { 103 | $query['scope'] = implode(' ', $scope); 104 | } 105 | 106 | $response = $this->builder->getHttpClient()->sendRequest($this->generateRequest($query, '/auth/oauth/device')); 107 | $body = json_decode($response->getBody()->__toString(), true); 108 | 109 | return new DeviceFlowAuthSession($body['device_code'], $body['expires_in'], $body['user_code'], $body['interval'], $body['verification_uri'], $body['verification_uri_complete']); 110 | } 111 | 112 | public function fetchTokenWithDeviceCode(string $code): TokenInterface 113 | { 114 | $body = [ 115 | 'grant_type' => GrantType::DEVICE_CODE, 116 | 'device_code' => $code, 117 | ]; 118 | 119 | try { 120 | $response = $this->builder->getHttpClient()->sendRequest($this->generateRequestWithBody($body, '/auth/oauth/token')); 121 | } catch (ClientErrorException $clientErrorException) { 122 | $response = $clientErrorException->getResponse(); 123 | $body = json_decode($response->getBody()->__toString(), true); 124 | 125 | if ($body['error'] == 'slow_down') { 126 | throw new SlowDownException($body['error_description'], 0, $clientErrorException); 127 | } elseif ($body['error'] == 'authorization_pending') { 128 | throw new AuthorizationPendingException($body['error_description'], 0, $clientErrorException); 129 | } elseif ($body['error'] == 'access_denied') { 130 | throw new AccessDeniedException($body['error_description'], 0, $clientErrorException); 131 | } else { 132 | throw $clientErrorException; 133 | } 134 | } 135 | 136 | return $this->tokenFactory->createFromResponse($response, GrantType::DEVICE_CODE); 137 | } 138 | 139 | public function fetchTokenWithCode(string $code): TokenInterface 140 | { 141 | $body = [ 142 | 'grant_type' => GrantType::AUTHORIZATION_CODE, 143 | 'code' => $code, 144 | 'redirect_uri' => $this->credentials->getRedirectUri(), 145 | ]; 146 | 147 | $response = $this->builder->getHttpClient()->sendRequest($this->generateRequestWithBody($body, '/auth/oauth/token')); 148 | 149 | return $this->tokenFactory->createFromResponse($response, GrantType::AUTHORIZATION_CODE); 150 | } 151 | 152 | public function fetchTokenWithRefreshToken(string $refreshToken): TokenInterface 153 | { 154 | $body = [ 155 | 'grant_type' => GrantType::REFRESH_TOKEN, 156 | 'refresh_token' => $refreshToken, 157 | 'redirect_uri' => $this->credentials->getRedirectUri(), 158 | ]; 159 | 160 | $response = $this->builder->getHttpClient()->sendRequest($this->generateRequestWithBody($body, '/auth/oauth/token')); 161 | 162 | return $this->tokenFactory->createFromResponse($response, GrantType::REFRESH_TOKEN); 163 | } 164 | 165 | public function fetchTokenWithClientCredentials(): TokenInterface 166 | { 167 | $body = ['grant_type' => GrantType::CLIENT_CREDENTIALS]; 168 | $response = $this->builder->getHttpClient()->sendRequest($this->generateRequestWithBody($body, '/auth/oauth/token')); 169 | 170 | return $this->tokenFactory->createFromResponse($response, GrantType::CLIENT_CREDENTIALS); 171 | } 172 | 173 | public function addPlugin(Plugin $plugin): void 174 | { 175 | $this->builder->addPlugin($plugin); 176 | } 177 | 178 | public function removePlugin(string $fqcn): void 179 | { 180 | $this->builder->removePlugin($fqcn); 181 | } 182 | 183 | /** 184 | * @param string[] $query 185 | * @param string $path 186 | * @return RequestInterface 187 | */ 188 | private function generateRequest(array $query, string $path): RequestInterface 189 | { 190 | $uri = $this->builder 191 | ->getUriFactory() 192 | ->createUri($path) 193 | ->withQuery(http_build_query($query)); 194 | $auth = base64_encode( 195 | sprintf( 196 | '%s:%s', 197 | $this->credentials->getClientId(), 198 | $this->credentials->getClientSecret() 199 | ) 200 | ); 201 | 202 | return $this->builder 203 | ->getRequestFactory() 204 | ->createRequest('POST', $uri) 205 | ->withHeader('Content-Type', ContentType::X_WWW_FORM_URLENCODED) 206 | ->withHeader('Accept', ContentType::JSON) 207 | ->withHeader('Authorization', sprintf('Basic %s', $auth)); 208 | } 209 | 210 | /** 211 | * @param string[] $body 212 | * @param string $path 213 | * @return RequestInterface 214 | */ 215 | private function generateRequestWithBody(array $body, string $path): RequestInterface 216 | { 217 | $uri = $this->builder 218 | ->getUriFactory() 219 | ->createUri($path); 220 | $auth = base64_encode( 221 | sprintf( 222 | '%s:%s', 223 | $this->credentials->getClientId(), 224 | $this->credentials->getClientSecret() 225 | ) 226 | ); 227 | $stream = $this->builder 228 | ->getStreamFactory() 229 | ->createStream(http_build_query($body)); 230 | 231 | return $this->builder 232 | ->getRequestFactory() 233 | ->createRequest('POST', $uri) 234 | ->withBody($stream) 235 | ->withHeader('Content-Type', ContentType::X_WWW_FORM_URLENCODED) 236 | ->withHeader('Accept', ContentType::JSON) 237 | ->withHeader('Authorization', sprintf('Basic %s', $auth)); 238 | } 239 | } 240 | --------------------------------------------------------------------------------