ApiGateway.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. class Laybuy_ApiGateway
  3. {
  4. const PRODUCTION_API_ENDPOINT = 'https://api.laybuy.com/';
  5. const SANDBOX_API_ENDPOINT = 'https://sandbox-api.laybuy.com/';
  6. const PAYMENT_STATUS_SUCCESS = 'SUCCESS';
  7. const PAYMENT_STATUS_ERROR = 'ERROR';
  8. const PAYMENT_STATUS_DECLINED = 'DECLINED';
  9. const PAYMENT_STATUS_CANCELLED = 'CANCELLED';
  10. protected $api_endpoint;
  11. protected $settings;
  12. public function __construct($settings)
  13. {
  14. $this->settings = $settings;
  15. if (!isset($settings['environment'])) {
  16. return;
  17. }
  18. if ($settings['environment'] == 'sandbox') {
  19. $this->api_endpoint = self::SANDBOX_API_ENDPOINT;
  20. } else {
  21. $this->api_endpoint = self::PRODUCTION_API_ENDPOINT;
  22. }
  23. }
  24. public function createOrder($data)
  25. {
  26. return $this->post_to_api($this->api_endpoint . 'order/create', $data);
  27. }
  28. public function confirmOrder($data)
  29. {
  30. return $this->post_to_api($this->api_endpoint . 'order/confirm', $data);
  31. }
  32. public function refund($data)
  33. {
  34. return $this->post_to_api($this->api_endpoint . 'order/refund', $data);
  35. }
  36. /**
  37. * Get the Merchant ID from our user settings.
  38. *
  39. * @since 2.0.0
  40. * @return string
  41. */
  42. public function get_merchant_id() {
  43. if ($this->isGlobal()) {
  44. return $this->settings["{$this->settings['environment']}_global_merchant_id"];
  45. }
  46. $currency = get_woocommerce_currency();
  47. if (in_array($currency, $this->settings['currency'])) {
  48. return $this->settings["{$this->settings['environment']}_{$currency}_merchant_id"];
  49. }
  50. return false;
  51. }
  52. /**
  53. * Get the Secret Key from our user settings.
  54. *
  55. * @since 2.0.0
  56. * @return string
  57. */
  58. public function get_api_key() {
  59. if ($this->isGlobal()) {
  60. return $this->settings["{$this->settings['environment']}_global_api_key"];
  61. }
  62. $currency = get_woocommerce_currency();
  63. if (in_array($currency, $this->settings['currency'])) {
  64. return $this->settings["{$this->settings['environment']}_{$currency}_api_key"];
  65. }
  66. return false;
  67. }
  68. /**
  69. * POST to an API endpoint and load the response.
  70. */
  71. public function post_to_api($url, $data) {
  72. WC_Laybuy_Logger::log("POST {$url}");
  73. $response = wp_remote_post( $url, array(
  74. 'timeout' => 30,
  75. 'headers' => array(
  76. 'Authorization' => $this->build_authorization_header(),
  77. 'User-Agent' => $this->build_user_agent_header(),
  78. 'Content-Type' => 'application/json',
  79. 'Accepts' => 'application/json'
  80. ),
  81. 'body' => json_encode($data)
  82. ) );
  83. if (!is_wp_error( $response )) {
  84. $body = json_decode(wp_remote_retrieve_body( $response ));
  85. if (!is_null($body)) {
  86. return $body;
  87. }
  88. WC_Laybuy_Logger::log("Failed to parse Laybuy response: " . var_export( $response, true));
  89. } else {
  90. # Unable to establish a secure connection with the Laybuy API endpoint.
  91. # Likely a TLS or network error.
  92. # Log the error details.
  93. foreach ($response->errors as $code => $messages_arr) {
  94. $messages_str = implode("\n", $messages_arr);
  95. WC_Laybuy_Logger::error("API NETWORK ERROR! Code: \"{$code}\"; Message(s):\n" . $messages_str);
  96. }
  97. # Get CloudFlare Header for the error
  98. $cf_ray = wp_remote_retrieve_header($response, "cf-ray");
  99. if (!empty($cf_ray)) {
  100. WC_Laybuy_Logger::error("Error CF-Ray: " . $cf_ray);
  101. }
  102. else {
  103. WC_Laybuy_Logger::error("No CF-Ray Detected");
  104. }
  105. # Return the WP_Error object.
  106. return $response;
  107. }
  108. return false;
  109. }
  110. public function build_authorization_header() {
  111. return 'Basic ' . base64_encode($this->get_merchant_id() . ':' . $this->get_api_key());
  112. }
  113. /**
  114. * Build the Laybuy User-Agent header for use with the APIs.
  115. */
  116. private function build_user_agent_header() {
  117. global $wp_version;
  118. $plugin_version = WC_Laybuy::$version;
  119. $php_version = PHP_VERSION;
  120. $woocommerce_version = WC()->version;
  121. $merchant_id = $this->get_merchant_id();
  122. $extra_detail_1 = '';
  123. $extra_detail_2 = '';
  124. $matches = array();
  125. if (array_key_exists('SERVER_SOFTWARE', $_SERVER) && preg_match('/^[a-zA-Z0-9]+\/\d+(\.\d+)*/', $_SERVER['SERVER_SOFTWARE'], $matches)) {
  126. $s = $matches[0];
  127. $extra_detail_1 .= "; {$s}";
  128. }
  129. if (array_key_exists('REQUEST_SCHEME', $_SERVER) && array_key_exists('HTTP_HOST', $_SERVER)) {
  130. $s = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
  131. $extra_detail_2 .= " {$s}";
  132. }
  133. return "Laybuy Gateway for WooCommerce/{$plugin_version} (PHP/{$php_version}; WordPress/{$wp_version}; WooCommerce/{$woocommerce_version}; Merchant/{$merchant_id}{$extra_detail_1}){$extra_detail_2}";
  134. }
  135. protected function isGlobal()
  136. {
  137. return $this->settings['global'] == 'yes';
  138. }
  139. }