Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Asemka\Services;
- use Asemka\Services\Contracts\BlibliServiceContract;
- use Setting;
- /**
- * Class ProductService
- * @package namespace Asemka\Services;
- */
- class BlibliService implements BlibliServiceContract
- {
- protected $api_username;
- protected $api_password;
- protected $secret_key;
- protected $access_token;
- protected $merchant_username;
- protected $merchant_password;
- public function __construct()
- {
- }
- public function oAuth()
- {
- return guzzle_oauth(setting('blibli_token_url'), [
- 'client_id' => $this->api_username,
- 'client_secret' => $this->api_password
- ]);
- }
- // @todo add mta_username & mta_password
- public function setCredential(array $credential)
- {
- $this->api_username = $credential['api_username'];
- $this->api_password = $credential['api_password'];
- $this->secret_key = $credential['secret_key'];
- $this->access_token = $credential['access_token'];
- $this->merchant_username = $credential['merchant_username'];
- $this->merchant_password = $credential['merchant_password'];
- }
- public function getDefaultParameters($params)
- {
- $default = [
- 'headers' => [
- 'Accept' => 'application/json'
- ],
- ];
- return array_merge($params, $default);
- }
- public function signature($method, $url, $body = null, $content_type = null)
- {
- $date = date('D M j H:i:s \W\I\B Y');
- if (isset($body)) {
- $body = md5(json_encode($body));
- }
- $body = '';
- $string = $method . '\n' . $body . '\n' . $content_type . '\n' . $date . '\n' . $url;
- $signature = hash_sha256($string, setting('blibli_url_token'));
- return $signature;
- }
- public function getAccessToken()
- {
- return guzzle('POST', setting('blibli_token_url'), [
- 'auth' => [$this->api_username, $this->api_password],
- 'headers' => [
- 'Accept' => 'application/json'
- ],
- 'form_params' => [
- 'grant_type' => setting('url_type'),
- 'username' => $this->merchant_username,
- 'password' => $this->merchant_password
- ],
- ]);
- }
- public function getProducts()
- {
- $method = 'GET';
- $uri = '/mtaapi/api/businesspartner/v1/product/getProductSummary';
- $params = 'requestId=123456&businessPartnerCode=BLI-17103';
- $uri = $uri . '?' . $params;
- $url = 'https://api.blibli.com/v2/proxy' . $uri;
- $signature = $this->signature($method, $uri);
- $data = [
- 'headers' => [
- 'Authorization' => 'bearer ' . $this->access_token,
- 'x-blibli-mta-authorization' => 'BMA ' . $this->merchant_username . ':' . $signature,
- 'x-blibli-mta-date-milis' => miliseconds(),
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'requestId' => '001-1001',
- 'sessionId' => str_random(16),
- 'username' => $this->merchant_username
- ]
- ];
- $orders = guzzle($method, $url, $data);
- // do {
- //$data['headers']['x-blibli-mta-authorization'] = 'BMA ' . $this->merchant_username . ':' . $signature;
- // if ($orders->errorCode == 401) {
- // $error_code = $orders->errorCode;
- // $signature = substr($orders->errorMessage, -44);
- // } else {
- // $error_code = 200;
- // }
- // } while ($error_code == 401);
- print_r("<pre>");
- print_r($data);
- print_r("<br>");
- print_r("<br>");
- print_r($orders);
- die;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement