diff --git a/.gitignore b/.gitignore index 432b975..8f721c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor /nbproject/private/ -.idea/ \ No newline at end of file +.idea/ +.DS_Store \ No newline at end of file diff --git a/lib/ApplicationCredit.php b/lib/ApplicationCredit.php new file mode 100644 index 0000000..58f8551 --- /dev/null +++ b/lib/ApplicationCredit.php @@ -0,0 +1,26 @@ + + * Created at 8/18/16 9:50 AM UTC+06:00 + * + * @see https://help.shopify.com/api/reference/applicationcharge Shopify API Reference for ApplicationCharge + */ + +namespace PHPShopify; + + +class ApplicationCredit extends ShopifyResource +{ + /** + * @inheritDoc + */ + protected $resourceKey = 'application_credit'; + + /** + * @inheritDoc + */ + public $countEnabled = false; + + +} diff --git a/lib/AuthHelper.php b/lib/AuthHelper.php index 101f903..de076b9 100644 --- a/lib/AuthHelper.php +++ b/lib/AuthHelper.php @@ -28,7 +28,7 @@ public static function getCurrentUrl() else { $protocol = 'http'; } - + $url = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $url = false !== ($qsPos = strpos($url, '?')) ? substr($url, 0, $qsPos) : $url; // remove query params diff --git a/lib/Checkout.php b/lib/Checkout.php new file mode 100644 index 0000000..401882a --- /dev/null +++ b/lib/Checkout.php @@ -0,0 +1,53 @@ + + * Created at 8/19/16 2:59 PM UTC+06:00 + * + * @see https://help.shopify.com/api/reference/sales-channels/checkout Shopify API Reference for Checkout + */ + +namespace PHPShopify; + + +/** + * -------------------------------------------------------------------------- + * Order -> Child Resources + * -------------------------------------------------------------------------- + * @property-read ShippingRate $ShippingRate + * + * @method ShippingRate ShippingRate(integer $id = null) + * + + * -------------------------------------------------------------------------- + * Checkout -> Custom actions + * -------------------------------------------------------------------------- + * @method array complete() Completes Checkout without payment + * + */ +class Checkout extends ShopifyResource +{ + /** + * @inheritDoc + */ + protected $resourceKey = 'checkout'; + + /** + * @inheritDoc + */ + public $countEnabled = false; + + /** + * @inheritDoc + */ + protected $childResource = array ( + 'ShippingRate', + ); + /** + * @inheritDoc + */ + protected $customPostActions = array( + 'complete', + 'payments' + ); +} diff --git a/lib/CollectionListing.php b/lib/CollectionListing.php new file mode 100644 index 0000000..bc80f84 --- /dev/null +++ b/lib/CollectionListing.php @@ -0,0 +1,32 @@ + + * Created at: 6/2/18 1:38 PM UTC+06:00 + * + * @see https://help.shopify.com/api/reference/sales_channels/collectionlisting + */ + +namespace PHPShopify; +/** + * -------------------------------------------------------------------------- + * CollectionListing -> Custom actions + * -------------------------------------------------------------------------- + * @method array productIds() Sets the address as default for the customer + */ + +class CollectionListing extends ShopifyResource +{ + /** + * @inheritDoc + */ + protected $resourceKey = 'collection_listing'; + + /** + * @inheritDoc + */ + protected $customGetActions = array( + 'product_ids' => 'productIds', + ); + +} diff --git a/lib/GiftCard.php b/lib/GiftCard.php index 2391064..e749b2d 100644 --- a/lib/GiftCard.php +++ b/lib/GiftCard.php @@ -29,6 +29,13 @@ class GiftCard extends ShopifyResource */ public $searchEnabled = true; + /** + * @inheritDoc + */ + protected $childResource = array( + 'GiftCardAdjustment' => 'Adjustment' + ); + /** * Disable a gift card. * Disabling a gift card is permanent and cannot be undone. @@ -45,4 +52,4 @@ public function disable() return $this->post($dataArray, $url); } -} \ No newline at end of file +} diff --git a/lib/GiftCardAdjustment.php b/lib/GiftCardAdjustment.php new file mode 100644 index 0000000..1592913 --- /dev/null +++ b/lib/GiftCardAdjustment.php @@ -0,0 +1,36 @@ + + * Created at 8/19/16 12:07 PM UTC+06:00 + * + * @see https://help.shopify.com/api/reference/customeraddress Shopify API Reference for CustomerAddress + */ + +namespace PHPShopify; + + +/** + * -------------------------------------------------------------------------- + * GiftCardAdjustment -> Custom actions + * -------------------------------------------------------------------------- + * @method array makeDefault() Sets the address as default for the customer + * + */ +class GiftCardAdjustment extends ShopifyResource +{ + /** + * @inheritDoc + */ + protected $resourceKey = 'adjustment'; + + /** + * @inheritDoc + */ + protected function pluralizeKey() + { + return 'adjustments'; + } + + +} diff --git a/lib/GraphQL.php b/lib/GraphQL.php index 185593b..00f4f4f 100644 --- a/lib/GraphQL.php +++ b/lib/GraphQL.php @@ -45,7 +45,7 @@ public function post($graphQL, $url = null, $wrapData = false, $variables = null if (!$url) $url = $this->generateUrl(); $response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders, $variables); - + return $this->processResponse($response); } @@ -75,4 +75,4 @@ public function delete($urlParams = array(), $url = null) { throw new SdkException("Only POST method is allowed for GraphQL!"); } -} \ No newline at end of file +} diff --git a/lib/HttpRequestGraphQL.php b/lib/HttpRequestGraphQL.php index 708f334..1dc27d8 100644 --- a/lib/HttpRequestGraphQL.php +++ b/lib/HttpRequestGraphQL.php @@ -44,14 +44,16 @@ protected static function prepareRequest($httpHeaders = array(), $data = array() throw new SdkException("The GraphQL Admin API requires an access token for making authenticated requests!"); } - self::$httpHeaders = $httpHeaders; - if (is_array($variables)) { self::$postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]); - self::$httpHeaders['Content-type'] = 'application/json'; + $httpHeaders['Content-type'] = 'application/json'; } else { - self::$httpHeaders['Content-type'] = 'application/graphql'; + $httpHeaders['Content-type'] = 'application/graphql'; } + + $httpHeaders['X-Shopify-Access-Token'] = $httpHeaders['X-Shopify-Access-Token']; + + self::$httpHeaders = $httpHeaders; } /** @@ -66,6 +68,7 @@ protected static function prepareRequest($httpHeaders = array(), $data = array() */ public static function post($url, $data, $httpHeaders = array(), $variables = null) { + self::prepareRequest($httpHeaders, $data, $variables); self::$postDataJSON = self::$postDataGraphQL; diff --git a/lib/HttpRequestJson.php b/lib/HttpRequestJson.php index c325b74..642e4ec 100644 --- a/lib/HttpRequestJson.php +++ b/lib/HttpRequestJson.php @@ -183,7 +183,7 @@ public static function shouldRetry($response, $error, $retry) { * * @param string $response * - * @return array + * @return string */ protected static function processResponse($response) { diff --git a/lib/ShippingRate.php b/lib/ShippingRate.php new file mode 100644 index 0000000..e5de310 --- /dev/null +++ b/lib/ShippingRate.php @@ -0,0 +1,19 @@ + + * Created at 8/19/16 7:27 PM UTC+06:00 + * + * @see https://help.shopify.com/api/reference/shipping_rates Shopify API Reference for ShippingRate + */ + +namespace PHPShopify; + + +class ShippingRate extends ShopifyResource +{ + /** + * @inheritDoc + */ + protected $resourceKey = 'shipping_rate'; +} diff --git a/lib/ShopifyResource.php b/lib/ShopifyResource.php index dd3b2cb..19bc198 100644 --- a/lib/ShopifyResource.php +++ b/lib/ShopifyResource.php @@ -136,6 +136,18 @@ abstract class ShopifyResource */ private $prevLink = null; + /** + * HTTP code used to check if we need to poll or not + */ + public $httpCode = null; + + /** + * Response Header Location, used for discount code lookup + * @see: https://shopify.dev/docs/admin-api/rest/reference/discounts/discountcode?api[version]=2020-04#lookup-2020-04 + * @var string $discountLocation + */ + private $discountLocation = null; + public function __construct($id = null, $parentResourceUrl = '') { $this->id = $id; @@ -319,7 +331,7 @@ protected function getResourcePath() */ public function generateUrl($urlParams = array(), $customAction = null) { - return $this->resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : ''); + return $this->resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . preg_replace('/\%5B\d+\%5D/', '%5B%5D', http_build_query($urlParams)) : ''); } /** @@ -514,7 +526,7 @@ protected function castString($array) /** * Process the request response * - * @param array $responseArray Request response in array format + * @param array $response Request response in array format * @param string $dataKey Keyname to fetch data from response array * * @throws ApiException if the response has an error specified @@ -522,36 +534,51 @@ protected function castString($array) * * @return array */ - public function processResponse($responseArray, $dataKey = null) + public function processResponse($response, $dataKey = null) { + self::$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders; $lastResponseHeaders = CurlRequest::$lastHttpResponseHeaders; + $this->getLinks($lastResponseHeaders); - if (isset($responseArray['errors'])) { - $message = $this->castString($responseArray['errors']); + $this->getLocationHeader($lastResponseHeaders); + + $httpCode = CurlRequest::$lastHttpCode; + $this->httpCode = $httpCode; + + if (isset($response['errors'])) { + $message = $this->castString($response['errors']); //check account already enabled or not if($message=='account already enabled'){ return array('account_activation_url'=>false); } - - throw new ApiException($message, CurlRequest::$lastHttpCode); + + throw new ApiException($message, $httpCode); } - if ($dataKey && isset($responseArray[$dataKey])) { - return $responseArray[$dataKey]; + if ($dataKey && isset($response[$dataKey])) { + return $response[$dataKey]; } else { - return $responseArray; + return $response; } } - public function getLinks($responseHeaders){ + public function getLinks($responseHeaders) { $this->nextLink = $this->getLink($responseHeaders,'next'); $this->prevLink = $this->getLink($responseHeaders,'previous'); } + public function getLocationHeader($responseHeaders) { + + if(!empty($responseHeaders['location'])) { + $this->discountLocation = $responseHeaders['location']; + } + + } + public function getLink($responseHeaders, $type='next'){ if(array_key_exists('x-shopify-api-version', $responseHeaders) @@ -587,6 +614,10 @@ public function getNextLink(){ return $this->nextLink; } + public function getDiscountLocation(){ + return $this->discountLocation; + } + public function getUrlParams($url) { if ($url) { $parts = parse_url($url); diff --git a/lib/ShopifySDK.php b/lib/ShopifySDK.php index 7dffb55..aa50e05 100644 --- a/lib/ShopifySDK.php +++ b/lib/ShopifySDK.php @@ -67,6 +67,7 @@ use PHPShopify\Exception\SdkException; /** + * @property-read ApplicationCredit $applicationCredit * @property-read AbandonedCheckout $AbandonedCheckout * @property-read AccessScope $AccessScope * @property-read ApplicationCharge $ApplicationCharge @@ -84,6 +85,7 @@ * @property-read Discount $Discount * @property-read DiscountCode $DiscountCode * @property-read DraftOrder $DraftOrder + * @property-read Checkout $Checkout * @property-read Event $Event * @property-read Fulfillment $Fulfillment * @property-read FulfillmentService $FulfillmentService @@ -98,6 +100,7 @@ * @property-read Policy $Policy * @property-read Product $Product * @property-read ProductListing $ProductListing + * @property-read CollectionListing $CollectionListing * @property-read ProductVariant $ProductVariant * @property-read PriceRule $PriceRule * @property-read RecurringApplicationCharge $RecurringApplicationCharge @@ -114,6 +117,7 @@ * @property-read Webhook $Webhook * @property-read GraphQL $GraphQL * + * @method ApplicationCredit ApplicationCredit(integer $id = null) * @method AbandonedCheckout AbandonedCheckout(integer $id = null) * @method AccessScope AccessScope() * @method ApplicationCharge ApplicationCharge(integer $id = null) @@ -142,11 +146,13 @@ * @method Metafield Metafield(integer $id = null) * @method Multipass Multipass(integer $id = null) * @method Order Order(integer $id = null) + * @method Checkout Checkout(integer $id = null) * @method Page Page(integer $id = null) * @method Policy Policy(integer $id = null) * @method Product Product(integer $id = null) * @method ProductListing ProductListing(integer $id = null) * @method ProductVariant ProductVariant(integer $id = null) + * @method CollectionListing CollectionListing(integer $id = null) * @method PriceRule PriceRule(integer $id = null) * @method RecurringApplicationCharge RecurringApplicationCharge(integer $id = null) * @method Redirect Redirect(integer $id = null) @@ -171,6 +177,8 @@ class ShopifySDK */ protected $resources = array( 'AbandonedCheckout', + 'Adjustment', + 'ApplicationCredit', 'AccessScope', 'ApplicationCharge', 'Blog', @@ -198,11 +206,13 @@ class ShopifySDK 'Metafield', 'Multipass', 'Order', + 'Checkout', 'Page', 'Policy', 'Product', 'ProductListing', 'ProductVariant', + 'CollectionListing', 'PriceRule', 'RecurringApplicationCharge', 'Redirect', @@ -252,6 +262,7 @@ class ShopifySDK 'Asset' => 'Theme', 'Balance' => 'ShopifyPayment', 'CustomerAddress' => 'Customer', + 'GiftCardAdjustment'=> 'GiftCard', 'Dispute' => 'ShopifyPayment', 'Fulfillment' => 'Order', 'FulfillmentEvent' => 'Fulfillment', @@ -263,6 +274,7 @@ class ShopifySDK 'Province' => 'Country', 'Refund' => 'Order', 'Transaction' => 'Order', + 'ShippingRate' => 'Checkout', 'Transactions' => 'Balance', 'UsageCharge' => 'RecurringApplicationCharge', );