Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
isaachall committed Nov 2, 2009
0 parents commit ba84aa5
Show file tree
Hide file tree
Showing 142 changed files with 43,649 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.DS_Store
*.tmproj
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2009 Recurly, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
44 changes: 44 additions & 0 deletions README.md
@@ -0,0 +1,44 @@
Recurly PHP Client
==================

Overview
--------
The Recurly PHP Client allows your PHP website to integrate with Recurly for your subscription management.


Installation
------------

If you already have git, the easiest way to download the Recurly PHP Client is with the git command:

git clone git://github.com/recurly/recurly-client-php.git /path/to/include/recurly

Alternatively, you may download the files in the library directory and place them within your PHP project.


Initialization
--------------

First, the Recurly classes must be loaded:

<?php
require_once('recurly/recurly.php');
?>

Next, specify your username and password for your Recurly account. Please see the [Authentication](http://support.recurly.com/faqs/api/authentication) documentation for more details

<?php
RecurlyClient::SetAuth(RECURLY_USERNAME, RECURLY_PASSWORD);
?>


Usage
-----

Please see the test code in the unitTest directory for examples.


API Documentation
-----------------

Please see the [Recurly API](http://support.recurly.com/faqs/api/) for more information.
11 changes: 11 additions & 0 deletions library/recurly.php
@@ -0,0 +1,11 @@
<?php

// Require all Recurly classes
require_once('recurlyclient.php');
require_once('recurlyaccount.php');
require_once('recurlybillinginfo.php');
require_once('recurlycreditcard.php');
require_once('recurlyexception.php');
require_once('recurlyplan.php');
require_once('recurlyresponse.php');
require_once('recurlysubscription.php');
211 changes: 211 additions & 0 deletions library/recurlyaccount.php
@@ -0,0 +1,211 @@
<?php

/**
* @category Recurly
* @package Recurly_Client_PHP
* @copyright Copyright (c) 2009 {@link http://recurly.com Recurly, Inc.}
*/
class RecurlyAccount
{
var $account_code;
var $username;
var $email;
var $first_name;
var $last_name;
var $company_name;

function RecurlyAccount($account_code = null, $username = null, $email = null, $first_name = null, $last_name = null, $company_name = null)
{
$this->account_code = $account_code;
$this->username = $username;
$this->email = $email;
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->company_name = $company_name;
}

public static function getAccount($accountCode)
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($accountCode);
$result = RecurlyClient::__sendRequest($uri, 'GET');
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'account', 'RecurlyAccount');
} else if ($result->code == '404') {
return null;
} else {
throw new RecurlyException("Could not get account info for {$accountCode}: {$result->response} -- ({$result->code})");
}
}

public function create()
{
$uri = RecurlyClient::PATH_ACCOUNTS;
$data = $this->getXml();
$result = RecurlyClient::__sendRequest($uri, 'POST', $data);
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'account', 'RecurlyAccount');
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not create an account for {$this->account_code}: {$result->response} -- ({$result->code}) " . $data);
}
}

public function update()
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($this->account_code);
$data = $this->getXml();
$result = RecurlyClient::__sendRequest($uri, 'PUT', $data);
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'account', 'RecurlyAccount');
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not update the account for {$this->account_code}: {$result->response} ({$result->code})");
}
}

public static function closeAccount($accountCode)
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($accountCode);
$result = RecurlyClient::__sendRequest($uri, 'DELETE');
if (preg_match("/^2..$/", $result->code)) {
return true;
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not close the account for {$accountCode}: {$result->response} ({$result->code})");
}
}

public function creditAccount($amount, $description = '')
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($this->account_code) . RecurlyClient::PATH_ACCOUNT_CREDITS;
$credit = new RecurlyAccountCredit($amount, $description);
$data = $credit->getXml();
$result = RecurlyClient::__sendRequest($uri, 'POST', $data);
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'credit', 'RecurlyAccountCredit');
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not create a credit for {$this->account_code}: {$result->response} ({$result->code})");
}
}

public function chargeAccount($amount, $description = '')
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($this->account_code) . RecurlyClient::PATH_ACCOUNT_CHARGES;
$credit = new RecurlyAccountCharge($amount, $description);
$data = $credit->getXml();
$result = RecurlyClient::__sendRequest($uri, 'POST', $data);
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'charge', 'RecurlyAccountCharge');
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not create a charge for {$this->account_code}: {$result->response} ({$result->code})");
}
}

public function listCredits()
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($this->account_code) . RecurlyClient::PATH_ACCOUNT_CREDITS;
$result = RecurlyClient::__sendRequest($uri);
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'credit', 'RecurlyAccountCredit');
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not list credits for account {$this->account_code}: {$result->response} ({$result->code})");
}
}

public function listCharges()
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($this->account_code) . RecurlyClient::PATH_ACCOUNT_CHARGES;
$result = RecurlyClient::__sendRequest($uri);
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'charge', 'RecurlyAccountCharge');
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not list charges for account {$this->account_code}: {$result->response} ({$result->code})");
}
}

public function getXml()
{
$doc = new DOMDocument("1.0");
$this->populateXmlDoc($doc, $doc);
return $doc->saveXML();
}

public function populateXmlDoc(&$doc, &$root)
{
$account = $root->appendChild($doc->createElement("account"));
$account->appendChild($doc->createElement("account_code", $this->account_code));
$account->appendChild($doc->createElement("username", $this->username));
$account->appendChild($doc->createElement("email", $this->email));
$account->appendChild($doc->createElement("first_name", $this->first_name));
$account->appendChild($doc->createElement("last_name", $this->last_name));
$account->appendChild($doc->createElement("company_name", $this->company_name));
return $account;
}
}

class RecurlyAccountCredit
{
var $amount_in_cents;
var $description;

public function RecurlyAccountCredit($amount = 0, $description = null)
{
$this->amount_in_cents = intval($amount * 100);
$this->description = $description;
}

/* Normalize the amount to a positive float amount */
public function amount()
{
return abs($this->amount_in_cents / 100.0);
}

public function getXml()
{
$amount_in_cents =
$doc = new DOMDocument("1.0");
$root = $doc->appendChild($doc->createElement("credit"));
$root->appendChild($doc->createElement("amount_in_cents", $this->amount_in_cents));
$root->appendChild($doc->createElement("description", $this->description));
return $doc->saveXML();
}
}

class RecurlyAccountCharge
{
var $amount_in_cents;
var $description;

public function RecurlyAccountCharge($amount = 0, $description = null)
{
$this->amount_in_cents = intval($amount * 100);
$this->description = $description;
}

/* Normalize the amount to a positive float amount */
public function amount()
{
return abs($this->amount_in_cents / 100.0);
}

public function getXml()
{
$amount_in_cents =
$doc = new DOMDocument("1.0");
$root = $doc->appendChild($doc->createElement("charge"));
$root->appendChild($doc->createElement("amount_in_cents", $this->amount_in_cents));
$root->appendChild($doc->createElement("description", $this->description));
return $doc->saveXML();
}
}
69 changes: 69 additions & 0 deletions library/recurlybillinginfo.php
@@ -0,0 +1,69 @@
<?php

/**
* @category Recurly
* @package Recurly_Client_PHP
* @copyright Copyright (c) 2009 {@link http://recurly.com Recurly, Inc.}
*/
class RecurlyBillingInfo
{
var $account_code;

var $first_name;
var $lastName;
var $address1;
var $address2;
var $city;
var $state;
var $country;
var $zip;
var $credit_card;
var $ip_address;

function RecurlyBillingInfo($accountCode = null)
{
$this->account_code = $accountCode;
$this->ip_address = $_SERVER['REMOTE_ADDR'];
$this->credit_card = new RecurlyCreditCard();
}

public function update()
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($this->account_code) . RecurlyClient::PATH_BILLING_INFO;
$data = $this->getXml();
$result = RecurlyClient::__sendRequest($uri, 'PUT', $data);
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'billing_info', 'RecurlyBillingInfo');
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not update the billing information for {$this->account_code}: {$result->response} ({$result->code})");
}
}

public function getXml()
{
$doc = new DOMDocument("1.0");
$billing = $this->populateXmlDoc($doc, $doc);

if (isset($this->credit_card) && $this->credit_card != null)
$this->credit_card->populateXmlDoc($doc, $billing);

return $doc->saveXML();
}

public function populateXmlDoc(&$doc, &$root)
{
$billing = $root->appendChild($doc->createElement("billing_info"));
$billing->appendChild($doc->createElement("first_name", $this->first_name));
$billing->appendChild($doc->createElement("last_name", $this->last_name));
$billing->appendChild($doc->createElement("address1", $this->address1));
$billing->appendChild($doc->createElement("address2", $this->address2));
$billing->appendChild($doc->createElement("city", $this->city));
$billing->appendChild($doc->createElement("state", $this->state));
$billing->appendChild($doc->createElement("zip", $this->zip));
$billing->appendChild($doc->createElement("country", $this->country));
$billing->appendChild($doc->createElement("ip_address", $this->ip_address));
return $billing;
}
}

0 comments on commit ba84aa5

Please sign in to comment.