/application/libraries/Stripe.php

https://gitlab.com/Anas7232/Layout-Changes · PHP · 127 lines · 107 code · 8 blank · 12 comment · 15 complexity · 4eb73e4d49d70076f8e96218f05396fa MD5 · raw file

  1. <?php
  2. /*
  3. Strip Payments Library v1.0 for codeigniter
  4. by Jaydeep Goswami
  5. */
  6. /*
  7. 1. get_credentials()
  8. 2. create_customer($customer_data)
  9. 3. construct_event($request_body, $sigHeader, $secret,$tolerance = DEFAULT_TOLERANCE)
  10. 4. create_payment_intent($c_data)
  11. 5. curl($url, $method = 'GET', $data = [])
  12. */
  13. const DEFAULT_TOLERANCE = 300;
  14. class Stripe
  15. {
  16. private $secret_key = "";
  17. private $publishable_key = "";
  18. private $webhook_secret_key = "";
  19. private $currency_code = "";
  20. private $url = "";
  21. function __construct()
  22. {
  23. $this->CI =& get_instance();
  24. $this->CI->load->helper('url');
  25. $this->CI->load->helper('form');
  26. // $this->CI->load->config('paypal');
  27. $settings = get_settings('payment_method',true);
  28. $system_settings = get_settings('system_settings',true);
  29. $this->secret_key = (isset($settings['stripe_secret_key']))?$settings['stripe_secret_key']:"";
  30. $this->publishable_key = (isset($settings['stripe_publishable_key']))?$settings['stripe_publishable_key']:"";
  31. $this->webhook_secret_key = (isset($settings['stripe_webhook_secret_key']))?$settings['stripe_webhook_secret_key']:"";
  32. $this->currency_code = (isset($settings['stripe_currency_code']))?strtolower($settings['stripe_currency_code']):"usd";
  33. $this->url = "https://api.stripe.com/";
  34. }
  35. public function get_credentials()
  36. {
  37. $data['secret_key'] = $this->secret_key;
  38. $data['publishable_key'] = $this->publishable_key;
  39. $data['webhook_key'] = $this->webhook_secret_key;
  40. $data['currency_code'] = $this->currency_code;
  41. $data['url'] = $this->url;
  42. return $data;
  43. }
  44. public function create_customer($customer_data)
  45. {
  46. $create_customer['name'] = $customer_data['name'];
  47. $create_customer['address']['line1'] = $customer_data['line1'];
  48. $create_customer['address']['postal_code'] = $customer_data['postal_code'];
  49. $create_customer['address']['city'] = $customer_data['city'];
  50. $url = $this->url . 'v1/customers';
  51. $method = 'POST';
  52. $response = $this->curl($url, $method, $create_customer);
  53. $res = json_decode($response['body'],true);
  54. return $res;
  55. }
  56. public function construct_event($request_body, $sigHeader, $secret,$tolerance = DEFAULT_TOLERANCE)
  57. {
  58. $explode_header = explode(",",$sigHeader);
  59. for($i = 0; $i<count($explode_header); $i++){
  60. $data[] = explode("=",$explode_header[$i]);
  61. }
  62. if(empty($data[0][1]) || $data[0][1] == "" || empty($data[1][1]) || $data[1][1] == ""){
  63. $response['error'] = true;
  64. $response['message'] = "Unable to extract timestamp and signatures from header" ;
  65. return $response;
  66. }
  67. $timestamp = $data[0][1];
  68. $signs = $data[1][1];
  69. $signed_payload = "{$timestamp}.{$request_body}";
  70. $expectedSignature = hash_hmac('sha256', $signed_payload, $secret);
  71. if($expectedSignature == $signs){
  72. if (($tolerance > 0) && (\abs(\time() - $timestamp) > $tolerance)) {
  73. $response['error'] = true;
  74. $response['message'] = "Timestamp outside the tolerance zone";
  75. return $response;
  76. }else{
  77. return "Matched";
  78. }
  79. }else{
  80. $response['error'] = true;
  81. $response['message'] = "No signatures found matching the expected signature for payload" ;
  82. return $response;
  83. }
  84. }
  85. public function create_payment_intent($c_data)
  86. {
  87. $c_data['currency'] = $this->currency_code;
  88. $url = $this->url . 'v1/payment_intents';
  89. $method = 'POST';
  90. $response = $this->curl($url, $method,$c_data);
  91. $res = json_decode($response['body'],true);
  92. return $res;
  93. }
  94. public function curl($url, $method = 'GET', $data = [])
  95. {
  96. $ch = curl_init();
  97. $curl_options = array(
  98. CURLOPT_URL => $url,
  99. CURLOPT_RETURNTRANSFER => 1,
  100. CURLOPT_HEADER => 0,
  101. CURLOPT_HTTPHEADER => array(
  102. 'Content-Type: application/x-www-form-urlencoded',
  103. 'Authorization: Basic ' . base64_encode($this->secret_key . ':')
  104. )
  105. );
  106. if (strtolower($method) == 'post') {
  107. $curl_options[CURLOPT_POST] = 1;
  108. $curl_options[CURLOPT_POSTFIELDS] = http_build_query($data);
  109. } else {
  110. $curl_options[CURLOPT_CUSTOMREQUEST] = 'GET';
  111. }
  112. curl_setopt_array($ch, $curl_options);
  113. $result = array(
  114. 'body' => curl_exec($ch),
  115. 'http_code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  116. );
  117. return $result;
  118. }
  119. }