2017 © Pedro Peláez
 

library bazaar-api-php

A PHP API wrapper for CafeBazaar Rest Api v2

image

nikapps/bazaar-api-php

A PHP API wrapper for CafeBazaar Rest Api v2

  • Wednesday, May 4, 2016
  • by alibo
  • Repository
  • 2 Watchers
  • 5 Stars
  • 225 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 9 Versions
  • 15 % Grown

The README.md

Bazaar-Api-PHP (BazaarApi for PHP)

A PHP API wrapper for Cafebazaar REST API (v2)., (*1)

If you are looking for version 1.x, please visit branch v1., (*2)

CafeBazaar Logo, (*3)

Table of Contents

Installation

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

https://getcomposer.org

Now run this command to install the package:, (*5)

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., (*6)

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

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

now you have your client-id and client-secret., (*9)

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>., (*10)

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

  • <REDIRECT_URI> is url of this file:, (*12)

$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, (*13)

Setting up config

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

For other api calls, we also should set refresh-token and storage., (*15)

$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:, (*16)

  • 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:, (*17)

$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, (*18)

Subscription

Here is the example of getting state of a subscription:, (*19)

$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, (*20)

Cancel Subscription (Unsubscribe)

Here is the example of how you can cancel a subscription:, (*21)

$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, (*22)

Customization

Custom Token Storage

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

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/, (*24)

Dependencies

Testing

Run:, (*25)

phpunit

Official Documentation

Contribute

Wanna contribute? simply fork this project and make a pull request!, (*26)

License

This project released under the MIT License., (*27)

/*
 * 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, (*28)

The Versions

04/05 2016

dev-master

9999999-dev

A PHP API wrapper for CafeBazaar Rest Api v2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar

29/02 2016

2.0.1

2.0.1.0

A PHP API wrapper for CafeBazaar Rest Api v2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar

28/02 2016

2.0.0

2.0.0.0

A PHP API wrapper for CafeBazaar Rest Api v2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar

28/02 2016

v1.x-dev

1.9999999.9999999.9999999-dev

A PHP API wrapper for CafeBazaar

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani
by Hossein Moradgholi

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar

28/02 2016

dev-develop

dev-develop

A PHP API wrapper for CafeBazaar Rest Api v2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar

29/05 2015

1.0.3

1.0.3.0

A PHP API wrapper for CafeBazaar

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani
by Hossein Moradgholi

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar

18/03 2015

1.0.2

1.0.2.0

A PHP API wrapper for CafeBazaar

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani
by Hossein Moradgholi

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar

18/03 2015

1.0.1

1.0.1.0

A PHP API wrapper for CafeBazaar

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani
by Hossein Moradgholi

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar

17/03 2015

1.0.0

1.0.0.0

A PHP API wrapper for CafeBazaar

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ali Borhani
by Hossein Moradgholi

api wrapper cafebazaar cafebazaar api in app purchase iap bazaar