Download the PHP package nikapps/bazaar-api-php without Composer
On this page you can find all versions of the php package nikapps/bazaar-api-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nikapps/bazaar-api-php
More information about nikapps/bazaar-api-php
Files in nikapps/bazaar-api-php
Package bazaar-api-php
Short Description A PHP API wrapper for CafeBazaar Rest Api v2
License MIT
Informations about the package bazaar-api-php
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.
Table of Contents
- Bazaar-Api-PHP (BazaarApi for PHP)
- Table of Contents
- Installation
- Configuration
- Create a client
- Getting refresh token
- Setting up config
- Usage
- Purchase
- Subscription
- Cancel Subscription (Unsubscribe)
- Customization
- Custom Token Storage
- Examples
- Dependencies
- Testing
- Official Documentation
- Contribute
- License
- Donation
Installation
If you don't have Composer, first you should install it on your system:
Now run this command to install the package:
- 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 returnedcode
andrefresh_token
. see the next section)
now you have your client-id
and client-secret
.
Getting refresh token
- Open this url in your browser:
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:
Official Documentation
Contribute
Wanna contribute? simply fork this project and make a pull request!
License
This project released under the MIT License.