Advertisement
Guest User

BliService.php

a guest
Aug 2nd, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Asemka\Services;
  4.  
  5. use Asemka\Services\Contracts\BlibliServiceContract;
  6. use Setting;
  7.  
  8. /**
  9. * Class ProductService
  10. * @package namespace Asemka\Services;
  11. */
  12. class BlibliService implements BlibliServiceContract
  13. {
  14. protected $api_username;
  15. protected $api_password;
  16. protected $secret_key;
  17. protected $access_token;
  18. protected $merchant_username;
  19. protected $merchant_password;
  20.  
  21. public function __construct()
  22. {
  23.  
  24. }
  25.  
  26. public function oAuth()
  27. {
  28. return guzzle_oauth(setting('blibli_token_url'), [
  29. 'client_id' => $this->api_username,
  30. 'client_secret' => $this->api_password
  31. ]);
  32. }
  33.  
  34. // @todo add mta_username & mta_password
  35. public function setCredential(array $credential)
  36. {
  37. $this->api_username = $credential['api_username'];
  38. $this->api_password = $credential['api_password'];
  39. $this->secret_key = $credential['secret_key'];
  40. $this->access_token = $credential['access_token'];
  41. $this->merchant_username = $credential['merchant_username'];
  42. $this->merchant_password = $credential['merchant_password'];
  43. }
  44.  
  45. public function getDefaultParameters($params)
  46. {
  47. $default = [
  48. 'headers' => [
  49. 'Accept' => 'application/json'
  50. ],
  51. ];
  52.  
  53. return array_merge($params, $default);
  54. }
  55.  
  56. public function signature($method, $url, $body = null, $content_type = null)
  57. {
  58. $date = date('D M j H:i:s \W\I\B Y');
  59.  
  60. if (isset($body)) {
  61. $body = md5(json_encode($body));
  62. }
  63. $body = '';
  64. $string = $method . '\n' . $body . '\n' . $content_type . '\n' . $date . '\n' . $url;
  65.  
  66. $signature = hash_sha256($string, setting('blibli_url_token'));
  67.  
  68. return $signature;
  69. }
  70.  
  71. public function getAccessToken()
  72. {
  73. return guzzle('POST', setting('blibli_token_url'), [
  74. 'auth' => [$this->api_username, $this->api_password],
  75. 'headers' => [
  76. 'Accept' => 'application/json'
  77. ],
  78. 'form_params' => [
  79. 'grant_type' => setting('url_type'),
  80. 'username' => $this->merchant_username,
  81. 'password' => $this->merchant_password
  82. ],
  83. ]);
  84. }
  85.  
  86. public function getProducts()
  87. {
  88. $method = 'GET';
  89.  
  90. $uri = '/mtaapi/api/businesspartner/v1/product/getProductSummary';
  91. $params = 'requestId=123456&businessPartnerCode=BLI-17103';
  92.  
  93. $uri = $uri . '?' . $params;
  94.  
  95. $url = 'https://api.blibli.com/v2/proxy' . $uri;
  96.  
  97. $signature = $this->signature($method, $uri);
  98.  
  99. $data = [
  100. 'headers' => [
  101. 'Authorization' => 'bearer ' . $this->access_token,
  102. 'x-blibli-mta-authorization' => 'BMA ' . $this->merchant_username . ':' . $signature,
  103. 'x-blibli-mta-date-milis' => miliseconds(),
  104. 'Accept' => 'application/json',
  105. 'Content-Type' => 'application/json',
  106. 'requestId' => '001-1001',
  107. 'sessionId' => str_random(16),
  108. 'username' => $this->merchant_username
  109. ]
  110. ];
  111.  
  112. $orders = guzzle($method, $url, $data);
  113.  
  114. // do {
  115. //$data['headers']['x-blibli-mta-authorization'] = 'BMA ' . $this->merchant_username . ':' . $signature;
  116.  
  117. // if ($orders->errorCode == 401) {
  118. // $error_code = $orders->errorCode;
  119. // $signature = substr($orders->errorMessage, -44);
  120. // } else {
  121. // $error_code = 200;
  122. // }
  123. // } while ($error_code == 401);
  124.  
  125. print_r("<pre>");
  126. print_r($data);
  127. print_r("<br>");
  128. print_r("<br>");
  129. print_r($orders);
  130. die;
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement