Skip to content

refactor connection usage #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
23 changes: 13 additions & 10 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ Example XML usage:
//assumes $subscription_xml contains xml data. This can be from your app, or
//populated from any Chargify Data object (inherits ChargifyBase) by calling the getXML() method.
// ex. $subscription_xml = $chargify_subscription->getXML();
$test_mode = false;
$request_format = 'XML';
$connector = new ChargifyConnector($test_mode);
$api_key = 'YOUR_API_KEY';

$connector = new ChargifyConnector($api_key);

try {
$new_subscription_xml = $connector->requestCreateSubscription($subscription_xml,$request_format);
Expand All @@ -32,9 +33,10 @@ Example JSON usage:
//assumes $subscription_json contains json data. This can be from your app, or
//populated from any Chargify Data object (inherits ChargifyBase) by calling the getJSON() method.
// ex. $subscription_json = $chargify_subscription->getJSON();
$test_mode = false;
$request_format = 'JSON';
$connector = new ChargifyConnector($test_mode);
$api_key = 'YOUR_API_KEY';

$connector = new ChargifyConnector($api_key);

try {
$new_subscription_json = $connector->requestCreateSubscription($subscription_json,$request_format);
Expand All @@ -46,10 +48,13 @@ try {

Example internal class usage:

// setup connector
$api_key = 'YOUR_API_KEY';
$connector = new ChargifyConnector($api_key);

//Customer creation
$test_mode = false;
$xml_import = null; //this is useful for populating from API response.
$chargify_customer = new ChargifyCustomer(null, $test_mode);
$chargify_customer = new ChargifyCustomer($connector, $xml_import);
$chargify_customer->email = "[email protected]";
$chargify_customer->first_name = "Charles";
$chargify_customer->last_name = "Jones";
Expand All @@ -66,7 +71,7 @@ try {

//Subscription creation
//create customer and credit card first
$chargify_customer = new ChargifyCustomer(); //defaults to same as above
$chargify_customer = new ChargifyCustomer($connector); //defaults to same as above
$chargify_customer->email = "[email protected]";
$chargify_customer->first_name = "Charles";
$chargify_customer->last_name = "Jones";
Expand All @@ -85,7 +90,7 @@ $chargify_card->billing_state = "CA";
$chargify_card->billing_zip = "55555";
$chargify_card->billing_country = 'US';

$chargify_subscription = new ChargifySubscription();
$chargify_subscription = new ChargifySubscription($connector);
//$chargify_subscription->customer_attributes is required
//(don't confuse with $chargify_subscription->customer.)
$chargify_subscription->customer_attributes = $chargify_customer;
Expand All @@ -99,5 +104,3 @@ try {
//process error handling code here.
echo $cve->getMessage();
}


10 changes: 5 additions & 5 deletions lib/ChargifyBase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
//Base class for Chargify Data Objects.
class ChargifyBase {
public function getXMLObject(&$xml = null) {
Expand All @@ -9,20 +9,20 @@ public function getXMLObject(&$xml = null) {
if ($key != 'connector') {
if (is_object($val) && method_exists($val, "getXMLObject")) {
$node = $xml->addChild($key);
$val->getXMLObject($node);
$val->getXMLObject($node);
} elseif ($val !== null) {
$xml->addChild($key,htmlentities($val, ENT_QUOTES));
}
}
}
return $xml;
return $xml;
}

public function getXML() {
$xml = $this->getXMLObject();
return $xml->asXML();
}

public function getJSON() {
return sprintf('{"%s":%s}', $this->getName(), json_encode($this->getXMLObject()));
}
Expand Down
26 changes: 13 additions & 13 deletions lib/ChargifyCharge.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

//Reference Documentation: http://support.chargify.com/faqs/api/api-charges

class ChargifyCharge extends ChargifyBase
class ChargifyCharge extends ChargifyBase
{
//******************************
//**** INPUT ONLY VARIABLES ****
Expand All @@ -11,40 +11,40 @@ class ChargifyCharge extends ChargifyBase

//******************************
//** INPUT & OUTPUT VARIABLES **
//******************************
//******************************
var $memo;

//******************************
//*** OUTPUT ONLY VARIABLES ****
//******************************
//******************************
var $success;
var $amount_in_cents;

private $connector;
public function __construct(SimpleXMLElement $product_xml_node = null, $test_mode = false)
public function __construct(ChargifyConnector $connector, SimpleXMLElement $product_xml_node = null)
{
$this->connector = new ChargifyConnector($test_mode);
$this->connector = $connector;
if ($product_xml_node) {
//Load object dynamically and convert SimpleXMLElements into strings
foreach($product_xml_node as $key => $element) {
$this->$key = (string)$element;
foreach($product_xml_node as $key => $element) {
$this->$key = (string)$element;
}
}
}

protected function getName() {
return "charge";
}

public function create($subscription_id) {
return $this->connector->createCharge($subscription_id, $this);
}

public function createByAmount($subscription_id) {
return $this->connector->createChargeByAmount($subscription_id, $this->amount, $this->memo);
}

public function createByAmountInCents($subscription_id) {
return $this->connector->createChargeByAmountInCents($subscription_id, $this->amount_in_cents, $this->memo);
}
}
}
Loading