Skip to content

nikapps/bazaar-api-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bazaar-Api-PHP (BazaarApi for PHP)

A PHP API wrapper for Cafebazaar REST API (v2).

If you are looking for version 1.x, please visit branch v1.

CafeBazaar Logo

Table of Contents

Installation

If you don't have Composer, first you should install it on your system:

https://getcomposer.org

Now run this command to install the package:

composer require nikapps/bazaar-api-php
  • Notice: if you don't know anything about composer, please read this article.

Configuration

Create a client

First, you should go to your cafebazaar panel and create a client.

  • Login to your panel and go to this url: https://pardakht.cafebazaar.ir/panel/developer-api/?l=fa&nd=False

  • Click on new client and enter your redirect uri (it is needed for getting returned code and refresh_token. see the next section)

now you have your client-id and client-secret.

Getting refresh token

  • Open this url in your browser:
https://pardakht.cafebazaar.ir/devapi/v2/auth/authorize/?response_type=code&access_type=offline&redirect_uri=<REDIRECT_URI>&client_id=<CLIENT_ID>

Don't forget to change <REDIRECT_URI> and <CLIENT_ID>.

  • After clicking on accept/confirm button, you will be redirected to: <REDIRECT_URI>?code=<CODE>

  • <REDIRECT_URI> is url of this file:

$bazaar = new Bazaar(new Config([
    'client-secret' => 'your-client-secret',
    'client-id' => 'your-client-id'
]));

$token = $bazaar->token('<REDIRECT_URI>');

echo "Refresh Token: " . $token->refreshToken();

Here is the full example: authorization.php

Setting up config

As you can see in previous section, we create a Config instance and set client-id and client-secret.

For other api calls, we also should set refresh-token and storage.

$bazaar = new Bazaar(new Config([
    'client-secret' => 'your-client-secret',
    'client-id' => 'your-client-id',
    'refresh-token' => 'refresh-token-123456',
    'storage' => new FileTokenStorage(__DIR__ . '/token.json')
]));

The storage handles storing and retrieving access_token. in this package we have two different storages:

  • FileTokenStorage which store token in a file.
  • MemoryTokenStorage which does not persist the token and you can only use it in current request.

Usage

Purchase

Here is the example of getting state of a purchase:

$purchase = $bazaar->purchase('com.example.app', 'product-id (sku)', 'purchase-token');

if ($purchase->failed()) {
    echo $purchase->errorDescription();
} else {
    echo "Purchased: " . $purchase->purchased();
    echo "Consumed: " . $purchase->consumed();
    echo "Developer Payload: " . $purchase->developerPayload();
    echo "Purchase Time (Timestamp in ms): " . $purchase->time();
}

Full Example: purchase.php

Subscription

Here is the example of getting state of a subscription:

$subscription = $bazaar->subscription('com.example.app', 'subscription-id (sku)', 'purchase-token');

if ($subscription->failed()) {
    echo $subscription->errorDescription();
} else {
    echo "Start Time (Timestamp in ms): " . $subscription->startTime(); // initiationTime()
    echo "End Time (Timestamp in ms): " . $subscription->endTime(); // expirationTime(), nextTime()
    echo "Is auto renewing? " . $subscription->autoRenewing();
    echo "Is expired? (end time is past) " . $subscription->expired();
}

Full Example: subscription.php

Cancel Subscription (Unsubscribe)

Here is the example of how you can cancel a subscription:

$unsubscribe = $bazaar->unsubscribe('com.example.app', 'subscription-id (sku)', 'purchase-token');

if ($unsubscribe->successful()) {
    echo "The subscription has been successfully cancelled!";
} else {
    echo $unsubscribe->errorDescription();
}

Full Example: unsubscribe.php

Customization

Custom Token Storage

If you want to store the token somewhere else (maybe database or redis?!), you can implement the TokenStorageInterface

class CustomTokenStorage implements TokenStorageInterface 
{

    public function save(Token $token)
    {
    	// store access token
    }

    public function retrieve()
    {
    	// return access token
    }

    public function expired()
    {
    	// is token expired?
    }

}

Examples

See: https://github.com/nikapps/bazaar-api-php/blob/master/examples/

Dependencies

Testing

Run:

phpunit

Official Documentation

Contribute

Wanna contribute? simply fork this project and make a pull request!

License

This project released under the MIT License.

/*
 * Copyright (C) 2015-2016 NikApps Team.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * 1- The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * 2- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

Donation

Donate via Paypal