Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

Commit

Permalink
Added StripePlan model
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Oct 11, 2011
1 parent 4068888 commit 361540f
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
124 changes: 124 additions & 0 deletions models/stripe_plan.php
@@ -0,0 +1,124 @@
<?php
/**
* Stripe plan model
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2011, Jeremy Harris
* @link http://42pixels.com
* @package stripe
* @subpackage stripe.models
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

/**
* StripePlan
*
* This model is a bit special because Stripe does not have an API call to
* modify a plan. Therefore, calling an `update` will fail.
*
* @package stripe.models
*/
class StripePlan extends StripeAppModel {

/**
* API path
*
* @var string
*/
public $path = '/plans';

/**
* Plan schema
*
* @var array
*/
public $_schema = array(
'id' => array('type' => 'string', 'length' => '45'),
'amount' => array('type' => 'integer'),
'currency' => array('type' => 'string', 'length' => '3'),
'interval' => array('type' => 'string', 'length' => '5'),
'name' => array('type' => 'string'),
'trial_period_days' => array('type' => 'integer')
);

/**
* Validation rules
*
* @var array
*/
public $validate = array(
'id' => array(
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an id.',
'required' => true
)
),
'amount' => array(
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an amount.',
'required' => true
),
'numeric'=> array(
'rule' => array('numeric'),
'message' => 'Amount must be a number.',
'required' => true
)
),
'currency' => array(
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a currency type.',
'required' => true
),
'between '=> array(
'rule' => array('inList', array('usd')),
'message' => 'Please enter a valid currency code.',
'required' => true
)
),
'interval' => array(
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a billing interval.',
'required' => true
),
'between '=> array(
'rule' => array('inList', array('month', 'year')),
'message' => 'Please enter a valid billing interval.',
'required' => true
)
),
'name' => array(
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an name.',
'required' => true
)
),
'trial_period_days' => array(
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an trial period.'
),
'numeric'=> array(
'rule' => array('numeric'),
'message' => 'Trial period must be an amount of days.'
)
)
);

/**
* No such thing as updating a plan in the Stripe API
*
* @return boolean True
*/
public function beforeSave() {
$this->id = null;
return true;
}

}
51 changes: 51 additions & 0 deletions tests/cases/models/stripe_plan.test.php
@@ -0,0 +1,51 @@
<?php
App::import('Model', 'Stripe.StripePlan');

class TestPlan extends CakeTestCase {

function startTest() {
$this->Model = new StripePlan();
$this->Model->setDataSource('stripe_test');
$sources = ConnectionManager::enumConnectionObjects();
$this->skipIf(!in_array('stripe_test', array_keys($sources)), '`stripe_test` db config not found');
}

function endTest() {
unset($this->Model);
}

function testValidation() {
$result = $this->Model->save(array('StripePlan' => array()));
$this->assertFalse($result);

$this->assertTrue(array_key_exists('id', $this->Model->validationErrors));
$this->assertTrue(array_key_exists('amount', $this->Model->validationErrors));
$this->assertTrue(array_key_exists('currency', $this->Model->validationErrors));
$this->assertTrue(array_key_exists('interval', $this->Model->validationErrors));
$this->assertTrue(array_key_exists('name', $this->Model->validationErrors));

$this->assertFalse(array_key_exists('trial_period_days', $this->Model->validationErrors));
}

function testFlow() {
$results = $this->Model->save(array(
'StripePlan' => array(
'id' => 'Basic',
'amount' => 500,
'currency' => 'usd',
'interval' => 'month',
'name' => 'Basic Plan'
)
));
$this->assertTrue($results);

$id = $this->Model->id;

$results = $this->Model->read();
$this->assertEqual($results['StripePlan']['id'], $id);
$this->assertEqual($results['StripePlan']['amount'], 500);

$this->assertTrue($this->Model->delete($id));
}

}

0 comments on commit 361540f

Please sign in to comment.