PageRenderTime 73ms CodeModel.GetById 3ms app.highlight 60ms RepoModel.GetById 0ms app.codeStats 1ms

/StripeComponent.php

https://github.com/hashmode/CakePHP-Stripe-Component-Full
PHP | 2674 lines | 1293 code | 556 blank | 825 comment | 144 complexity | c2260cb654658762fa785abf089f21e2 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php 
   2/**
   3 * Stripe Component
   4 * 
   5 * PHP 5
   6 * 
   7 * Licensed under The MIT License
   8 * associative
   9 * @version		1.0
  10 * @author		http://hashmode.com
  11 * @license		MIT License (http://www.opensource.org/licenses/mit-license.php)
  12 * @link		https://github.com/hashmode/CakePHP-Stripe-Component-Full
  13 * 
  14 * Compatible with Stripe API version 1.15.0
  15 * 
  16 * ***** IMPORTANT ******
  17 * Stripe PHP library is not included, it should be downloaded from Stripe's website
  18 * @link	https://stripe.com/docs/libraries  
  19 * 
  20 * Stripe php documentation
  21 * @link	https://stripe.com/docs/api/php
  22 */
  23
  24App::uses('Component', 'Controller');
  25
  26/**
  27 *	Stripe Components allows to use Stripe Payments 
  28 *	
  29 */
  30class StripeComponent extends Component {
  31	
  32	
  33/**
  34 * Stripe mode, can be 'Live' or 'Test' 
  35 *
  36 * @var string
  37 */
  38	public $mode = 'Test';
  39	
  40
  41/**
  42 * Stripe API Secret key
  43 *
  44 * @var string
  45 */
  46	public $key = null;
  47	
  48
  49/**
  50 * Stripe currency, default is 'usd' 
  51 *
  52 * @var string
  53 */
  54	public $currency = 'usd';
  55	
  56	
  57/**
  58 * 
  59 * If provided, statuses will be saved in that file, default is false
  60 * if enabled, log should be added in bootstrap, e.g. if log file should be in tmp/logs/stripe.log
  61 * 
  62	CakeLog::config('stripe', array(
  63		'engine' => 'FileLog',
  64		'file' => 'stripe',
  65	));
  66 *
  67 * @var string
  68 */
  69	public $logFile = false;
  70
  71	
  72/**
  73 * Can be 'both', 'success' or 'error', to what results to save, default is 'error'
  74 *
  75 * @var string
  76 */
  77	public $logType = 'error';
  78
  79	
  80/**
  81 *  For saving the reflection class, to use in the loop
  82 *
  83 * @var array
  84 */
  85	protected $reflectionClass = array();
  86	
  87	
  88
  89
  90/**
  91 * Initialize component
  92 *
  93 * @param Controller $controller Instantiating controller
  94 * @return void
  95 */
  96
  97	public function initialize(Controller $controller) {
  98		$this->Controller = $controller;
  99		
 100		App::import ( 'Vendor', 'stripe', 
 101			array(
 102				'file' => 'stripe'.DS.'lib'.DS.'Stripe.php' 
 103			)
 104		);
 105		
 106		if (!class_exists('Stripe')) {
 107			throw new CakeException('Stripe PHP Library not found. Be sure it is unpacked in app/Vendor/stripe directory.
 108									It can be downloaded from https://stripe.com/docs/libraries');
 109		}
 110
 111		// if mode is not set in bootstrap, defaults to 'Test' 
 112		$mode = Configure::read('Stripe.mode');
 113		if ($mode) {
 114			$this->mode = $mode;
 115		}
 116
 117		// set the Stripe API key
 118		$this->key = Configure::read('Stripe.' . $this->mode . 'Secret');
 119		if (!$this->key) {
 120			throw new CakeException('Stripe API Secret key is not set');
 121		}
 122		
 123		// if currency is not set, defaults to 'usd'
 124		$currency = Configure::read('Stripe.currency');
 125		if ($currency) {
 126			$this->currency = strtolower($currency);
 127		}
 128		
 129		// set logging
 130		if (isset($this->settings['logFile'])) {
 131			$this->logFile = $this->settings['logFile'];
 132			
 133			// validate logFile 
 134			$logFiles = CakeLog::configured();
 135			if (array_search($this->logFile, $logFiles) === false) {
 136				throw new CakeException(__('Logging file is not added. Add this in your bootstrap').': '
 137											.'CakeLog::config("'.$this->logFile.'", array("engine" => "FileLog", "file" => "'.$this->logFile.'" ));' );
 138			}
 139
 140			if (isset($this->settings['logType'])) {
 141				$this->logType = $this->settings['logType'];
 142
 143				// validate logType
 144				if (!in_array($this->logType, array('both', 'success', 'error'))) {
 145					throw new CakeException(__('Log Type can be only set as "success", "error" or "both" '));
 146				}
 147			}
 148		}
 149
 150	}	
 151
 152	
 153	
 154	
 155	
 156	
 157	
 158	
 159	
 160	
 161
 162/**
 163 * charge method
 164 * Charges the given credit card(card_id, array or token) or customer
 165 *
 166 * @param array $data
 167 * @param string $customerId[optional] 
 168 * @return array
 169 * 
 170 * @link https://stripe.com/docs/api#create_charge
 171 */
 172	public function charge($data = null, $customerId = null) {
 173		if (!$customerId && (!isset($data['card']) || empty($data['card'])) ) {
 174			throw new CakeException(__('Customer Id or Card is required'));
 175		}
 176		
 177		if ($customerId) {
 178			$data['customer'] = $customerId;
 179		}
 180		
 181		// set default currency
 182		if (!isset($data['currency'])) {
 183			$data['currency'] = $this->currency;
 184		}
 185		
 186		return $this->request(__FUNCTION__, $data);
 187	}
 188	
 189/**
 190 * retrieveCharge method
 191 * Retrieves the details of a charge that has previously been created
 192 *
 193 * @param string $chargeId
 194 * @return array
 195 * 
 196 * @link https://stripe.com/docs/api#retrieve_charge
 197 */
 198	public function retrieveCharge($chargeId = null) {
 199		if (!$chargeId) {
 200			throw new CakeException(__('Charge Id is required'));
 201		}
 202		
 203		$data = array(
 204			'charge_id' => $chargeId	
 205		);
 206		
 207		return $this->request(__FUNCTION__, $data);
 208	}
 209	
 210	
 211/**
 212 * updateCharge method
 213 * Updates the specified charge
 214 *
 215 * @param string $chargeId
 216 * @param array $data
 217 * @return array
 218 *
 219 * @link https://stripe.com/docs/api#update_charge
 220 */
 221	public function updateCharge($chargeId = null, $data = array()) {
 222		if (!$chargeId) {
 223			throw new CakeException(__('Charge Id is not provided'));
 224		}
 225	
 226		if (empty($data)) {
 227			throw new CakeException(__('No data is provided to updates the card'));
 228		}
 229	
 230		$data = array(
 231			'charge_id' => $chargeId,
 232			'fields' => $data,
 233		);
 234	
 235		return $this->request(__FUNCTION__, $data);
 236	}
 237	
 238	
 239/**
 240 * refundCharge method
 241 * Refunds a charge that has previously been created but not yet refunded
 242 *
 243 * @param string $chargeId
 244 * @param array $data
 245 * @return array
 246 *
 247 * @link https://stripe.com/docs/api#refund_charge
 248 */
 249	public function refundCharge($chargeId = null, $data = array()) {
 250		if (!$chargeId) {
 251			throw new CakeException(__('Charge Id is not provided'));
 252		}
 253	
 254		$data['charge_id'] = $chargeId;
 255
 256		return $this->request(__FUNCTION__, $data);
 257	}
 258	
 259	
 260/**
 261 * captureCharge method
 262 * Capture the payment of an existing, uncaptured, charge.
 263 *
 264 * @param string $chargeId
 265 * @param array $data
 266 * @return array
 267 *
 268 * @link https://stripe.com/docs/api#charge_capture
 269 */
 270	public function captureCharge($chargeId = null, $data = array()) {
 271		if (!$chargeId) {
 272			throw new CakeException(__('Charge Id is not provided'));
 273		}
 274	
 275		$data['charge_id'] = $chargeId;
 276
 277		return $this->request(__FUNCTION__, $data);
 278	}
 279	
 280	
 281/**
 282 * listCharges method
 283 * Returns a list of charges you've previously created
 284 *
 285 * @param array $data
 286 * @return array
 287 *
 288 * @link https://stripe.com/docs/api#list_charges
 289 */
 290	public function listCharges($data = array()) {
 291		$data = array(
 292			'options' => $data
 293		);
 294	
 295		return $this->request(__FUNCTION__, $data);
 296	}
 297	
 298	
 299	
 300	
 301	
 302	
 303	
 304	
 305	
 306	
 307	
 308	
 309	
 310	
 311	
 312	
 313	
 314	
 315	
 316	
 317	
 318
 319/**
 320 * createCustomer method
 321 * Creates a new customer
 322 *
 323 * @param array	$data - according to customer object
 324 * @return array
 325 * 
 326 * @link  https://stripe.com/docs/api/php#create_customers
 327 */
 328	public function createCustomer($data) {
 329		if (empty($data) || !is_array($data)) {
 330			throw new CakeException(__('Data is empty or is not an array'));
 331		}
 332
 333		return $this->request(__FUNCTION__, $data);
 334	}
 335	
 336
 337/**
 338 * retrieveCustomer method
 339 * Retrives the customer information
 340 *
 341 * @param string $customerId
 342 * @return array
 343 * 
 344 * @link https://stripe.com/docs/api/php#retrieve_customer
 345 */
 346	public function retrieveCustomer($customerId = null) {
 347		if (!$customerId) {
 348			throw new CakeException(__('Customer Id is not provided'));
 349		}
 350
 351		$data = array(
 352			'customer_id' => $customerId	
 353		);
 354
 355		return $this->request(__FUNCTION__, $data);
 356	}
 357	
 358	
 359/**
 360 * updateCustomer method
 361 * Updates the customer info
 362 *
 363 * @param string $customerId
 364 * @param array $fields - fields to be updated
 365 * @return array
 366 * 
 367 * @link https://stripe.com/docs/api/php#update_customer
 368 */
 369	public function updateCustomer($customerId = null, $fields = array()) {
 370		if (!$customerId) {
 371			throw new CakeException(__('Customer Id is not provided'));
 372		}
 373		
 374		if (empty($fields)) {
 375			throw new CakeException(__('Update fields are empty'));
 376		}
 377
 378		$data = array(
 379			'customer_id' => $customerId,	
 380			'fields' => $fields
 381		);
 382
 383		return $this->request(__FUNCTION__, $data);
 384	}
 385	
 386	
 387/**
 388 * deleteCustomer method
 389 * Deletes the customer
 390 *
 391 * @param string $customerId
 392 * @return array
 393 * 
 394 * @link https://stripe.com/docs/api/php#delete_customer
 395 */
 396	public function deleteCustomer($customerId = null) {
 397		if (!$customerId) {
 398			throw new CakeException(__('Customer Id is not provided'));
 399		}
 400		
 401		$data = array(
 402			'customer_id' => $customerId
 403		);
 404
 405		return $this->request(__FUNCTION__, $data);
 406	}
 407	
 408/**
 409 * listCustomers method
 410 * Returns array with customers
 411 *  *
 412 * @param array $data
 413 * @param array $cards - default is false, if true each customers cards will be returned as array
 414 * @param array $subscriptions - default is false, if true each customers subscriptions will be returned as array
 415 * @return array
 416 * 
 417 * @link https://stripe.com/docs/api/php#list_customers
 418 */
 419	public function listCustomers($data = array()) {
 420		$data = array(
 421			'options' => $data
 422		);
 423		return $this->request(__FUNCTION__, $data);
 424	}
 425	
 426	
 427	
 428	
 429/**
 430 * createCard method
 431 * Creates a new card for the given customer
 432 * 
 433 * @param string $customerId
 434 * @param mixed $data - card data, token or array 
 435 * @return array
 436 * 
 437 * @link https://stripe.com/docs/api/php#create_card
 438 */
 439	public function createCard($customerId = null, $card = null) {
 440		if (!$customerId) {
 441			throw new CakeException(__('Customer Id is not provided'));
 442		}
 443		
 444		if (!$card) {
 445			throw new CakeException(__('Card data is not provided'));
 446		}
 447		
 448		$data = array(
 449			'customer_id' => $customerId,
 450			'card' => $card
 451		);
 452
 453		return $this->request(__FUNCTION__, $data);
 454	}
 455	
 456/**
 457 * retrieveCard method
 458 * Retrives an existing card for the given customer
 459 * 
 460 * @param string $customerId
 461 * @param string $cardId
 462 * @return array
 463 * 
 464 * @link https://stripe.com/docs/api/php#retrieve_card
 465 */
 466	public function retrieveCard($customerId = null, $cardId = null) {
 467		if (!$customerId) {
 468			throw new CakeException(__('Customer Id is not provided'));
 469		}
 470		
 471		if (!$cardId) {
 472			throw new CakeException(__('Card Id is not provided'));
 473		}
 474		
 475		$data = array(
 476			'customer_id' => $customerId,
 477			'card_id' => $cardId
 478		);
 479
 480		return $this->request(__FUNCTION__, $data);
 481	}
 482	
 483/**
 484 * updateCard method
 485 * Updates an existing card for the given customer
 486 * 
 487 * @param string $customerId
 488 * @param string $cardId
 489 * @param array $data
 490 * @return array
 491 * 
 492 * @link https://stripe.com/docs/api/php#update_card
 493 */
 494	public function updateCard($customerId = null, $cardId = null, $data = array()) {
 495		if (!$customerId) {
 496			throw new CakeException(__('Customer Id is not provided'));
 497		}
 498		
 499		if (!$cardId) {
 500			throw new CakeException(__('Card Id is not provided'));
 501		}
 502		
 503		if (empty($data)) {
 504			throw new CakeException(__('No data is provided to updates the card'));
 505		}
 506		
 507		$data = array(
 508			'customer_id' => $customerId,
 509			'card_id' => $cardId,
 510			'fields' => $data,
 511		);
 512
 513		return $this->request(__FUNCTION__, $data);
 514	}
 515	
 516/**
 517 * deleteCard method
 518 * Deletes an existing card for the given customer
 519 * 
 520 * @param string $customerId
 521 * @param string $cardId
 522 * @return array
 523 * 
 524 * @link https://stripe.com/docs/api/php#delete_card
 525 */
 526	public function deleteCard($customerId = null, $cardId = null) {
 527		if (!$customerId) {
 528			throw new CakeException(__('Customer Id is not provided'));
 529		}
 530		
 531		if (!$cardId) {
 532			throw new CakeException(__('Card Id is not provided'));
 533		}
 534		
 535		$data = array(
 536			'customer_id' => $customerId,
 537			'card_id' => $cardId
 538		);
 539
 540		return $this->request(__FUNCTION__, $data);
 541	}
 542
 543/**
 544 * listCards method
 545 * Returs cards for the given customer
 546 * 
 547 * @param string $customerId
 548 * @param string $cardId
 549 * @return array
 550 * 
 551 * @link https://stripe.com/docs/api/php#list_cards
 552 */
 553	public function listCards($customerId = null, $data = array()) {
 554		if (!$customerId) {
 555			throw new CakeException(__('Customer Id is not provided'));
 556		}
 557		
 558		$data = array(
 559			'customer_id' => $customerId,
 560			'options' => $data
 561		);
 562
 563		return $this->request(__FUNCTION__, $data);
 564	}
 565	
 566	
 567	
 568	
 569	
 570	
 571	
 572	
 573	
 574	
 575/**
 576 * createSubscription method
 577 * Creates a new subscription for the given customer
 578 * 
 579 * @param string $customerId
 580 * @param array $data - subscription data, token or array 
 581 * @return array
 582 * 
 583 * @link https://stripe.com/docs/api/php#create_subscription
 584 */
 585	public function createSubscription($customerId = null, $subscription = null) {
 586		if (!$customerId) {
 587			throw new CakeException(__('Customer Id is not provided'));
 588		}
 589		
 590		if (!$subscription) {
 591			throw new CakeException(__('Subscription data is not provided'));
 592		}
 593		
 594		$data = array(
 595			'customer_id' => $customerId,
 596			'subscription' => $subscription
 597		);
 598
 599		return $this->request(__FUNCTION__, $data);
 600	}
 601	
 602/**
 603 * retrieveSubscription method
 604 * Retrives an existing subscription for the given customer
 605 * 
 606 * @param string $customerId
 607 * @param string $subscriptionId
 608 * @return array
 609 * 
 610 * @link https://stripe.com/docs/api/php#retrieve_subscription
 611 */
 612	public function retrieveSubscription($customerId = null, $subscriptionId = null) {
 613		if (!$customerId) {
 614			throw new CakeException(__('Customer Id is not provided'));
 615		}
 616		
 617		if (!$subscriptionId) {
 618			throw new CakeException(__('Subscription Id is not provided'));
 619		}
 620		
 621		$data = array(
 622			'customer_id' => $customerId,
 623			'subscription_id' => $subscriptionId
 624		);
 625
 626		return $this->request(__FUNCTION__, $data);
 627	}
 628	
 629/**
 630 * updateSubscription method
 631 * Updates an existing subscription for the given customer
 632 * 
 633 * @param string $customerId
 634 * @param string $subscriptionId
 635 * @param array $data
 636 * @return array
 637 * 
 638 * @link https://stripe.com/docs/api/php#update_subscription
 639 */
 640	public function updateSubscription($customerId = null, $subscriptionId = null, $data = array()) {
 641		if (!$customerId) {
 642			throw new CakeException(__('Customer Id is not provided'));
 643		}
 644		
 645		if (!$subscriptionId) {
 646			throw new CakeException(__('Subscription Id is not provided'));
 647		}
 648		
 649		if (empty($data)) {
 650			throw new CakeException(__('No data is provided to update the subscription'));
 651		}
 652		
 653		$data = array(
 654			'customer_id' => $customerId,
 655			'subscription_id' => $subscriptionId,
 656			'fields' => $data,
 657		);
 658
 659		return $this->request(__FUNCTION__, $data);
 660	}
 661	
 662/**
 663 * cancelSubscription method
 664 * Cancels an existing subscription for the given customer
 665 * 
 666 * @param string $customerId
 667 * @param string $subscriptionId
 668 * @return array
 669 * 
 670 * @link https://stripe.com/docs/api/php#cancel_subscription
 671 */
 672	public function cancelSubscription($customerId = null, $subscriptionId = null) {
 673		if (!$customerId) {
 674			throw new CakeException(__('Customer Id is not provided'));
 675		}
 676		
 677		if (!$subscriptionId) {
 678			throw new CakeException(__('Subscription Id is not provided'));
 679		}
 680		
 681		$data = array(
 682			'customer_id' => $customerId,
 683			'subscription_id' => $subscriptionId
 684		);
 685
 686		return $this->request(__FUNCTION__, $data);
 687	}
 688
 689/**
 690 * listSubscriptions method
 691 * Returs subscriptions for the given customer
 692 * 
 693 * @param string $customerId
 694 * @param string $subscriptionId
 695 * @return array
 696 * 
 697 * @link https://stripe.com/docs/api/php#list_subscriptions
 698 */
 699	public function listSubscriptions($customerId = null, $data = array()) {
 700		if (!$customerId) {
 701			throw new CakeException(__('Customer Id is not provided'));
 702		}
 703		
 704		$data = array(
 705			'customer_id' => $customerId,
 706			'options' => $data
 707		);
 708
 709		return $this->request(__FUNCTION__, $data);
 710	}
 711
 712	
 713	
 714	
 715	
 716	
 717	
 718	
 719	
 720	
 721	
 722
 723/**
 724 * createPlan method
 725 * Creates a new subscription plan
 726 * 
 727 * @param array $data
 728 * @return array
 729 * 
 730 * @link https://stripe.com/docs/api/php#create_plan
 731 */
 732	public function createPlan($data = array()) {
 733		if (empty($data) || !is_array($data)) {
 734			throw new CakeException(__('Data is empty or is not an array'));
 735		}
 736		
 737		// set default currency
 738		if (!isset($data['currency'])) {
 739			$data['currency'] = $this->currency;
 740		}
 741		
 742		return $this->request(__FUNCTION__, $data);
 743	}
 744	
 745
 746/**
 747 * retrievePlan method
 748 * Retrieves the existing subscription plan
 749 * 
 750 * @param string $planId
 751 * @return array
 752 * 
 753 * @link https://stripe.com/docs/api/php#retrieve_plan
 754 */
 755	public function retrievePlan($planId = null) {
 756		if (!$planId) {
 757			throw new CakeException(__('Plan Id is required'));
 758		}
 759
 760		$data = array(
 761			'plan_id' => $planId	
 762		);
 763		
 764		return $this->request(__FUNCTION__, $data);
 765	}
 766
 767/**
 768 * updatePlan method
 769 * Updates the existing subscription plan
 770 * 
 771 * @param string $planId
 772 * @return array
 773 * 
 774 * @link https://stripe.com/docs/api/php#update_plan
 775 */
 776	public function updatePlan($planId = null, $data = array()) {
 777		if (!$planId) {
 778			throw new CakeException(__('Plan Id is required'));
 779		}
 780		
 781		if (empty($data)) {
 782			throw new CakeException(__('No data is provided to updates the plan'));
 783		}
 784
 785		$data = array(
 786			'plan_id' => $planId,
 787			'fields' => $data
 788		);
 789		
 790		return $this->request(__FUNCTION__, $data);
 791	}
 792	
 793/**
 794 * deletePlan method
 795 * Deletes the existing subscription plan
 796 * 
 797 * @param string $planId
 798 * @return array
 799 * 
 800 * @link https://stripe.com/docs/api/php#delete_plan
 801 */
 802	public function deletePlan($planId = null) {
 803		if (!$planId) {
 804			throw new CakeException(__('Plan Id is required'));
 805		}
 806		
 807		$data = array(
 808			'plan_id' => $planId,
 809		);
 810		
 811		return $this->request(__FUNCTION__, $data);
 812	}
 813	
 814/**
 815 * listPlans method
 816 * Returns all the plans
 817 * 
 818 * @param array $data
 819 * @return array
 820 * 
 821 * @link https://stripe.com/docs/api/php#list_plans
 822 */
 823	public function listPlans($data = array()) {
 824		$data = array(
 825			'options' => $data,
 826		);
 827		
 828		return $this->request(__FUNCTION__, $data);
 829	}
 830	
 831	
 832	
 833	
 834	
 835	
 836/**
 837 * createCoupon method
 838 * Creates a new coupon
 839 * 
 840 * @param array $data
 841 * @return array
 842 * 
 843 * @link https://stripe.com/docs/api/php#create_coupon
 844 */
 845	public function createCoupon($data = array()) {
 846		if (empty($data) || !is_array($data)) {
 847			throw new CakeException(__('Data is empty or is not an array'));
 848		}
 849		
 850		// set default currency
 851		if (!isset($data['currency'])) {
 852			$data['currency'] = $this->currency;
 853		}
 854		
 855		return $this->request(__FUNCTION__, $data);
 856	}
 857	
 858
 859/**
 860 * retrieveCoupon method
 861 * Retrieves the existing coupon
 862 * 
 863 * @param string $couponId
 864 * @return array
 865 * 
 866 * @link https://stripe.com/docs/api/php#retrieve_coupon
 867 */
 868	public function retrieveCoupon($couponId = null) {
 869		if (!$couponId) {
 870			throw new CakeException(__('Coupon Id is required'));
 871		}
 872
 873		$data = array(
 874			'coupon_id' => $couponId	
 875		);
 876		
 877		return $this->request(__FUNCTION__, $data);
 878	}
 879	
 880/**
 881 * deleteCoupon method
 882 * Deletes the existing coupon
 883 * 
 884 * @param string $couponId
 885 * @return array
 886 * 
 887 * @link https://stripe.com/docs/api/php#delete_coupon
 888 */
 889	public function deleteCoupon($couponId = null) {
 890		if (!$couponId) {
 891			throw new CakeException(__('Coupon Id is required'));
 892		}
 893		
 894		$data = array(
 895			'coupon_id' => $couponId,
 896		);
 897		
 898		return $this->request(__FUNCTION__, $data);
 899	}
 900	
 901/**
 902 * listCoupons method
 903 * Returns all the plans
 904 * 
 905 * @param array $data
 906 * @return array
 907 * 
 908 * @link https://stripe.com/docs/api/php#list_coupons
 909 */
 910	public function listCoupons($data = array()) {
 911		$data = array(
 912			'options' => $data,
 913		);
 914		
 915		return $this->request(__FUNCTION__, $data);
 916	}
 917	
 918	
 919/**
 920 * deleteCustomerDiscount method
 921 * Removes the currently applied discount on a customer
 922 * 
 923 * @param string $customerId
 924 * @return array
 925 * 
 926 * @link https://stripe.com/docs/api/php#delete_discount
 927 */
 928	public function deleteCustomerDiscount($customerId = null) {
 929		if (!$customerId) {
 930			throw new CakeException(__('Customer Id is not provided'));
 931		}
 932		
 933		$data = array(
 934			'customer_id' => $customerId	
 935		);
 936		
 937		return $this->request(__FUNCTION__, $data);
 938	}
 939	
 940/**
 941 * deleteSubscriptionDiscount method
 942 * Removes the currently applied discount on a subscription.
 943 * 
 944 * @param string $customerId
 945 * @param string $subscriptionId
 946 * @return array
 947 * 
 948 * @link https://stripe.com/docs/api#delete_subscription_discount
 949 */
 950	public function deleteSubscriptionDiscount($customerId = null, $subscriptionId = null) {
 951		if (!$customerId) {
 952			throw new CakeException(__('Customer Id is not provided'));
 953		}
 954		
 955		if (!$subscriptionId) {
 956			throw new CakeException(__('Subscription Id is not provided'));
 957		}
 958		
 959		$data = array(
 960			'customer_id' => $customerId,
 961			'subscription_id' => $subscriptionId
 962		);
 963
 964		return $this->request(__FUNCTION__, $data);
 965	}
 966	
 967	
 968	
 969	
 970	
 971	
 972/**
 973 * retrieveInvoice method
 974 * Retrives an existing invoice
 975 *
 976 * @param string $invoiceId
 977 * @return array
 978 *
 979 * @link https://stripe.com/docs/api/php#retrieve_invoice
 980 */
 981	public function retrieveInvoice($invoiceId = null) {
 982		if (!$invoiceId) {
 983			throw new CakeException(__('Invoice Id is not provided'));
 984		}
 985	
 986		$data = array(
 987				'invoice_id' => $invoiceId
 988		);
 989	
 990		return $this->request(__FUNCTION__, $data);
 991	}
 992	
 993/**
 994 * retrieveInvoiceLine method
 995 * Retrives an existing invoice's line
 996 *
 997 * @param string $invoiceId
 998 * @param array $data
 999 * @return array
1000 *
1001 * @link https://stripe.com/docs/api#invoice_lines
1002 */
1003	public function retrieveInvoiceLine($invoiceId = null, $data = array()) {
1004		if (!$invoiceId) {
1005			throw new CakeException(__('Invoice Id is not provided'));
1006		}
1007	
1008		$data = array(
1009				'invoice_id' => $invoiceId,
1010				'options' => $data
1011		);
1012	
1013		return $this->request(__FUNCTION__, $data);
1014	}
1015	
1016/**
1017 * createInvoice method
1018 * Creates a new invoice
1019 *
1020 * @param string $customerId
1021 * @param array $data
1022 * @return array
1023 *
1024 * @link https://stripe.com/docs/api#create_invoice
1025 */
1026	public function createInvoice($customerId = null, $data = array()) {
1027		if (!$customerId) {
1028			throw new CakeException(__('Customer Id is not provided'));
1029		}
1030	
1031		$data['customer'] = $customerId;
1032	
1033		return $this->request(__FUNCTION__, $data);
1034	}
1035	
1036/**
1037 * payInvoice method
1038 * Pay invoice
1039 *
1040 * @param string $invoiceId
1041 * @return array
1042 *
1043 * @link https://stripe.com/docs/api#pay_invoice
1044 */
1045	public function payInvoice($invoiceId = null) {
1046		if (!$invoiceId) {
1047			throw new CakeException(__('Invoice Id is not provided'));
1048		}
1049	
1050		$data['invoice_id'] = $invoiceId;
1051	
1052		return $this->request(__FUNCTION__, $data);
1053	}
1054	
1055/**
1056 * updateInvoice method
1057 * Update Invoice
1058 *
1059 * @param string $invoiceId
1060 * @return array
1061 *
1062 * @link https://stripe.com/docs/api#update_invoice
1063 */
1064	public function updateInvoice($invoiceId = null, $data = array()) {
1065		if (!$invoiceId) {
1066			throw new CakeException(__('Invoice Id is not provided'));
1067		}
1068	
1069		if (empty($data)) {
1070			throw new CakeException(__('Data is empty for updating the invoice'));
1071		}
1072	
1073		$data = array(
1074			'invoice_id' => $invoiceId,
1075			'fields' => $data
1076		);
1077
1078		return $this->request(__FUNCTION__, $data);
1079	}
1080	
1081/**
1082 * listInvoices method
1083 * List all invoices, if customer id is provided, only corresponding invoices will be returned 
1084 *
1085 * @param string $customerId - not required
1086 * @param array $data
1087 * @return array
1088 *
1089 * @link https://stripe.com/docs/api#list_customer_invoices
1090 */
1091	public function listInvoices($customerId = null, $data = array()) {
1092		$data['customer'] = $customerId;
1093
1094		return $this->request(__FUNCTION__, $data);
1095	}
1096	
1097	
1098	
1099/**
1100 * retrieveUpcomingInvoice method
1101 *  
1102 * @param string $customerId - not required
1103 * @param string $subscriptionId
1104 * @return array
1105 *
1106 * @link https://stripe.com/docs/api#retrieve_customer_invoice
1107 */
1108	public function retrieveUpcomingInvoice($customerId = null, $subscriptionId = null) {
1109		if (!$customerId) {
1110			throw new CakeException(__('Customer Id is not provided'));
1111		}
1112		
1113		$data = array(
1114			'customer' => $customerId,	
1115			'subscription' => $subscriptionId
1116		);
1117
1118		return $this->request(__FUNCTION__, $data);
1119	}
1120	
1121	
1122	
1123	
1124	
1125	
1126	
1127/**
1128 * createInvoiceItem method
1129 * Adds an arbitrary charge or credit to the customer's upcoming invoice.
1130 *
1131 * @param string $customerId
1132 * @param array $data
1133 * @return array
1134 *
1135 * @link https://stripe.com/docs/api#create_invoiceitem
1136 */
1137	public function createInvoiceItem($customerId = null, $data = array()) {
1138		if (!$customerId) {
1139			throw new CakeException(__('Customer Id is not provided'));
1140		}
1141	
1142		$data['customer'] = $customerId;
1143		if (!isset($data['currency'])) {
1144			$data['currency'] = $this->currency;
1145		}
1146		
1147		return $this->request(__FUNCTION__, $data);
1148	}	
1149	
1150/**
1151 * retrieveInvoiceItem method
1152 * Retrieves the invoice item with the given ID.
1153 *
1154 * @param string $invoiceItemId
1155 * @return array
1156 *
1157 * @link https://stripe.com/docs/api#retrieve_invoiceitem
1158 */
1159	public function retrieveInvoiceItem($invoiceItemId = null) {
1160		if (!$invoiceItemId) {
1161			throw new CakeException(__('Invoice Item Id is not provided'));
1162		}
1163	
1164		$data = array(
1165			'invoice_item_id' => $invoiceItemId		
1166		);
1167		
1168		return $this->request(__FUNCTION__, $data);
1169	}
1170		
1171/**
1172 * updateInvoiceItem method
1173 * Updates the amount or description of an invoice item on an upcoming invoice. 
1174 * Updating an invoice item is only possible before the invoice it's attached to is closed.
1175 *
1176 * @param string $invoiceId
1177 * @return array
1178 *
1179 * @link https://stripe.com/docs/api#update_invoiceitem
1180 */
1181	public function updateInvoiceItem($invoiceItemId = null, $data = array()) {
1182		if (!$invoiceItemId) {
1183			throw new CakeException(__('Invoice Item Id is not provided'));
1184		}
1185	
1186		$data = array(
1187			'invoice_item_id' => $invoiceItemId,
1188			'fields' => $data		
1189		);
1190		
1191		return $this->request(__FUNCTION__, $data);
1192	}	
1193	
1194/**
1195 * deleteInvoiceItem method
1196 * Removes an invoice item from the upcoming invoice. Removing an invoice item is only possible before the invoice it's attached to is closed.
1197 *
1198 * @param string $invoiceId
1199 * @return array
1200 *
1201 * @link https://stripe.com/docs/api#delete_invoiceitem
1202 */
1203	public function deleteInvoiceItem($invoiceItemId = null) {
1204		if (!$invoiceItemId) {
1205			throw new CakeException(__('Invoice Item Id is not provided'));
1206		}
1207	
1208		$data = array(
1209			'invoice_item_id' => $invoiceItemId		
1210		);
1211		
1212		return $this->request(__FUNCTION__, $data);
1213	}	
1214	
1215/**
1216 * listInvoiceItems method
1217 * Returns a list of your invoice items. Invoice Items are returned sorted by creation date, with the most recently created invoice items appearing first.
1218 *
1219 * @param array $data
1220 * @return array
1221 *
1222 * @link https://stripe.com/docs/api#list_invoiceitems
1223 */
1224	public function listInvoiceItems($data = array()) {
1225		$data = array(
1226			'options' => $data		
1227		);
1228		
1229		return $this->request(__FUNCTION__, $data);
1230	}	
1231	
1232/**
1233 * updateDispute method
1234 *
1235 * @param string $chargeId
1236 * @param array $data
1237 * @return array
1238 * 
1239 * @link https://stripe.com/docs/api#update_dispute
1240 */
1241	public function updateDispute($chargeId = null, $data = array()) {
1242		if (!$chargeId) {
1243			throw new CakeException(__('Charge Id is not provided'));
1244		}
1245		
1246		if (empty($data)) {
1247			throw new CakeException(__('Data is empty for updating the dispute'));
1248		}
1249		
1250		$data = array(
1251			'charge_id' => $chargeId,
1252			'dispute' => $data
1253		);
1254
1255		return $this->request(__FUNCTION__, $data);
1256	}	
1257	
1258/**
1259 * closeDispute method
1260 * Closes the dispute, which changes the status from "under_review" to "lost"
1261 *
1262 * @param string $chargeId
1263 * @return array
1264 * 
1265 * @link https://stripe.com/docs/api#close_dispute
1266 */
1267	public function closeDispute($chargeId = null) {
1268		if (!$chargeId) {
1269			throw new CakeException(__('Charge Id is not provided'));
1270		}
1271
1272		$data = array(
1273			'charge_id' => $chargeId,
1274		);
1275
1276		return $this->request(__FUNCTION__, $data);
1277	}	
1278	
1279	
1280	
1281	
1282	
1283	
1284	
1285	
1286	
1287	
1288	
1289
1290/**
1291 * createTransfer method
1292 * Used to send funds from your Stripe account to a third-party recipient or to your own bank account
1293 *
1294 * @param string $recipientId
1295 * @param array $data
1296 * @return array
1297 *
1298 * @link https://stripe.com/docs/api/php#create_transfer
1299 */
1300	public function createTransfer($recipientId = null, $data = array()) {
1301		if (!$recipientId) {
1302			throw new CakeException(__('Recipient Id is not provided'));
1303		}
1304	
1305		if (empty($data)) {
1306			throw new CakeException(__('Transfer data is not provided'));
1307		}
1308	
1309		$data['recipient'] = $recipientId;
1310
1311		if (!isset($data['currency'])) {
1312			$data['currency'] = $this->currency;
1313		}
1314	
1315		return $this->request(__FUNCTION__, $data);
1316	}
1317	
1318/**
1319 * retrieveTransfer method
1320 * Retrieves the details of an existing transfer
1321 *
1322 * @param string $recipientId
1323 * @param string $transferId
1324 * @return array
1325 *
1326 * @link https://stripe.com/docs/api/php#retrieve_transfer
1327 */
1328	public function retrieveTransfer($transferId = null) {
1329		if (!$transferId) {
1330			throw new CakeException(__('Transfer Id is not provided'));
1331		}
1332	
1333		$data = array(
1334			'transfer_id' => $transferId
1335		);
1336	
1337		return $this->request(__FUNCTION__, $data);
1338	}
1339	
1340/**
1341 * updateTransfer method
1342 * Updates an existing transfer
1343 *
1344 * @param string $recipientId
1345 * @param string $transferId
1346 * @param array $data
1347 * @return array
1348 *
1349 * @link https://stripe.com/docs/api/php#update_transfer
1350 */
1351	public function updateTransfer($transferId = null, $data = array()) {
1352		if (!$transferId) {
1353			throw new CakeException(__('Transfer Id is not provided'));
1354		}
1355	
1356		if (empty($data)) {
1357			throw new CakeException(__('No data is provided to update the transfer'));
1358		}
1359	
1360		$data = array(
1361			'transfer_id' => $transferId,
1362			'fields' => $data
1363		);
1364	
1365		return $this->request(__FUNCTION__, $data);
1366	}
1367	
1368/**
1369 * cancelTransfer method
1370 * Cancels an existing transfer
1371 *
1372 * @param string $recipientId
1373 * @param string $transferId
1374 * @return array
1375 *
1376 * @link https://stripe.com/docs/api/php#cancel_transfer
1377 */
1378	public function cancelTransfer($transferId = null) {
1379		if (!$transferId) {
1380			throw new CakeException(__('Transfer Id is not provided'));
1381		}
1382	
1383		$data = array(
1384			'transfer_id' => $transferId
1385		);
1386	
1387		return $this->request(__FUNCTION__, $data);
1388	}
1389	
1390/**
1391 * listTransfers method
1392 * Returs transfers
1393 *
1394 * @param array $data
1395 * @return array
1396 *
1397 * @link https://stripe.com/docs/api/php#list_transfers
1398 */
1399	public function listTransfers($data = array()) {
1400		$data = array(
1401			'options' => $data
1402		);
1403	
1404		return $this->request(__FUNCTION__, $data);
1405	}
1406	
1407
1408	
1409	
1410/**
1411 * createCardToken method
1412 * Creates a single use token that wraps the details of a credit card.
1413 *
1414 * @param array $data - card data
1415 * @param array $customerId [optional]
1416 * @return array
1417 *
1418 * @link https://stripe.com/docs/api#create_card_token
1419 */
1420	public function createCardToken($card = null, $customerId = null) {
1421		if (!$card) {
1422			throw new CakeException(__('Card data is not provided'));
1423		}
1424		
1425		$data = array(
1426			'customer' => $customerId,
1427			'card' => $card
1428		);
1429	
1430		return $this->request(__FUNCTION__, $data);
1431	}	
1432	
1433/**
1434 * createBankAccountToken method
1435 * Creates a single use token that wraps the details of a bank account.
1436 *
1437 * @param array $data - card data	
1438 * @return array
1439 *
1440 * @link https://stripe.com/docs/api#create_bank_account_token
1441 */
1442	public function createBankAccountToken($bankAccount = null) {
1443		if (!$bankAccount) {
1444			throw new CakeException(__('Bank Account is not provided'));
1445		}
1446		
1447		$data = array(
1448			'bank_account' => $bankAccount
1449		);
1450	
1451		return $this->request(__FUNCTION__, $data);
1452	}	
1453	
1454/**
1455 * retrieveToken method
1456 * Retrieves the token with the given ID.
1457 *
1458 * @param string $tokenId
1459 * @return array
1460 *
1461 * @link https://stripe.com/docs/api#retrieve_token
1462 */
1463	public function retrieveToken($tokenId = null) {
1464		if (!$tokenId) {
1465			throw new CakeException(__('Token Id is not provided'));
1466		}
1467
1468		$data = array(
1469			'token_id' => $tokenId
1470		);
1471	
1472		return $this->request(__FUNCTION__, $data);
1473	}
1474	
1475	
1476	
1477	
1478	
1479	
1480	
1481	
1482	
1483	
1484	
1485	
1486	
1487	
1488	
1489	
1490/**
1491 * createRecipient method
1492 * Creates a new recipient
1493 *
1494 * @param array	$data - according to recipient object
1495 * @return array
1496 *
1497 * @link  https://stripe.com/docs/api#create_recipient
1498 */
1499	public function createRecipient($data) {
1500		if (empty($data) || !is_array($data)) {
1501			throw new CakeException(__('Data is empty or is not an array'));
1502		}
1503	
1504		return $this->request(__FUNCTION__, $data);
1505	}
1506	
1507	
1508/**
1509 * retrieveRecipient method
1510 * Retrives the recipient information
1511 *
1512 * @param string $recipientId
1513 * @return array
1514 *
1515 * @link https://stripe.com/docs/api/php#retrieve_recipient
1516 */
1517	public function retrieveRecipient($recipientId = null) {
1518		if (!$recipientId) {
1519			throw new CakeException(__('Recipient Id is not provided'));
1520		}
1521	
1522		$data = array(
1523			'recipient_id' => $recipientId
1524		);
1525	
1526		return $this->request(__FUNCTION__, $data);
1527	}
1528	
1529	
1530/**
1531 * updateRecipient method
1532 * Updates the recipient info
1533 *
1534 * @param string $recipientId
1535 * @param array $fields - fields to be updated
1536 * @return array
1537 *
1538 * @link https://stripe.com/docs/api/php#update_recipient
1539 */
1540	public function updateRecipient($recipientId = null, $fields = array()) {
1541		if (!$recipientId) {
1542			throw new CakeException(__('Recipient Id is not provided'));
1543		}
1544	
1545		if (empty($fields)) {
1546			throw new CakeException(__('Update fields are empty'));
1547		}
1548	
1549		$data = array(
1550			'recipient_id' => $recipientId,
1551			'fields' => $fields
1552		);
1553	
1554		return $this->request(__FUNCTION__, $data);
1555	}
1556	
1557	
1558/**
1559 * deleteRecipient method
1560 * Deletes the recipient
1561 *
1562 * @param string $recipientId
1563 * @return array
1564 *
1565 * @link https://stripe.com/docs/api/php#delete_recipient
1566 */
1567	public function deleteRecipient($recipientId = null) {
1568		if (!$recipientId) {
1569			throw new CakeException(__('Recipient Id is not provided'));
1570		}
1571	
1572		$data = array(
1573				'recipient_id' => $recipientId
1574		);
1575	
1576		return $this->request(__FUNCTION__, $data);
1577	}
1578	
1579/**
1580 * listRecipients method
1581 * Returns array with recipients
1582 *
1583 * @param array $data
1584 * @return array
1585 *
1586 * @link https://stripe.com/docs/api/php#list_recipients
1587 */
1588	public function listRecipients($data = array()) {
1589		$data = array(
1590			'options' => $data
1591		);
1592		return $this->request(__FUNCTION__, $data);
1593	}
1594	
1595	
1596	
1597/**
1598 * retrieveApplicationFee method
1599 * Retrieves the details of an application fee that your account has collected
1600 *
1601 * @param string $applicationFeeId
1602 * @return array
1603 *
1604 * @link https://stripe.com/docs/api#retrieve_application_fee
1605 */
1606	public function retrieveApplicationFee($applicationFeeId = null) {
1607		if (!$applicationFeeId) {
1608			throw new CakeException(__('Application Fee Id is not provided'));
1609		}
1610		
1611		$data = array(
1612			'application_fee_id' => $applicationFeeId
1613		);
1614
1615		return $this->request(__FUNCTION__, $data);
1616	}
1617	
1618	
1619/**
1620 * refundApplicationFee method
1621 * Returns a list of application fees you've previously collected
1622 *
1623 * @param string $applicationFeeId
1624 * @param int $amount
1625 * @return array
1626 *
1627 * @link https://stripe.com/docs/api#refund_application_fee
1628 */
1629	public function refundApplicationFee($applicationFeeId = null) {
1630		if (!$applicationFeeId) {
1631			throw new CakeException(__('Application Fee Id is not provided'));
1632		}
1633		
1634		$data['application_fee_id'] = $applicationFeeId;
1635		
1636		return $this->request(__FUNCTION__, $data);
1637	}	
1638
1639/**
1640 * listApplicationFees method
1641 * Returns a list of application fees you've previously collected
1642 *
1643 * @param array $data
1644 * @return array
1645 *
1646 * @link https://stripe.com/docs/api#list_application_fees
1647 */
1648	public function listApplicationFees($data = array()) {
1649		$data = array(
1650			'options' => $data
1651		);
1652		return $this->request(__FUNCTION__, $data);
1653	}	
1654
1655	
1656	
1657	
1658/**
1659 * retrieveAccount method
1660 * Retrieves the details of the account, based on the API key that was used to make the request.
1661 *
1662 * @return array
1663 *
1664 * @link https://stripe.com/docs/api#retrieve_account
1665 */
1666	public function retrieveAccount() {
1667		$data = array();
1668		return $this->request(__FUNCTION__, $data);
1669	}	
1670	
1671	
1672/**
1673 * retrieveBalance method
1674 * Retrieves the current account balance, based on the API key that was used to make the request.
1675 *
1676 * @return array
1677 *
1678 * @link https://stripe.com/docs/api#retrieve_balance
1679 */
1680	public function retrieveBalance() {
1681		$data = array();
1682		return $this->request(__FUNCTION__, $data);
1683	}	
1684	
1685	
1686/**
1687 * retrieveBalanceTransaction method
1688 * Retrieves the balance transaction with the given ID.
1689 *
1690 * @param string $transactionId
1691 * @return array
1692 *
1693 * @link https://stripe.com/docs/api#retrieve_balance_transaction
1694 */
1695	public function retrieveBalanceTransaction($transactionId = null) {
1696		if (!$transactionId) {
1697			throw new CakeException(__('Transaction Id is not provided'));
1698		}
1699		
1700		$data = array(
1701			'transaction_id' => $transactionId
1702		);
1703
1704		return $this->request(__FUNCTION__, $data);
1705	}	
1706	
1707	
1708/**
1709 * listBalanceHistory method
1710 * Returns a list of transactions that have contributed to the Stripe account balance (includes charges, refunds, transfers, and so on).
1711 *
1712 * @param array $data
1713 * @return array
1714 *
1715 * @link https://stripe.com/docs/api#balance_history
1716 */
1717	public function listBalanceHistory($data = array()) {
1718		$data = array(
1719			'options' => $data
1720		);
1721
1722		return $this->request(__FUNCTION__, $data);
1723	}	
1724	
1725	
1726	
1727	
1728
1729
1730
1731/**
1732 * retrievePlan method
1733 * Retrieves the details of an event
1734 *
1735 * @param string $eventId
1736 * @return array
1737 *
1738 * @link https://stripe.com/docs/api#retrieve_event
1739 */
1740	public function retrieveEvent($eventId = null) {
1741		if (!$eventId) {
1742			throw new CakeException(__('Event Id is required'));
1743		}
1744	
1745		$data = array(
1746			'event_id' => $eventId
1747		);
1748	
1749		return $this->request(__FUNCTION__, $data);
1750	}	
1751	
1752/**
1753 * listEvents method
1754 * List events, going back up to 30 days.
1755 *
1756 * @param array $data
1757 * @return array
1758 *
1759 * @link https://stripe.com/docs/api#list_events
1760 */
1761	public function listEvents($data = array()) {
1762		$data = array(
1763			'options' => $data,
1764		);
1765	
1766		return $this->request(__FUNCTION__, $data);
1767	}
1768	
1769	
1770	
1771	
1772	
1773	
1774	
1775	
1776	
1777	
1778	
1779	
1780	
1781	
1782	
1783	
1784	
1785	
1786	
1787	
1788	
1789	
1790	
1791	
1792	
1793	
1794/**
1795 * request method
1796 * 
1797 * @param string $method
1798 * @param array $data
1799 *  	
1800 * @return array - containing 'status', 'message' and 'response' keys
1801 * 					if response was successful, keys will be 'success', 'Success' and the stripe response as associative array respectively,
1802 *   				if request failed, keys will be 'error', the card error message if it was card_error, boolen false otherwise, and 
1803 *   								error data as an array respectively
1804 */	
1805	private function request($method = null, $data = null) {
1806		if (!$method) {
1807			throw new CakeException(__('Request method is missing'));
1808		}
1809		if (is_null($data)) {
1810			throw new CakeException(__('Request Data is not provided'));
1811		}
1812		
1813		Stripe::setApiKey($this->key);
1814		
1815		$success = null;
1816		$error = null;
1817		$message = false;
1818		$log = null;
1819		
1820
1821		try {
1822			switch ($method) {
1823				
1824			/**
1825			 * 	
1826			 * 		CHARGES	
1827			 *  
1828			 */
1829			case 'charge':
1830				$success = $this->fetch(Stripe_Charge::create($data));
1831				break;
1832			case 'retrieveCharge':
1833				$success = $this->fetch(Stripe_Charge::retrieve($data['charge_id']));
1834				
1835				if (!empty($success['refunds'])) {
1836					foreach ($success['refunds'] as &$refund) {
1837						$refund = $this->fetch($refund);
1838					}
1839				}
1840
1841				break;
1842			case 'updateCharge':
1843				$charge = Stripe_Charge::retrieve($data['charge_id']);
1844			
1845				foreach ($data['fields'] as $field => $value) {
1846					$charge->$field = $value;
1847				}
1848
1849				$success = $this->fetch($charge->save());
1850				break;
1851			case 'refundCharge':
1852				$charge = Stripe_Charge::retrieve($data['charge_id']);
1853				
1854				unset($data['charge_id']);
1855				$success = $this->fetch($charge->refund($data));
1856
1857				foreach ($success['refunds'] as &$refund) {
1858					$refund = $this->fetch($refund);
1859				}
1860				break;
1861			case 'captureCharge':
1862				$charge = Stripe_Charge::retrieve($data['charge_id']);
1863				
1864				unset($data['charge_id']);
1865				$success = $this->fetch($charge->capture($data));
1866
1867				foreach ($success['refunds'] as &$refund) {
1868					$refund = $this->fetch($refund);
1869				}
1870				break;
1871			case 'listCharges':
1872				$charges = Stripe_Charge::all();
1873				$success = $this->fetch($charges);
1874			
1875				foreach ($success['data'] as &$charge) {
1876					$charge = $this->fetch($charge);
1877					
1878					if (isset($charge['refunds']) && !empty($charge['refunds'])) {
1879						foreach ($charge['refunds'] as &$refund) {
1880							$refund = $this->fetch($refund);
1881						}
1882						unset($refund);
1883					}
1884				}
1885					
1886				break;
1887				
1888				
1889				
1890				
1891				
1892				
1893
1894				/**
1895				 * 		CUSTOMERS
1896				 */
1897				case 'createCustomer':
1898					$customer = Stripe_Customer::create($data);
1899					$success = $this->fetch($customer);
1900					
1901					if (!empty($success['cards']['data'])) {
1902						foreach ($success['cards']['data'] as &$card) {
1903							$card = $this->fetch($card);
1904						}
1905						unset($card);
1906					}
1907						
1908					if (!empty($success['subscriptions']['data'])) {
1909						foreach ($success['subscriptions']['data'] as &$subscription) {
1910							$subscription = $this->fetch($subscription);
1911						}
1912						unset($subscription);
1913					}
1914					
1915					break;
1916				case 'retrieveCustomer':
1917					$customer = Stripe_Customer::retrieve($data['customer_id']);
1918					$success = $this->fetch($customer);
1919					
1920					if (!empty($success['cards']['data'])) {
1921						foreach ($success['cards']['data'] as &$card) {
1922							$card = $this->fetch($card);
1923						}
1924						unset($card);
1925					}
1926					
1927					if (!empty($success['subscriptions']['data'])) {
1928						foreach ($success['subscriptions']['data'] as &$subscription) {
1929							$subscription = $this->fetch($subscription);
1930						}
1931						unset($subscription);
1932					}
1933
1934					break;
1935				case 'updateCustomer':
1936					$cu = Stripe_Customer::retrieve($data['customer_id']);
1937					
1938					foreach ($data['fields'] as $field => $value) {
1939						$cu->$field = $value;
1940					}
1941					
1942					$success = $this->fetch($cu->save());
1943					
1944					if (!empty($success['cards']['data'])) {
1945						foreach ($success['cards']['data'] as &$card) {
1946							$card = $this->fetch($card);
1947						}
1948						unset($card);
1949					}
1950					
1951					if (!empty($success['subscriptions']['data'])) {
1952						foreach ($success['subscriptions']['data'] as &$subscription) {
1953							$subscription = $this->fetch($subscription);
1954						}
1955						unset($subscription);
1956					}
1957					
1958					break;
1959				case 'deleteCustomer':
1960					$cu = Stripe_Customer::retrieve($data['customer_id']);
1961					$success = $this->fetch($cu->delete());
1962					
1963					break;
1964				case 'listCustomers':
1965					$customers = Stripe_Customer::all($data['options']);
1966					$success = $this->fetch($customers);
1967					
1968					foreach ($success['data'] as &$customer) {
1969						$customer = $this->fetch($customer);
1970						
1971						if (!empty($customer['cards']['data'])) {
1972							foreach ($customer['cards']['data'] as &$card) {
1973								$card = $this->fetch($card);
1974							}
1975							unset($card);
1976						}
1977
1978						if (!empty($customer['subscriptions']['data'])) {
1979							foreach ($customer['subscriptions']['data'] as &$subscription) {
1980								$subscription = $this->fetch($subscription);
1981							}
1982							unset($subscription);
1983						}
1984					}
1985					
1986					break;
1987					
1988					
1989				/**
1990				 * 		CARDS
1991				 *  	
1992				 */	
1993				case 'createCard':
1994					$cu = Stripe_Customer::retrieve($data['customer_id']);
1995
1996					// unset customer_id to prevent unknown parameter stripe error
1997					unset($data['customer_id']);
1998					$card = $cu->cards->create($data);					
1999
2000					$success = $this->fetch($card);
2001					break;
2002				case 'retrieveCard':
2003					$cu = Stripe_Customer::retrieve($data['customer_id']);
2004					$card = $cu->cards->retrieve($data['card_id']);
2005
2006					$success = $this->fetch($card);
2007					
2008					break;
2009				case 'updateCard':	
2010					$cu = Stripe_Customer::retrieve($data['customer_id']);
2011					$cuCard = $cu->cards->retrieve($data['card_id']);
2012
2013					foreach ($data['fields'] as $field => $value) {
2014						$cuCard->$field = $value;
2015					}
2016					
2017					$card = $cuCard->save();					
2018					
2019					$success = $this->fetch($card);
2020					break;
2021				case 'deleteCard':
2022					$cu = Stripe_Customer::retrieve($data['customer_id']);
2023					$card = $cu->cards->retrieve($data['card_id'])->delete();
2024					
2025					$success = $this->fetch($card);
2026					break;
2027				case 'listCards':
2028					$cu = Stripe_Customer::retrieve($data['customer_id']);
2029					$cards = $cu->cards->all($data['options']);
2030					$success = $this->fetch($cards); 
2031
2032					foreach ($success['data'] as &$card) {
2033						$card = $this->fetch($card);
2034					}
2035					
2036					break;
2037
2038					
2039
2040				/**
2041				 * 		SUBSCRIPTIONS
2042				 *  	
2043				 */	
2044				case 'createSubscription':
2045					$cu = Stripe_Customer::retrieve($data['customer_id']);
2046					
2047					// unset customer_id to prevent unknown parameter stripe error
2048					unset($data['customer_id']);
2049					$subscription = $cu->subscriptions->create($data['subscription']);					
2050
2051					$success = $this->fetch($subscription);
2052					break;
2053				case 'retrieveSubscription':
2054					$cu = Stripe_Customer::retrieve($data['customer_id']);
2055					$subscription = $cu->subscriptions->retrieve($data['subscription_id']);
2056
2057					$success = $this->fetch($subscription);
2058					break;
2059				case 'updateSubscription':
2060					$cu = Stripe_Customer::retrieve($data['customer_id']);
2061					$cuSubscription = $cu->subscriptions->retrieve($data['subscription_id']);
2062
2063					foreach ($data['fields'] as $field => $value) {
2064						$cuSubscription->$field = $value;
2065					}
2066					
2067					$subscription = $cuSubscription->save();					
2068					
2069					$success = $this->fetch($subscription);
2070					break;
2071				case 'cancelSubscription':
2072					$cu = Stripe_Customer::retrieve($data['customer_id']);
2073					$subscription = $cu->subscriptions->retrieve($data['subscription_id'])->cancel();
2074					
2075					$success = $this->fetch($subscription);
2076					break;
2077				case 'listSubscriptions':
2078					$cu = Stripe_Customer::retrieve($data['customer_id']);
2079					$subscriptions = $cu->subscriptions->all($data['options']);
2080					$success = $this->fetch($subscriptions); 
2081
2082					foreach ($success['data'] as &$subscription) {
2083						$subscription = $this->fetch($subscription);
2084					}
2085					
2086					break;
2087					
2088					
2089					
2090				/**
2091				 * 		PLANS
2092				 *  	
2093				 */	
2094				case 'createPlan':
2095					$plan = Stripe_Plan::create($data);
2096					$success = $this->fetch($plan);
2097					break;
2098				case 'retrievePlan':
2099					$plan = Stripe_Plan::retrieve($data['plan_id']);
2100					$success = $this->fetch($plan);
2101					break;
2102				case 'updatePlan':
2103					$p = Stripe_Plan::retrieve($data['plan_id']);
2104					
2105					foreach ($data['fields'] as $field => $value) {
2106						$p->$field = $value;
2107					}
2108
2109					$plan = $p->save();
2110					$success = $this->fetch($plan);
2111					break;
2112				case 'deletePlan':
2113					$p = Stripe_Plan::retrieve($data['plan_id']);
2114					$plan = $p->delete();
2115					
2116					$success = $this->fetch($plan);
2117					break;
2118				case 'listPlans':
2119					$plans = Stripe_Plan::all($data['options']);
2120					$success = $this->fetch($plans);
2121					
2122					foreach ($success['data'] as &$plan) {
2123						$plan = $this->fetch($plan);
2124					}
2125					break;
2126					
2127					
2128				/**
2129				 * 	 	COUPONS
2130				 * 	
2131				 */	
2132				case 'createCoupon':
2133					$coupon = Stripe_Coupon::create($data);
2134					$success = $this->fetch($coupon);
2135					break;
2136				case 'retrieveCoupon':
2137					$coupon = Stripe_Coupon::retrieve($data['coupon_id']);
2138					$success = $this->fetch($coupon);
2139					break;
2140				case 'deleteCoupon':
2141					$c = Stripe_Coupon::retrieve($data['coupon_id']);
2142					$coupon = $c->delete();
2143						
2144					$success = $this->fetch($coupon);
2145					break;
2146				case 'listCoupons':
2147					$coupons = Stripe_Coupon::all($data['options']);
2148					$success = $this->fetch($coupons);
2149						
2150					foreach ($success['data'] as &$coupon) {
2151						$coupon = $this->fetch($coupon);
2152					}
2153					break;
2154					
2155					
2156				/**
2157				 * 	 	DISCOUNTS
2158				 *
2159				 */
2160				case 'deleteCustomerDiscount':
2161					$cu = Stripe_Customer::retrieve($data['customer_id']);
2162					$discount = $cu->deleteDiscount();
2163					
2164					$success = $this->fetch($discount);
2165					break;
2166				case 'deleteSubscriptionDiscount':
2167					$cu = Stripe_Customer::retrieve($data['customer_id']);
2168					$discount = $cu->subscriptions->retrieve($data['subscription_id'])->deleteDiscount();
2169					
2170					$success = $this->fetch($discount);
2171					break;
2172
2173					
2174				/**
2175				 * 
2176				 * 	INVOICES
2177				 * 
2178				 */
2179				case 'retrieveInvoice':
2180					$coupon = Stripe_Invoice::retrieve($data['invoice_id']);
2181					$success = $this->fetch($coupon);
2182					
2183					if (!empty($success['lines']['data'])) {
2184						foreach ($success['lines']['data'] as &$invoice) {
2185							$invoice = $this->fetch($invoice);
2186						}
2187					}
2188					break;
2189				case 'retrieveInvoiceLine':
2190					$in = Stripe_Invoice::retrieve($data['invoice_id'])->lines->all($data['options']);
2191					$success = $this->fetch($in);
2192					
2193					if (!empty($success['data'])) {
2194						foreach ($success['data'] as &$invoice) {
2195							$invoice = $this->fetch($invoice);
2196						}
2197					}
2198					break;
2199				case 'createInvoice':
2200					$invoice = Stripe_Invoice::create($data);
2201					$success = $this->fetch($invoice);
2202					break;
2203				case 'payInvoice':
2204					$invoice = Stripe_Invoice::retrieve($data['invoice_id']);
2205					$in = $invoice->pay();
2206
2207					$success = $this->fetch($in);
2208					break;
2209				case 'updateInvoice':
2210					$in = Stripe_Invoice::retrieve($data['invoice_id']);
2211						
2212					foreach ($data['fields'] as $field => $value) {
2213						$in->$field = $value;
2214					}
2215
2216					$invoice = $in->save();
2217					$success = $this->fetch($invoice);
2218					
2219					if (!empty($success['lines']['data'])) {
2220						foreach ($success['lines']['data'] as &$invoice) {
2221							$invoice = $this->fetch($invoice);
2222						}
2223					}
2224					
2225					break;
2226				case 'listInvoices':
2227					$invocies = Stripe_Invoice::all($data);
2228					$success = $this->fetch($invocies);
2229				
2230					if (!empty($success['data'])) {
2231						foreach ($success['data'] as &$invoice) {
2232							$invoice = $this->fetch($invoice);
2233							
2234							if (!empty($invoice['lines']['data'])) {
2235								foreach ($invoice['lines']['data'] as &$invoiceLine) {
2236									$invoiceLine = $this->fetch($invoiceLine);
2237								}
2238								unset($invoiceLine);
2239							}
2240							
2241						}
2242					}
2243					break;
2244				case 'retrieveUpcomingInvoice':
2245					$invoice = Stripe_Invoice::upcoming($data);
2246					$success = $this->fetch($invoice);
2247
2248					if (!empty($success['lines']['data'])) {
2249						foreach ($success['lines']['data'] as &$invoice) {
2250							$invoice = $this->fetch($invoice);
2251						}
2252					}
2253					break;
2254					
2255				/**
2256				 * 
2257				 *  	INVOICE ITEMS
2258				 *  	
2259				 */	
2260				case 'createInvoiceItem':
2261					$success = $this->fetch(Stripe_InvoiceItem::create($data));
2262					break;
2263				case 'retrieveInvoiceItem':
2264					$success = $this->fetch(Stripe_InvoiceItem::retrieve($data['invoice_item_id']));
2265					break;
2266				case 'updateInvoiceItem':
2267					$ii = Stripe_InvoiceItem::retrieve($data['invoice_item_id']);
2268					
2269					foreach ($data['fields'] as $field => $value) {
2270						$ii->$field = $value;
2271					}
2272					
2273					$success = $this->fetch($ii->save());
2274					break;
2275				case 'deleteInvoiceItem':
2276					$ii = Stripe_InvoiceItem::retrieve($data['invoice_item_id']);
2277					
2278					$success = 

Large files files are truncated, but you can click here to view the full file