Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .buildpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<buildpath>
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
<buildpathentry kind="lib" path="thirdparty/phpunit.phar"/>
<buildpathentry kind="src" path=""/>
</buildpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
22 changes: 22 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>chargeio-php</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.dltk.core.scriptbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.php.core.PHPNature</nature>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions .settings/org.eclipse.php.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Thu Jul 03 08:47:12 CDT 2014
eclipse.preferences.version=1
include_path=0;/chargeio-php\u00051;/chargeio-php/thirdparty/phpunit.phar
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2014- ChargeIO, LLC. (http://chargeio.com)

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.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,40 @@ chargeio-php
============

ChargeIO PHP Client Library

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

Download the PHP client library by doing the following:

git clone git://github.com/charge-io/chargeio-php.git

To use the library in your application, add the following to your PHP script:

require_once '/path/to/chargeio-php/lib/ChargeIO.php';

The library's APIs require credentials to access your merchant data on the
ChargeIO servers. You can provide credentials as arguments to the APIs used to
retrieve objects, or you can configure the library with default credentials to
use when necessary. To set the default credentials, substitute your ChargeIO
public key and test or live-mode secret key in a call to setCredentials:

ChargeIO::setCredentials(new ChargeIO_Credentials('<public_key>', '<secret_key>'));

Once your credentials are set, running a basic credit card charge looks like:

$card = new ChargeIO_Card(array('number' => '4242424242424242', 'exp_month' => 10, 'exp_year' => '2016'));
$charge = ChargeIO_Charge::create($card, 1200);

Using the ChargeIO.js library for payment tokenization support on your payment page
simplifies the process even more. Just configure the token callback on your page to
POST the token ID you receive to your PHP script and then perform the charge:

$amount = $_POST['amount'];
$token_id = $_POST['token_id'];
$charge = ChargeIO_Charge::create(new ChargeIO_PaymentMethodReference(array('id' => $token_id)), $amount);

Documentation
-----------

The latest ChargeIO API documentation is available at https://chargeio.com/.
50 changes: 50 additions & 0 deletions lib/ChargeIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

// This snippet (and some of the curl code) due to the Facebook SDK.
if (!function_exists('curl_init')) {
throw new Exception('ChargeIO needs the CURL PHP extension.');
}
if (!function_exists('json_decode')) {
throw new Exception('ChargeIO needs the JSON PHP extension.');
}
if (!function_exists('mb_detect_encoding')) {
throw new Exception('ChargeIO needs the Multibyte String PHP extension.');
}

// ChargeIO singleton
require(dirname(__FILE__) . '/ChargeIO/ChargeIO.php');

// Errors
require(dirname(__FILE__) . '/ChargeIO/Error.php');
require(dirname(__FILE__) . '/ChargeIO/ApiError.php');
require(dirname(__FILE__) . '/ChargeIO/AuthenticationError.php');
require(dirname(__FILE__) . '/ChargeIO/InvalidRequestError.php');

// Infrastructure
require(dirname(__FILE__) . '/ChargeIO/Credentials.php');
require(dirname(__FILE__) . '/ChargeIO/Connection.php');
require(dirname(__FILE__) . '/ChargeIO/Utils.php');

// Models
require(dirname(__FILE__) . '/ChargeIO/Object.php');
require(dirname(__FILE__) . '/ChargeIO/Merchant.php');
require(dirname(__FILE__) . '/ChargeIO/MerchantAccount.php');
require(dirname(__FILE__) . '/ChargeIO/AchAccount.php');
require(dirname(__FILE__) . '/ChargeIO/Signature.php');
require(dirname(__FILE__) . '/ChargeIO/Transaction.php');
require(dirname(__FILE__) . '/ChargeIO/Charge.php');
require(dirname(__FILE__) . '/ChargeIO/Refund.php');
require(dirname(__FILE__) . '/ChargeIO/Credit.php');
require(dirname(__FILE__) . '/ChargeIO/RecurringCharge.php');
require(dirname(__FILE__) . '/ChargeIO/RecurringChargeOccurrence.php');
require(dirname(__FILE__) . '/ChargeIO/PaymentMethod.php');
require(dirname(__FILE__) . '/ChargeIO/PaymentMethodReference.php');
require(dirname(__FILE__) . '/ChargeIO/Card.php');
require(dirname(__FILE__) . '/ChargeIO/Bank.php');
require(dirname(__FILE__) . '/ChargeIO/OneTimeToken.php');
require(dirname(__FILE__) . '/ChargeIO/List.php');
require(dirname(__FILE__) . '/ChargeIO/TransactionList.php');
require(dirname(__FILE__) . '/ChargeIO/RecurringChargeList.php');
require(dirname(__FILE__) . '/ChargeIO/RecurringChargeOccurrenceList.php');
require(dirname(__FILE__) . '/ChargeIO/BankList.php');
require(dirname(__FILE__) . '/ChargeIO/CardList.php');
15 changes: 15 additions & 0 deletions lib/ChargeIO/AchAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

class ChargeIO_AchAccount extends ChargeIO_Object {
public function __construct($attributes = array(), $connection = null) {
parent::__construct($attributes, $connection);
$this->connection = $connection;
$this->attributes = array_merge($this->attributes, $attributes);
}

public function update($attributes = array()) {
$accountAttrs = array_merge($this->attributes, $attributes);
$response = $this->connection->put('/ach-accounts/' . $this->id, $accountAttrs);
$this->updateAttributes($response);
}
}
30 changes: 30 additions & 0 deletions lib/ChargeIO/ApiError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

class ChargeIO_ApiError extends ChargeIO_Error {
public function __construct($json) {
parent::__construct(null, $json);

$this->messages = array();
$this->errors = array();
$attrs = json_decode($json, true);
if ($attrs['messages']) {
foreach ($attrs['messages'] as $message) {
if ($message['level'] == 'error' && $message['message']) {
if (empty($this->errors)) {
$this->code = $message['code'];
}
$this->messages[] = $message;
$this->errors[] = $message['message'];
}
}
}

if (empty($this->errors)) {
$this->messages[] = array('code' => 'application_error', 'level' => 'error');
$this->errors[] = 'Unknown ChargeIO Error';
$this->code = 'application_error';
}

$this->message = $this->errors[0];
}
}
4 changes: 4 additions & 0 deletions lib/ChargeIO/AuthenticationError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

class ChargeIO_AuthenticationError extends ChargeIO_Error {
}
47 changes: 47 additions & 0 deletions lib/ChargeIO/Bank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

class ChargeIO_Bank extends ChargeIO_Object implements ChargeIO_PaymentMethod {
public function __construct($attributes = array(), $connection = null) {
parent::__construct($attributes, $connection);
$this->connection = $connection;
$this->attributes = array_merge($this->attributes, $attributes);
$this->attributes['type'] = 'bank';
}

public static function create($attributes = array()) {
return self::createUsingCredentials(ChargeIO::getCredentials(), $attributes);
}

public static function createUsingCredentials($credentials, $attributes = array()) {
$conn = new ChargeIO_Connection($credentials);

$attributes['type'] = 'bank';
$response = $conn->post('/banks', $attributes);
return new ChargeIO_Bank($response, $conn);
}

public static function all($params = array()) {
return self::allUsingCredentials(ChargeIO::getCredentials(), $params);
}

public static function allUsingCredentials($credentials, $params = array()) {
$conn = new ChargeIO_Connection($credentials);
$response = $conn->get('/banks', $params);
return new ChargeIO_BankList($response, $conn);
}

public static function findById($id) {
return self::findByIdUsingCredentials(ChargeIO::getCredentials(), $id);
}

public static function findByIdUsingCredentials($credentials, $id) {
$conn = new ChargeIO_Connection($credentials);
$response = $conn->get('/banks/' . $id);
return new ChargeIO_Bank($response, $conn);
}

public function delete() {
$response = $this->connection->delete('/banks/' . $this->id);
$this->updateAttributes($response);
}
}
8 changes: 8 additions & 0 deletions lib/ChargeIO/BankList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class ChargeIO_BankList extends ChargeIO_List {
protected function parseResult($offset) {
$bankAttrs = $this->attributes['results'][$offset];
return new ChargeIO_Bank($bankAttrs, $this->connection);
}
}
47 changes: 47 additions & 0 deletions lib/ChargeIO/Card.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

class ChargeIO_Card extends ChargeIO_Object implements ChargeIO_PaymentMethod {
public function __construct($attributes = array(), $connection = null) {
parent::__construct($attributes, $connection);
$this->connection = $connection;
$this->attributes = array_merge($this->attributes, $attributes);
$this->attributes['type'] = 'card';
}

public static function create($attributes = array()) {
return self::createUsingCredentials(ChargeIO::getCredentials(), $attributes);
}

public static function createUsingCredentials($credentials, $attributes = array()) {
$conn = new ChargeIO_Connection($credentials);

$attributes['type'] = 'card';
$response = $conn->post('/cards', $attributes);
return new ChargeIO_Card($response, $conn);
}

public static function all($params = array()) {
return self::allUsingCredentials(ChargeIO::getCredentials(), $params);
}

public static function allUsingCredentials($credentials, $params = array()) {
$conn = new ChargeIO_Connection($credentials);
$response = $conn->get('/cards', $params);
return new ChargeIO_CardList($response, $conn);
}

public static function findById($id) {
return self::findByIdUsingCredentials(ChargeIO::getCredentials(), $id);
}

public static function findByIdUsingCredentials($credentials, $id) {
$conn = new ChargeIO_Connection($credentials);
$response = $conn->get('/cards/' . $id);
return new ChargeIO_Card($response, $conn);
}

public function delete() {
$response = $this->connection->delete('/cards/' . $this->id);
$this->updateAttributes($response);
}
}
8 changes: 8 additions & 0 deletions lib/ChargeIO/CardList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class ChargeIO_CardList extends ChargeIO_List {
protected function parseResult($offset) {
$cardAttrs = $this->attributes['results'][$offset];
return new ChargeIO_Card($cardAttrs, $this->connection);
}
}
52 changes: 52 additions & 0 deletions lib/ChargeIO/Charge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

class ChargeIO_Charge extends ChargeIO_Transaction {
public static function create($paymentMethod, $amount, $params = array()) {
return self::createUsingCredentials(ChargeIO::getCredentials(), $paymentMethod, $amount, $params);
}

public static function createUsingCredentials($credentials, $paymentMethod, $amount, $params = array()) {
$conn = new ChargeIO_Connection($credentials);

$params['amount'] = $amount;
if ($paymentMethod instanceof ChargeIO_PaymentMethodReference) {
$params['method'] = $paymentMethod->id;
}
else {
$params['method'] = $paymentMethod->attributes;
}

$response = $conn->post('/charges', $params);
return new ChargeIO_Charge($response, $conn);
}

public static function authorize($paymentMethod, $amount, $params = array()) {
return self::authorizeUsingCredentials(ChargeIO::getCredentials(), $paymentMethod, $amount, $params);
}

public static function authorizeUsingCredentials($credentials, $paymentMethod, $amount, $params = array()) {
return self::createUsingCredentials($credentials, $paymentMethod, $amount, array_merge($params, array('auto_capture' => false)));
}

public function refund($amount, $params = array()) {
$params['amount'] = $amount;
$response = $this->connection->post('/charges/' . $this->id . '/refund', $params);
return new ChargeIO_Refund($response, $this->connection);
}

public function capture($amount, $params = array()) {
$params['amount'] = $amount;
$response = $this->connection->post('/charges/' . $this->id . '/capture', $params);
$this->updateAttributes($response);
}

public static function allHolds($params = array()) {
return self::allHoldsUsingCredentials(ChargeIO::getCredentials(), $params);
}

public static function allHoldsUsingCredentials($credentials, $params = array()) {
$conn = new ChargeIO_Connection($credentials);
$response = $conn->get('/charges/holds', $params);
return new ChargeIO_TransactionList($response, $conn);
}
}
Loading