Skip to content

Commit

Permalink
adding direct checkout to Charge class
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Christenson committed Apr 25, 2013
1 parent 01b4bbf commit ddbcf88
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/Twocheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ abstract class Twocheckout
public static $format = "json";
public static $apiBaseUrl = "https://www.2checkout.com/api/";
public static $error;
const VERSION = '0.1.1';
const VERSION = '0.1.2';

static function setCredentials($user, $pass)
{
Expand Down
26 changes: 26 additions & 0 deletions lib/Twocheckout/TwocheckoutCharge.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ public static function form($params, $type='Checkout')
}
}

public static function direct($params, $type='Checkout')
{
echo '<form id="2checkout" action="https://www.2checkout.com/checkout/purchase" method="post">';

foreach ($params as $key => $value)
{
echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
}

if ($type == 'auto') {
echo '<input type="submit" value="Click here if the payment form does not open automatically." /></form>';
echo '<script type="text/javascript">
function submitForm() {
document.getElementById("tco_lightbox").style.display = "block";
document.getElementById("2checkout").submit();
}
setTimeout("submitForm()", 2000);
</script>';
} else {
echo '<input type="submit" value="'.$type.'" />';
echo '</form>';
}

echo '<script src="https://www.2checkout.com/static/checkout/javascript/direct.min.js"></script>';
}

public static function link($params)
{
$url = 'https://www.2checkout.com/checkout/purchase?'.http_build_query($params, '', '&amp;');
Expand Down
169 changes: 121 additions & 48 deletions test/ChargeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,128 @@
class TestCharge extends PHPUnit_Framework_TestCase
{

public function testChargeForm()
{
$params = array(
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01'
);
$test = '<form id="2checkout" action="https://www.2checkout.com/checkout/purchase" method="post">' +
'<input type="hidden" name="sid" value="1817037"/>' +
'<input type="hidden" name="mode" value="2CO"/>' +
'<input type="hidden" name="li_0_name" value="Test Product"/>' +
'<input type="hidden" name="li_0_price" value="0.01"/>' +
'<input type="submit" value="Click Here!" />' +
'</form>';
$result = Twocheckout_Charge::form($params, "Click Here!");
$this->assertEquals($test, $result);
}

public function testChargeFormAuto()
{
$params = array(
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01'
);
$test = '<form id="2checkout" action="https://www.2checkout.com/checkout/purchase" method="post">' +
'<input type="hidden" name="sid" value="1817037"/>' +
'<input type="hidden" name="mode" value="2CO"/>' +
'<input type="hidden" name="li_0_name" value="Test Product"/>' +
'<input type="hidden" name="li_0_price" value="0.01"/>' +
'<input type="submit" value="Click here if you are not redirected automatically" /></form>' +
'<script type="text/javascript">document.getElementById(\'2checkout\').submit();</script>';
$result = Twocheckout_Charge::form($params, 'auto');
$this->assertEquals($test, $result);
}

public function testChargeLink()
{
public function testChargeForm()
{
$params = array(
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01'
);
$test = '<form id="2checkout" action="https://www.2checkout.com/checkout/purchase" method="post">' +
'<input type="hidden" name="sid" value="1817037"/>' +
'<input type="hidden" name="mode" value="2CO"/>' +
'<input type="hidden" name="li_0_name" value="Test Product"/>' +
'<input type="hidden" name="li_0_price" value="0.01"/>' +
'<input type="submit" value="Click Here!" />' +
'</form>';
$result = Twocheckout_Charge::form($params, "Click Here!");
$this->assertEquals($test, $result);
}

public function testChargeFormAuto()
{
$params = array(
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01'
);
$test = '<form id="2checkout" action="https://www.2checkout.com/checkout/purchase" method="post">' +
'<input type="hidden" name="sid" value="1817037"/>' +
'<input type="hidden" name="mode" value="2CO"/>' +
'<input type="hidden" name="li_0_name" value="Test Product"/>' +
'<input type="hidden" name="li_0_price" value="0.01"/>' +
'<input type="submit" value="Click here if you are not redirected automatically" /></form>' +
'<script type="text/javascript">document.getElementById(\'2checkout\').submit();</script>';
$result = Twocheckout_Charge::form($params, 'auto');
$this->assertEquals($test, $result);
}

public function testDirect()
{
$params = array(
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01',
'card_holder_name' => 'Testing Tester',
'email' => '[email protected]',
'street_address' => '123 test st',
'city' => 'Columbus',
'state' => 'Ohio',
'zip' => '43123',
'country' => 'USA'
);
$test = '<form id="2checkout" action="https://www.2checkout.com/checkout/purchase" method="post">' +
'<input type="hidden" name="sid" value="1817037"/>' +
'<input type="hidden" name="mode" value="2CO"/>' +
'<input type="hidden" name="li_0_name" value="Test Product"/>' +
'<input type="hidden" name="li_0_price" value="0.01"/>' +
'<input type="hidden" name="card_holder_name" value="Testing Tester"/>' +
'<input type="hidden" name="email" value="[email protected]"/>' +
'<input type="hidden" name="street_address" value="123 test st"/>' +
'<input type="hidden" name="city" value="Columbus"/>' +
'<input type="hidden" name="state" value="Ohio"/>' +
'<input type="hidden" name="zip" value="43123"/>' +
'<input type="hidden" name="country" value="USA"/>' +
'<input type="submit" value="Click Here!" /></form>' +
'<script src="https://www.2checkout.com/static/checkout/javascript/direct.min.js"></script>';
$result = Twocheckout_Charge::direct($params, "Click Here!");
$this->assertEquals($test, $result);
}

public function testDirectAuto()
{
$params = array(
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01',
'card_holder_name' => 'Testing Tester',
'email' => '[email protected]',
'street_address' => '123 test st',
'city' => 'Columbus',
'state' => 'Ohio',
'zip' => '43123',
'country' => 'USA'
);
$test = '<form id="2checkout" action="https://www.2checkout.com/checkout/purchase" method="post">' +
'<input type="hidden" name="sid" value="1817037"/>' +
'<input type="hidden" name="mode" value="2CO"/>' +
'<input type="hidden" name="li_0_name" value="Test Product"/>' +
'<input type="hidden" name="li_0_price" value="0.01"/>' +
'<input type="hidden" name="card_holder_name" value="Testing Tester"/>' +
'<input type="hidden" name="email" value="[email protected]"/>' +
'<input type="hidden" name="street_address" value="123 test st"/>' +
'<input type="hidden" name="city" value="Columbus"/>' +
'<input type="hidden" name="state" value="Ohio"/>' +
'<input type="hidden" name="zip" value="43123"/>' +
'<input type="hidden" name="country" value="USA"/>' +
'<input type="submit" value="Click here if the payment form does not open automatically." /></form>' +
'<script type="text/javascript">
function submitForm() {
document.getElementById("tco_lightbox").style.display = "block";
document.getElementById("2checkout").submit();
}
setTimeout("submitForm()", 2000);
</script>' +
'<script src="https://www.2checkout.com/static/checkout/javascript/direct.min.js"></script>';
$result = Twocheckout_Charge::direct($params, 'auto');
$this->assertEquals($test, $result);
}

public function testChargeLink()
{
$params = array(
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01'
'sid' => '1817037',
'mode' => '2CO',
'li_0_name' => 'Test Product',
'li_0_price' => '0.01'
);
$test = 'https://www.2checkout.com/checkout/purchase?sid=1817037&amp;mode=2CO&amp;li_0_name=Test+Product&amp;li_0_price=0.01';
$result = Twocheckout_Charge::link($params);
$this->assertEquals($test, $result);
}
$test = 'https://www.2checkout.com/checkout/purchase?sid=1817037&amp;mode=2CO&amp;li_0_name=Test+Product&amp;li_0_price=0.01';
$result = Twocheckout_Charge::link($params);
$this->assertEquals($test, $result);
}

}
2 changes: 1 addition & 1 deletion test/SaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testSaleRefundSale()
$sale = Twocheckout_Sale::refund($params, 'array');
$this->assertEquals("OK", sizeof($sale['response_code']));
} catch (Twocheckout_Error $e) {
$this->assertEquals("Invoice was already refunded.", $e->getMessage());
$this->assertEquals("Invoice too old to refund.", $e->getMessage());
}
}

Expand Down

0 comments on commit ddbcf88

Please sign in to comment.