Source of file PayPalPayment.php
Size: 9,994 Bytes - Last Modified: 2021-12-23T10:33:28+00:00
/var/www/docs.ssmods.com/process/src/code/PayPalPayment/PayPalPayment.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 | <?php /** * Implementation of Payment that supports PayPal Website Payment Standard * (https://www.paypal.com/IntegrationCenter/ic_standard_home.html) as its payment processor * * Configure using PayPalPayment::setMyVariable(value); * in www.mysite.com/ecommerce/_config.php file * * Must configure: * * $setPayPalRealAccount; * $setPayPalTestAccount; * $setPayPalUseTestAccount; * * Optionally configure: * * $setPayPalImageLocation; * $setPayPalContinueNextButton; * $setPayPalPurchaseName; * $setPayPalCppHeaderImage; * $setPayPalCppHeaderBackcolor; * $setPayPalCppHeaderBordercolor; * $setPayPalCppPayflowColor; * $setPayPalCs; * REQUIREMENTS: * * Need to add: "PayPalInstructions" field to CheckoutPage.php * * static $db = array( * "PayPalInstructions" => "HTMLText" * ); * * Have a PayPalPaymentPage.ss template or replace some code below * (search for renderWith). On the PayPalPaymentPage you can use * $PayPalInstructions (see above) * * @package payment */ class PayPalPayment extends Payment { public static $db = array( 'AuthorisationCode' => 'Text' ); // PayPal Informations protected static $privacy_link = 'https://www.paypal.com/us/cgi-bin/webscr?cmd=p/gen/ua/policy_privacy-outside'; protected static $logo = 'payment/images/payments/paypal.jpg'; // URLs protected static $url = 'https://www.paypal.com/cgi-bin/webscr'; protected static $test_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; // Test Mode protected static $test_mode = false; protected static $test_account_email; public static function set_test_mode($test_account_email) { self::$test_mode = true; self::$test_account_email = $test_account_email; } // Payment Informations protected static $account_email; public static function set_account_email($account_email) { self::$account_email = $account_email; } // PayPal Pages Style Optional Informations protected static $continue_button_text; public static function set_continue_button_text($continue_button_text) { self::$continue_button_text = $continue_button_text; } protected static $header_image_url; public static function set_header_image_url($header_image_url) { self::$header_image_url = $header_image_url; } protected static $header_back_color; public static function set_header_back_color($header_back_color) { self::$header_back_color = $header_back_color; } protected static $header_border_color; public static function set_header_border_color($header_border_color) { self::$header_border_color = $header_border_color; } protected static $payflow_color; public static function set_payflow_color($payflow_color) { self::$payflow_color = $payflow_color; } protected static $back_color; public static function set_back_color_black() { self::$back_color = '1'; } protected static $image_url; public static function set_image_url($image_url) { self::$image_url = $image_url; } protected static $page_style; public static function set_page_style($page_style) { self::$page_style = $page_style; } public function getPaymentFormFields() { $logo = '<img src="' . self::$logo . '" alt="Credit card payments powered by PayPal"/>'; $privacyLink = '<a href="' . self::$privacy_link . '" target="_blank" title="Read PayPal\'s privacy policy">' . $logo . '</a><br/>'; return new FieldSet( new LiteralField('PayPalInfo', $privacyLink), new LiteralField( 'PayPalPaymentsList', '<img src="payment/images/payments/methods/visa.jpg" alt="Visa"/>' . '<img src="payment/images/payments/methods/mastercard.jpg" alt="MasterCard"/>' . '<img src="payment/images/payments/methods/american-express.gif" alt="American Express"/>' . '<img src="payment/images/payments/methods/discover.jpg" alt="Discover"/>' . '<img src="payment/images/payments/methods/paypal.jpg" alt="PayPal"/>' ) ); } public function getPaymentFormRequirements() { return null; } public function processPayment($data, $form) { $page = new Page(); $page->Title = 'Redirection to PayPal...'; $page->Logo = '<img src="' . self::$logo . '" alt="Payments powered by PayPal"/>'; $page->Form = $this->PayPalForm(); $controller = new Page_Controller($page); $form = $controller->renderWith('PaymentProcessingPage'); return new Payment_Processing($form); } public function PayPalForm() { Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); // 1) Main Informations $fields = ''; $order = $this->Order(); $items = $order->Items(); $member = $order->Member(); // 2) Main Settings $url = self::$test_mode ? self::$test_url : self::$url; $inputs['cmd'] = '_cart'; $inputs['upload'] = '1'; // 3) Items Informations $cpt = 0; foreach ($items as $item) { $inputs['item_name_' . ++$cpt] = $item->TableTitle(); // item_number is unnecessary $inputs['amount_' . $cpt] = $item->UnitPrice(); $inputs['quantity_' . $cpt] = $item->Quantity; } // 4) Payment Informations And Authorisation Code $inputs['business'] = self::$test_mode ? self::$test_account_email : self::$account_email; $inputs['custom'] = $this->ID . '-' . $this->AuthorisationCode; // Add Here The Shipping And/Or Taxes $inputs['currency_code'] = $this->Currency; // 5) Redirection Informations $inputs['cancel_return'] = Director::absoluteBaseURL() . PayPalPayment_Handler::cancel_link($inputs['custom']); $inputs['return'] = Director::absoluteBaseURL() . PayPalPayment_Handler::complete_link(); $inputs['rm'] = '2'; // Add Here The Notify URL // 6) PayPal Pages Style Optional Informations if (self:: $continue_button_text) { $inputs['cbt'] = self::$continue_button_text; } if (self::$header_image_url) { $inputs['cpp_header_image'] = urlencode(self::$header_image_url); } if (self::$header_back_color) { $inputs['cpp_headerback_color'] = self::$header_back_color; } if (self::$header_border_color) { $inputs['cpp_headerborder_color'] = self::$header_border_color; } if (self::$payflow_color) { $inputs['cpp_payflow_color'] = self::$payflow_color; } if (self::$back_color) { $inputs['cs'] = self::$back_color; } if (self::$image_url) { $inputs['image_url'] = urlencode(self::$image_url); } if (self::$page_style) { $inputs['page_style'] = self::$page_style; } // 7) Prepopulating Customer Informations $inputs['first_name'] = $member->FirstName; $inputs['last_name'] = $member->Surname; $inputs['address1'] = $member->Address; $inputs['address2'] = $member->AddressLine2; $inputs['city'] = $member->City; $inputs['country'] = $member->Country; $inputs['email'] = $member->Email; if ($member->hasMethod('getState')) { $inputs['state'] = $member->getState(); } if ($member->hasMethod('getZip')) { $inputs['zip'] = $member->getZip(); } // 8) Form Creation if (is_array($inputs) && count($inputs)) { foreach ($inputs as $name => $value) { $ATT_value = Convert::raw2att($value); $fields .= "<input type=\"hidden\" name=\"$name\" value=\"$ATT_value\" />"; } } return <<<HTML <form id="PaymentForm" method="post" action="$url"> $fields <input type="submit" value="Submit" /> </form> <script type="text/javascript"> jQuery(document).ready(function() { jQuery("input[type='submit']").hide(); jQuery('#PaymentForm').submit(); }); </script> HTML; } public function populateDefaults() { parent::populateDefaults(); $this->AuthorisationCode = md5(uniqid(rand(), true)); } } /** * Handler for responses from the PayPal site */ class PayPalPayment_Handler extends Controller { public static $URLSegment = 'paypal'; public static function complete_link() { return self::$URLSegment . '/complete'; } public static function cancel_link($custom) { return self::complete_link() . '?custom=' . $custom; } /** * Manages the 'return' and 'cancel' PayPal replies */ public function complete() { if (isset($_REQUEST['custom']) && $custom = $_REQUEST['custom']) { $params = explode('-', $custom); if (count($params) == 2) { if ($payment = DataObject::get_by_id('PayPalPayment', $params[0])) { if ($payment->AuthorisationCode == $params[1]) { if (isset($_REQUEST['payment_status']) && $_REQUEST['payment_status'] == 'Completed') { $payment->Status = 'Success'; $payment->TxnRef = $_REQUEST['txn_id']; } else { $payment->Status = 'Failure'; } $payment->write(); $payment->redirectToOrder(); } } } } } } |