Created
October 17, 2011 17:19
-
-
Save ryanwachtl/1293155 to your computer and use it in GitHub Desktop.
SilverStripe PayPal Mini Cart Integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MiniCart extends DataObjectDecorator { | |
... | |
function contentcontrollerInit($controller) { | |
Requirements::javascript( | |
MODULE_MINICART_DIR . '/thirdparty/minicart/minicart.js' | |
); | |
Requirements::customScript('PAYPAL.apps.MiniCart.render();', 'minicart'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MiniCart extends DataObjectDecorator { | |
... | |
public static function MiniCartItemShortcodeHandler($attributes, $content = null, $parser = null) { | |
if(empty($attributes['name']) || empty($attributes['price'])) { | |
return; | |
} | |
$item_name = $attributes['name']; | |
$item_price = $attributes['price']; | |
$button = (!empty($content)) ? $content : "Add to cart"; | |
$data = new ArrayData( | |
array( | |
'ItemName' => $item_name, | |
'Amount' => $item_price, | |
'Button' => $button, | |
'Business' => self::$business_email, | |
'CurrencyCode' => self::$currency_code, | |
'Return' => self::$return_url, | |
'Cancel' => self::$cancel_return_url | |
) | |
); | |
return $data->renderWith('MiniCartItem'); | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MiniCart extends DataObjectDecorator { | |
protected static $business_email = "[email protected]"; | |
protected static $currency_code = "USD"; | |
protected static $return_url = "https://minicart.paypal-labs.com/?success"; | |
protected static $cancel_return_url = "https://minicart.paypal-labs.com/?cancel"; | |
public static function set_business_email($email) { | |
self::$business_email = $email; | |
} | |
public static function set_currency_code($code) { | |
self::$currency_code = $code; | |
} | |
public static function set_return_url($url) { | |
self::$return_url = $url; | |
} | |
public static function set_cancel_return_url($url) { | |
self::$cancel_return_url = $url; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MiniCart extends DataObjectDecorator { | |
... | |
public function ViewCart($button = "View your cart") { | |
$data = new ArrayData( | |
array( | |
'Business' => self::$business_email, | |
'Button' => $button | |
) | |
); | |
return $data->renderWith('ViewCart'); | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('MODULE_MINICART_DIR', 'module_minicart'); | |
DataObject::add_extension('SiteTree', 'MiniCart'); | |
ShortcodeParser::get()->register('mini_cart_item', array('MiniCart', 'MiniCartItemShortcodeHandler')); | |
MiniCart::set_business_email("[email protected]"); | |
MiniCart::set_currency_code("USD"); | |
MiniCart::set_return_url("https://example.com/page?success"); | |
MiniCart::set_cancel_return_url("https://example.com/page?cancel"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> | |
<fieldset> | |
<input type="hidden" name="cmd" value="_cart" /> | |
<input type="hidden" name="add" value="1" /> | |
<input type="hidden" name="business" value="$Business" /> | |
<input type="hidden" name="item_name" value="$ItemName" /> | |
<input type="hidden" name="amount" value="$Amount" /> | |
<input type="hidden" name="currency_code" value="$CurrencyCode" /> | |
<input type="hidden" name="return" value="$Return" /> | |
<input type="hidden" name="cancel_return" value="$Cancel" /> | |
<p><strong>$ItemName</strong><br />${$Amount}</p> | |
<input type="submit" name="submit" value="$Button" class="button" /> | |
</fieldset> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" class="last"> | |
<fieldset> | |
<input type="hidden" name="business" value="$Business" /> | |
<input type="hidden" name="cmd" value="_cart" /> | |
<input type="hidden" name="display" value="1" /> | |
<input type="submit" name="submit" value="$Button" class="button" /> | |
</fieldset> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment