Changeset 883553
- Timestamp:
- 03/28/2014 01:48:32 PM (12 years ago)
- File:
-
- 1 edited
-
instamojo/branches/develop/lib/Instamojo.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
instamojo/branches/develop/lib/Instamojo.php
r881515 r883553 1 1 <?php 2 2 3 class Instamojo{ 4 5 // internal constant to enable/disable debugging. 6 const debug = true; 7 8 // Instamojo API base URL. 9 const API_URL = 'https://www.instamojo.com/api/1/'; 10 11 // API version. 12 const version = '0.1'; 13 14 // A curl instance. 15 protected $curl; 16 17 // The username of the User. 18 protected $username = null; 19 20 // The password of the User 21 protected $password = null; 22 23 // Token provided by Instamojo. 24 protected $APP_TOKEN = null; 25 26 // APP_ID provided by Instamojo. 27 protected $APP_ID = null; 28 29 // Time-out. 30 protected $timeout = 10; 31 32 public $title = null; 33 public $description = null; 34 public $currency = null; 35 public $base_price = null; 36 public $quantity = null; 37 public $start_date = null; 38 public $end_date = null; 39 public $timezone = null; 40 public $venue = null; 41 public $redirect_url = null; 42 public $note = null; 43 public $file_path = null; 44 public $cover_path = null; 45 46 private $currencies = array("INR", "USD"); 47 48 /** 49 * Default constructor 50 * @param string $username Instamojo username of the user. 51 * @param string $password Instamojo password of the user. 52 */ 53 public function __construct($username, $password, $id){ 54 $this->setUsername($username); 55 $this->setPassword($password); 56 $this->setID($id); 57 } 58 59 /** 60 * Default destructor. 61 */ 62 public function __destruct(){ 63 if($this->curl != null) curl_close($this->curl); 64 } 65 66 /** 67 * Get the version of the API wrapper. 68 * @return string Version of the API wrapper. 69 */ 70 public function getVersion(){ 71 return self::version; 72 } 73 74 private function allowed_currency($currency){ 75 if(in_array($currency, $this->currencies)) return true; 76 return false; 77 } 78 79 /** 80 * Set teh Username. 81 * @param string $username Instamojo username of the user. 82 */ 83 private function setUsername($username){ 84 $this->username = (string) $username; 85 } 86 87 /** 88 * Set the password. 89 * @param string $password Instamojo username of the password. 90 */ 91 private function setPassword($password){ 92 $this->password = (string) $password; 93 } 94 95 /** 96 * Set the ID. 97 * @param string $id Instamojo APP_ID provided by Instamojo. 98 */ 99 100 private function setID($id){ 101 $this->APP_ID = (string) $id; 102 } 103 104 /** 105 * Create the absolute path for the request. 106 * @param string $url The base URL (Here it is used by API_URL) 107 * @param string $path The relative path. 108 */ 109 private function buildPath($url, $path){ 110 return $url . $path; 111 } 112 113 /** 114 * Request the instamojo API. 115 * @param string $path The relative path. 116 * @param string $method POST/GET/POST/DELETE 117 * @param array $data Data to be passed. 118 */ 119 private function apiRequest($path, $method, array $data = null){ 120 $path = (string) $path; 121 $method = (string) $method; 122 $data = (array) $data; 123 124 $headers = array("X-App-Id: $this->APP_ID"); 125 126 if($this->APP_TOKEN){ 127 $headers[] = "X-Auth-Token: $this->APP_TOKEN"; 128 } 129 130 $request_path = $this->buildPath(self::API_URL, $path); 131 132 $options = array(); 133 $options[CURLOPT_HTTPHEADER] = $headers; 134 $options[CURLOPT_RETURNTRANSFER] = true; 135 $options[CURLOPT_URL] = $request_path; 136 137 if($method == 'POST'){ 138 $data_string = ""; 139 140 foreach ($data as $key => $value) { 141 $data_string .= $key.'='.$value.'&'; 142 } 143 144 $data_string = rtrim($data_string, '&'); 145 146 $options[CURLOPT_POST] = count($data); 147 $options[CURLOPT_POSTFIELDS] = $data_string; 148 } 149 150 else if($method == 'GET'){ 151 // Nothing to be done here. 152 } 153 154 else if($method == 'PUT'){ 155 156 } 157 158 else if($method == 'DELETE'){ 159 $options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; 160 } 161 162 else if($method == 'PATCH'){ 163 $data_string = ""; 164 165 foreach ($data as $key => $value) { 166 $data_string .= $key.'='.$value.'&'; 167 } 168 169 $data_string = rtrim($data_string, '&'); 170 171 $options[CURLOPT_POST] = count($data); 172 $options[CURLOPT_POSTFIELDS] = $data_string; 173 $options[CURLOPT_CUSTOMREQUEST] = 'PATCH'; 174 } 175 176 $this->curl = curl_init(); 177 curl_setopt_array($this->curl, $options); 178 $response = curl_exec($this->curl); 179 $headers = curl_getinfo($this->curl); 180 181 $errorNumber = curl_errno($this->curl); 182 $errorMessage = curl_error($this->curl); 183 184 return array('response' => $response, 'headers' => $headers, 'error' => $errorMessage, 'errno' => $errorNumber); 185 } 186 187 188 /** 189 * Authenticate the application. 190 * @return array PHP array of the JSON response. 191 */ 192 public function apiAuth(){ 193 $response = $this->apiRequest('auth/', 'POST', $data = array('username' => $this->username, 'password' => $this->password)); 194 $json = @json_decode($response['response'], true); 195 196 if($response['errno']) throw new Exception("Exception: " . $response['error']); 197 if(!$json["success"]) throw new Exception("Application authentication failed. Check credentials"); 198 199 $this->APP_TOKEN = $json["token"]; 200 return $json; 201 } 202 203 /** 204 * List all the offers of the user. 205 * @return array PHP array of the JSON response. 206 */ 207 public function listAllOffers(){ 208 if(!$this->APP_TOKEN) throw new Exception("Please authenticate your application."); 209 $response = $this->apiRequest('offer/', 'GET'); 210 $json = @json_decode($response['response'], true); 211 if(!$json['success']) throw new Exception("Error in listing all offers."); 212 return $json; 213 } 214 215 /** 216 * List the complete offer details of the offer id mentioned in $slug. 217 * @param array $slug The offer id. 218 * @return array PHP array of the JSON response. 219 */ 220 public function listOneOfferDetail($slug){ 221 if(!$this->APP_TOKEN) throw new Exception("Please authenticate your application."); 222 $response = $this->apiRequest("offer/$slug/", 'GET'); 223 $json = @json_decode($response['response'], true); 224 if(!$json['success']) throw new Exception("Error in listing offer of $slug."); 225 return $json; 226 } 227 228 /** 229 * Used to get an upload URL for the files to be uploaded, i.e. The cover image and the File. 230 * @return array PHP array of the JSON response. 231 */ 232 public function getUploadUrl(){ 233 if(!$this->APP_TOKEN) throw new Exception("Please authenticate your application."); 234 $response = $this->apiRequest('offer/get_file_upload_url/', 'GET'); 235 $json = @json_decode($response['response'], true); 236 if(!$json['success']) throw new Exception("Cannot get an URL."); 237 return $json["upload_url"]; 238 } 239 240 /** 241 * Deletes the authentication toekn recieved from Instamojo. 242 */ 243 public function deleteAuthToken(){ 244 if(!$this->APP_TOKEN) throw new Exception("No token loaded, unable to delete."); 245 $response = $this->apiRequest("auth/$this->APP_TOKEN/", 'DELETE'); 246 $json = @json_decode($response['response'], true); 247 if(!$json['success']) throw new Exception("Could not delete auth token."); 248 $this->APP_TOKEN = null; 249 } 250 251 /** 252 * Archives(Deletes) the offer whos id is supplied. 253 * @param string $slug Id of the offer. 254 */ 255 public function archiveOffer($slug){ 256 if(!$this->APP_TOKEN) throw new Exception("No token loaded, unable to archive."); 257 $response = $this->apiRequest("offer/$slug/", 'DELETE'); 258 $json = @json_decode($response['response'], true); 259 if(!$json['success']) throw new Exception("Could not archive offer."); 260 } 261 262 /** 263 * Title, keep concise since slug is auto-generated 264 * from the title [max: 200 char, required] 265 * @param string $title Title of the offer. 266 */ 267 public function setTitle($title){ 268 if(strlen($title) > 200) throw new Exception("Title size not more than 200 allowed."); 269 $this->title = (string) $title; 270 } 271 272 /** 273 * Detailed description of the offer, can contain markdown. 274 * @param string $description Description of the offer. 275 */ 276 public function setDescription($description){ 277 $this->description = (string) $description; 278 } 279 280 /** 281 * Currency of the offer. Can be INR or USD. 282 * @param string $currency Currency of the offer. 283 */ 284 public function setCurrency($currency){ 285 if(!$this->allowed_currency($currency)) throw new Exception("Invalid currency."); 286 $this->currency = (string) $currency; 287 } 288 289 /** 290 * Price of the offer as a decimal (up to 2 decimal places) 291 * @param string $base_price Base price of the offer. 292 */ 293 public function setBasePrice($base_price){ 294 if(!(is_numeric($base_price) && (int)$base_price >= 0)) throw new Exception("The base_price should be a positive number or zero."); 295 $this->base_price = (string) $base_price; 296 } 297 298 /** 299 * Keep zero for unlimited quantity, 300 * any other positive number will limit sales/claims of the offer 301 * and make it unavailable afterwards. 302 * @param string $quantity of the offer. 0 for unlimited. 303 */ 304 public function setQuantity($quantity){ 305 if(!(is_numeric($quantity) && (int)$quantity == $quantity && (int)$quantity >= 0)) 306 throw new Exception("The quantity should be a positive number or zero."); 307 $this->quantity = (string) $quantity; 308 } 309 310 /** 311 * Required for events, date-time when the event begins. 312 * Format: YYYY-MM-DD HH:mm 313 * @param string $start_date Start date of the offer. 314 */ 315 public function setStartDate($start_date){ 316 $this->start_date = $start_date; 317 } 318 319 /** 320 * Required for events, date-time when the event begins. 321 * Format: YYYY-MM-DD HH:mm 322 * @param string $end_date End date of the offer. 323 */ 324 public function setEndDate($end_date){ 325 $this->end_date = $end_date; 326 } 327 328 /** 329 * Timezone of the event. Example: Asia/Kolkata 330 * @param string $timezone Timezone of the offer. 331 */ 332 public function setTimeZone($timezone){ 333 $this->timezone = $timezone; 334 } 335 336 /** 337 * Required for events, location where the event will be held. 338 * @param string $venue Venue of the offer. 339 */ 340 public function setVenue($venue){ 341 $this->venue = $venue; 342 } 343 344 /** 345 * You can set this to a thank-you page on your site. 346 * Buyers will be redirected here after successful payment. 347 * @param string $redirect_url The URL to be redirected to after a buyer downloads the digital file. 348 */ 349 public function setRedirectURL($redirect_url){ 350 $this->redirect_url = $redirect_url; 351 } 352 353 /** 354 * A note to be displayed to buyer after successful 355 * payment. This will also be sent via email and 356 * in the receipt/ticket that is sent as attachment 357 * to the email. 358 */ 359 public function setNote($note){ 360 $this->note = $note; 361 } 362 363 /** 364 * Path to the file you want to sell. 365 * @param string $file_path Path to the file. 366 */ 367 public function setFilePath($file_path){ 368 $this->file_path = $file_path; 369 } 370 371 /** 372 * Path to the cover image. 373 * @param string $cover_path Path to the cover image. 374 */ 375 public function setCoverPath($cover_path){ 376 $this->cover_path = $cover_path; 377 } 378 379 /** 380 * A utility function to send POST request to the URL recieved from 381 * getUploadUrl() and upload a file. 382 * @param string $file_upload_url The URL recieved from getUploadUrl(). 383 * @param string $file_path The path to the file in your computer. 384 * @return JSON The JSON recieved from the request. 385 */ 386 private function getFileUploadJson($file_upload_url, $file_path){ 387 $file_path = realpath($file_path); 388 $file_name = basename($file_path); 389 $ch = curl_init(); 390 $data = array('fileUpload' => '@'.$file_path); 391 curl_setopt($ch, CURLOPT_URL, $file_upload_url); 392 curl_setopt($ch, CURLOPT_POST, 1); 393 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 394 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 395 $uploadJson = curl_exec($ch); 396 return $uploadJson; 397 } 398 399 /** 400 * Utility function to build the data array which will be used 401 * to send data for creating offer through apiRequest(). 402 * @return array The array to be used later to send data about the offer to Instamojo API. 403 */ 404 private function buildDataArray(){ 405 $data = array(); 406 if(!$this->title) throw new Exception("title is a must for creating an offer."); 407 $data['title'] = $this->title; 408 if(!$this->description) throw new Exception("description is a must for creating an offer."); 409 $data['description'] = $this->description; 410 if(!$this->currency) throw new Exception("currency is a must for creating an offer."); 411 $data['currency'] = $this->currency; 412 if(!$this->base_price && $this->base_price != '0') throw new Exception("base_price is a must for creating an offer."); 413 $data['base_price'] = $this->base_price; 414 if($this->quantity) 415 $data['quantity'] = $this->quantity; 416 if($this->start_date) 417 $data['start_date'] = $this->start_date; 418 if($this->end_date) 419 $data['end_date'] = $this->end_date; 420 if($this->timezone) 421 $data['timezone'] = $this->timezone; 422 if($this->venue) 423 $data['venue'] = $this->venue; 424 if($this->redirect_url) 425 $data['redirect_url'] = $this->redirect_url; 426 if($this->note) 427 $data['note'] = $this->note; 428 if(!$this->file_path) throw new Exception("file is a must for creating an offer."); 429 430 $upload_url = $this->getUploadUrl(); 431 $file_upload_json = $this->getFileUploadJson($upload_url, $this->file_path); 432 $data['file_upload_json'] = $file_upload_json; 433 434 if($this->cover_path){ 435 $upload_url = $this->getUploadUrl(); 436 $cover_upload_json = $this->getFileUploadJson($upload_url, $this->cover_path); 437 $data['cover_image_json'] = $cover_upload_json; 438 } 439 return $data; 440 } 441 442 /** 443 * Function to create an instamojo offer. 444 * @return JSON The response resieved from Instamojo API. 445 */ 446 public function createOffer(){ 447 $offer_array = $this->buildDataArray(); 448 $request = $this->apiRequest('offer/', 'POST', $data = $offer_array); 449 $json = @json_decode($request['response'], true); 450 if(!$json['success']) throw new Exception("Connot create offer."); 451 return $request; 452 } 453 454 /** 455 * Function to to edit an offer. 456 * @param string $slug The offer ID. 457 * @return JSON The response recieved from Instamojo API. 458 */ 459 public function editOffer($slug){ 460 $offer_array = $this->buildDataArray(); 461 $request = $this->apiRequest("offer/$slug/", 'PATCH', $data = $offer_array); 462 $json = @json_decode($request['response'], true); 463 if(!$json['success']) throw new Exception("Connot edit offer."); 464 return $request; 465 } 3 /** 4 * The Instamojo API wrapper 5 */ 6 7 class Instamojo { 8 9 // internal constant to enable/disable debugging 10 const debug = true; 11 12 // Instamojo API base URL 13 const API_URL = 'https://www.instamojo.com/api/1/'; 14 15 // API version 16 const version = '0.1'; 17 18 // A curl instance 19 protected $curl; 20 21 // APP_ID provided by Instamojo 22 protected $APP_ID = null; 23 24 // The username of the User 25 protected $username = null; 26 27 // The password of the User 28 protected $password = null; 29 30 // Token provided by Instamojo 31 protected $AUTH_TOKEN = null; 32 33 // Time-out 34 protected $timeout = 10; 35 36 public $title = null; 37 public $description = null; 38 public $currency = null; 39 public $base_price = null; 40 public $quantity = null; 41 public $start_date = null; 42 public $end_date = null; 43 public $timezone = null; 44 public $venue = null; 45 public $redirect_url = null; 46 public $note = null; 47 public $file_path = null; 48 public $cover_path = null; 49 50 private $_currencies = array("INR", "USD"); 51 52 /** 53 * Default constructor 54 * @param string $username Instamojo username of the user 55 * @param string $password Instamojo password of the user 56 * @param string $app_id Application ID provided by Instamojo 57 */ 58 public function __construct($app_id, $username = null, $password = null) 59 { 60 if (isset($app_id)) 61 { 62 $this->setID($app_id); 63 } 64 else 65 { 66 throw new Exception("$app_id is required.", 1); 67 } 68 69 if (isset($username)) 70 { 71 $this->setUsername($username); 72 } 73 74 if (isset($password)) 75 { 76 $this->setPassword($password); 77 } 78 } 79 80 /** 81 * Default destructor. 82 */ 83 public function __destruct() 84 { 85 if ($this->curl != null) 86 { 87 curl_close($this->curl); 88 } 89 } 90 91 /** 92 * Get the version of the API wrapper. 93 * @return string Version of the API wrapper. 94 */ 95 public function getVersion() 96 { 97 return self::version; 98 } 99 100 private function allowed_currency($currency) 101 { 102 if (in_array($currency, $this->currencies)) 103 { 104 return true; 105 } 106 return false; 107 } 108 109 /** 110 * Set the username 111 * @param string $username Instamojo username of the user 112 */ 113 private function setUsername($username) 114 { 115 $this->username = (string) $username; 116 } 117 118 /** 119 * Set the password 120 * @param string $password Instamojo username of the password 121 */ 122 private function setPassword($password) 123 { 124 $this->password = (string) $password; 125 } 126 127 /** 128 * Set the ID 129 * @param string $id Instamojo APP_ID provided by Instamojo 130 */ 131 private function setID($id) 132 { 133 $this->APP_ID = (string) $id; 134 } 135 136 /** 137 * Create the absolute path for the request 138 * @param string $url The base URL (Here it is used by API_URL) 139 * @param string $path The relative path 140 */ 141 private function buildPath($url, $path) 142 { 143 return $url . $path; 144 } 145 146 /** 147 * Set Authentication Token for the instance 148 * @param string $token The authentication token 149 */ 150 public function setAuthToken($token) 151 { 152 $this->AUTH_TOKEN = (string) $token; 153 } 154 155 /** 156 * Request the Instamojo API 157 * @param string $path The relative path 158 * @param string $method POST/GET/POST/DELETE 159 * @param array $data Data to be passed 160 */ 161 private function apiRequest($path, $method, array $data = null) 162 { 163 $path = (string) $path; 164 $method = (string) $method; 165 $data = (array) $data; 166 167 $headers = array("X-App-Id: $this->APP_ID"); 168 169 if ($this->AUTH_TOKEN) 170 { 171 $headers[] = "X-Auth-Token: $this->AUTH_TOKEN"; 172 } 173 174 $request_path = $this->buildPath(self::API_URL, $path); 175 176 $options = array(); 177 $options[CURLOPT_HTTPHEADER] = $headers; 178 $options[CURLOPT_RETURNTRANSFER] = true; 179 $options[CURLOPT_URL] = $request_path; 180 181 if ($method == 'POST') 182 { 183 $data_string = ""; 184 185 foreach ($data as $key => $value) { 186 $data_string .= $key.'='.$value.'&'; 187 } 188 189 $data_string = rtrim($data_string, '&'); 190 191 $options[CURLOPT_POST] = count($data); 192 $options[CURLOPT_POSTFIELDS] = $data_string; 193 } 194 195 else if ($method == 'GET') 196 { 197 // Nothing to be done here. 198 } 199 200 else if ($method == 'PUT') 201 { 202 203 } 204 205 else if ($method == 'DELETE') 206 { 207 $options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; 208 } 209 210 else if ($method == 'PATCH') 211 { 212 $data_string = ""; 213 214 foreach ($data as $key => $value) 215 { 216 $data_string .= $key.'='.$value.'&'; 217 } 218 219 $data_string = rtrim($data_string, '&'); 220 221 $options[CURLOPT_POST] = count($data); 222 $options[CURLOPT_POSTFIELDS] = $data_string; 223 $options[CURLOPT_CUSTOMREQUEST] = 'PATCH'; 224 } 225 226 $this->curl = curl_init(); 227 curl_setopt_array($this->curl, $options); 228 $response = curl_exec($this->curl); 229 $headers = curl_getinfo($this->curl); 230 231 $errorNumber = curl_errno($this->curl); 232 $errorMessage = curl_error($this->curl); 233 234 return array('response' => $response, 'headers' => $headers, 'error' => $errorMessage, 'errno' => $errorNumber); 235 } 236 237 238 /** 239 * Authenticate the application. 240 * @return array PHP array of the JSON response. 241 */ 242 public function apiAuth() 243 { 244 $response = $this->apiRequest('auth/', 'POST', $data = array('username' => $this->username, 'password' => $this->password)); 245 $json = @json_decode($response['response'], true); 246 247 if ($response['errno']) 248 { 249 throw new Exception("Exception: " . $response['error']); 250 } 251 if (!$json["success"]) 252 { 253 throw new Exception("Application authentication failed. Check credentials"); 254 } 255 256 $this->AUTH_TOKEN = $json["token"]; 257 return $json; 258 } 259 260 /** 261 * List all the offers of the user. 262 * @return array PHP array of the JSON response. 263 */ 264 public function listAllOffers() 265 { 266 if (!$this->AUTH_TOKEN) 267 { 268 throw new Exception("Please authenticate your application."); 269 } 270 $response = $this->apiRequest('offer/', 'GET'); 271 $json = @json_decode($response['response'], true); 272 if (!$json['success']) 273 { 274 throw new Exception("Error in listing all offers."); 275 } 276 return $json; 277 } 278 279 /** 280 * List the complete offer details of the offer id mentioned in $slug. 281 * @param array $slug The offer id. 282 * @return array PHP array of the JSON response. 283 */ 284 public function listOneOfferDetail($slug) 285 { 286 if (!$this->AUTH_TOKEN) 287 { 288 throw new Exception("Please authenticate your application."); 289 } 290 $response = $this->apiRequest("offer/$slug/", 'GET'); 291 $json = @json_decode($response['response'], true); 292 if (!$json['success']) 293 { 294 throw new Exception("Error in listing offer of $slug."); 295 } 296 return $json; 297 } 298 299 /** 300 * Used to get an upload URL for the files to be uploaded, i.e. The cover image and the File. 301 * @return array PHP array of the JSON response. 302 */ 303 public function getUploadUrl() 304 { 305 if (!$this->AUTH_TOKEN) 306 { 307 throw new Exception("Please authenticate your application."); 308 } 309 $response = $this->apiRequest('offer/get_file_upload_url/', 'GET'); 310 $json = @json_decode($response['response'], true); 311 if (!$json['success']) 312 { 313 throw new Exception("Cannot get an URL."); 314 } 315 return $json["upload_url"]; 316 } 317 318 /** 319 * Deletes the authentication token received from Instamojo 320 */ 321 public function deleteAuthToken() 322 { 323 if (!$this->AUTH_TOKEN) 324 { 325 throw new Exception("No token loaded, unable to delete."); 326 } 327 $response = $this->apiRequest("auth/$this->AUTH_TOKEN/", 'DELETE'); 328 $json = @json_decode($response['response'], true); 329 if (!$json['success']) 330 { 331 throw new Exception("Could not delete auth token."); 332 } 333 $this->AUTH_TOKEN = null; 334 } 335 336 /** 337 * Archives(Deletes) the offer whose id is supplied 338 * @param string $slug Id of the offer 339 */ 340 public function archiveOffer($slug) 341 { 342 if (!$this->AUTH_TOKEN) 343 { 344 throw new Exception("No token loaded, unable to archive."); 345 } 346 $response = $this->apiRequest("offer/$slug/", 'DELETE'); 347 $json = @json_decode($response['response'], true); 348 if (!$json['success']) 349 { 350 throw new Exception("Could not archive offer."); 351 } 352 } 353 354 /** 355 * Title, keep concise since slug is auto-generated 356 * from the title [max: 200 char, required] 357 * @param string $title Title of the offer 358 */ 359 public function setTitle($title) 360 { 361 if (strlen($title) > 200) 362 { 363 throw new Exception("Title size not more than 200 allowed."); 364 } 365 $this->title = (string) $title; 366 } 367 368 /** 369 * Detailed description of the offer, can contain markdown 370 * @param string $description Description of the offer 371 */ 372 public function setDescription($description) 373 { 374 $this->description = (string) $description; 375 } 376 377 /** 378 * Currency of the offer. Can be INR or USD 379 * @param string $currency Currency of the offer 380 */ 381 public function setCurrency($currency) 382 { 383 if (!$this->allowed_currency($currency)) 384 { 385 throw new Exception("Invalid currency."); 386 } 387 $this->currency = (string) $currency; 388 } 389 390 /** 391 * Price of the offer as a decimal (up to 2 decimal places) 392 * @param string $base_price Base price of the offer 393 */ 394 public function setBasePrice($base_price) 395 { 396 if (!(is_numeric($base_price) && (int) $base_price >= 0)) 397 { 398 throw new Exception("The base_price should be a positive number or zero."); 399 } 400 $this->base_price = (string) $base_price; 401 } 402 403 /** 404 * Keep zero for unlimited quantity, 405 * any other positive number will limit sales/claims of the offer 406 * and make it unavailable afterwards 407 * @param string $quantity of the offer. 0 for unlimited 408 */ 409 public function setQuantity($quantity) 410 { 411 if (!(is_numeric($quantity) && (int) $quantity == $quantity && (int) $quantity >= 0)) 412 { 413 throw new Exception("The quantity should be a positive number or zero."); 414 } 415 $this->quantity = (string) $quantity; 416 } 417 418 /** 419 * Required for events, date-time when the event begins 420 * Format: YYYY-MM-DD HH:mm 421 * @param string $start_date Start date of the offer 422 */ 423 public function setStartDate($start_date) 424 { 425 $this->start_date = $start_date; 426 } 427 428 /** 429 * Required for events, date-time when the event begins 430 * Format: YYYY-MM-DD HH:mm 431 * @param string $end_date End date of the offer 432 */ 433 public function setEndDate($end_date) 434 { 435 $this->end_date = $end_date; 436 } 437 438 /** 439 * Timezone of the event. Example: Asia/Kolkata 440 * @param string $timezone Timezone of the offer 441 */ 442 public function setTimeZone($timezone) 443 { 444 $this->timezone = $timezone; 445 } 446 447 /** 448 * Required for events, location where the event will be held 449 * @param string $venue Venue of the offer 450 */ 451 public function setVenue($venue) 452 { 453 $this->venue = $venue; 454 } 455 456 /** 457 * You can set this to a thank-you page on your site 458 * Buyers will be redirected here after successful payment 459 * @param string $redirect_url The URL to be redirected to after a buyer downloads the digital file 460 */ 461 public function setRedirectURL($redirect_url) 462 { 463 $this->redirect_url = $redirect_url; 464 } 465 466 /** 467 * A note to be displayed to buyer after successful 468 * payment. This will also be sent via email and 469 * in the receipt/ticket that is sent as attachment 470 * to the email. 471 */ 472 public function setNote($note) 473 { 474 $this->note = $note; 475 } 476 477 /** 478 * Path to the file you want to sell 479 * @param string $file_path Path to the file 480 */ 481 public function setFilePath($file_path) 482 { 483 $this->file_path = $file_path; 484 } 485 486 /** 487 * Path to the cover image 488 * @param string $cover_path Path to the cover image 489 */ 490 public function setCoverPath($cover_path) 491 { 492 $this->cover_path = $cover_path; 493 } 494 495 /** 496 * A utility function to send POST request to the URL received from 497 * getUploadUrl() and upload a file. 498 * @param string $file_upload_url The URL received from getUploadUrl(). 499 * @param string $file_path The path to the file in your computer. 500 * @return JSON The JSON received from the request. 501 */ 502 private function getFileUploadJson($file_upload_url, $file_path){ 503 $file_path = realpath($file_path); 504 $file_name = basename($file_path); 505 $ch = curl_init(); 506 $data = array('fileUpload' => '@'.$file_path); 507 curl_setopt($ch, CURLOPT_URL, $file_upload_url); 508 curl_setopt($ch, CURLOPT_POST, 1); 509 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 510 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 511 $uploadJson = curl_exec($ch); 512 return $uploadJson; 513 } 514 515 /** 516 * Utility function to build the data array which will be used 517 * to send data for creating offer through apiRequest() 518 * @return array The array to be used later to send data about the offer to Instamojo API 519 */ 520 private function buildDataArray() 521 { 522 $data = array(); 523 if (!$this->title) throw new Exception("title is a must for creating an offer."); 524 $data['title'] = $this->title; 525 if (!$this->description) throw new Exception("description is a must for creating an offer."); 526 $data['description'] = $this->description; 527 if (!$this->currency) throw new Exception("currency is a must for creating an offer."); 528 $data['currency'] = $this->currency; 529 if (!$this->base_price && $this->base_price != '0') throw new Exception("base_price is a must for creating an offer."); 530 $data['base_price'] = $this->base_price; 531 if ($this->quantity) 532 $data['quantity'] = $this->quantity; 533 if ($this->start_date) 534 $data['start_date'] = $this->start_date; 535 if ($this->end_date) 536 $data['end_date'] = $this->end_date; 537 if ($this->timezone) 538 $data['timezone'] = $this->timezone; 539 if ($this->venue) 540 $data['venue'] = $this->venue; 541 if ($this->redirect_url) 542 $data['redirect_url'] = $this->redirect_url; 543 if ($this->note) 544 $data['note'] = $this->note; 545 if (!$this->file_path) throw new Exception("file is a must for creating an offer."); 546 547 $upload_url = $this->getUploadUrl(); 548 $file_upload_json = $this->getFileUploadJson($upload_url, $this->file_path); 549 $data['file_upload_json'] = $file_upload_json; 550 551 if ($this->cover_path){ 552 $upload_url = $this->getUploadUrl(); 553 $cover_upload_json = $this->getFileUploadJson($upload_url, $this->cover_path); 554 $data['cover_image_json'] = $cover_upload_json; 555 } 556 return $data; 557 } 558 559 /** 560 * Function to create an instamojo offer 561 * @return JSON The response received from Instamojo API 562 */ 563 public function createOffer() 564 { 565 $offer_array = $this->buildDataArray(); 566 $request = $this->apiRequest('offer/', 'POST', $data = $offer_array); 567 $json = @json_decode($request['response'], true); 568 if (!$json['success']) throw new Exception("Connot create offer."); 569 return $request; 570 } 571 572 /** 573 * Function to to edit an offer 574 * @param string $slug The offer ID 575 * @return JSON The response received from Instamojo API 576 */ 577 public function editOffer($slug){ 578 $offer_array = $this->buildDataArray(); 579 $request = $this->apiRequest("offer/$slug/", 'PATCH', $data = $offer_array); 580 $json = @json_decode($request['response'], true); 581 if (!$json['success']) throw new Exception("Connot edit offer."); 582 return $request; 583 } 466 584 } 467 585
Note: See TracChangeset
for help on using the changeset viewer.