Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Class FooBar_Controller
- */
- class FooBar_Controller extends Page_Controller {
- private static $allowed_actions = array(
- 'Form',
- 'complete'
- );
- public function init() {
- parent::init();
- Requirements::javascript('mysite/javascript/script.js');
- }
- /**
- * Create form to handle the submit() method
- *
- * @return mixed
- */
- public function Form() {
- $action = new FormAction('submit', 'Make Payment');
- $action->useButtonTag = true;
- $action->addExtraClass('btn btn-primary btn-paypal');
- return Form::create($this, 'Form',
- new FieldList(),
- new FieldList(
- $action
- )
- );
- }
- /**
- * Assign a member to a Payment, then redirect to the gateway.
- *
- * @param $data
- * @param $form
- * @return bool|SS_HTTPResponse
- */
- public function submit($data, $form) {
- $memberID = Member::currentUserID();
- /**
- * Check if user exists, if not redirect to the previous page.
- */
- if(!$member = DataObject::get_by_id('Member', $memberID)) {
- // TODO: Display error
- $this->setFlash('You\re not currently logged in', 'danger');
- return $this->redirect($this->Link());
- }
- /**
- * Assign the member id to the payment
- * TODO: Check against a config/SiteConfig setting for the payment gateway, and price.
- */
- $payment = Payment::create()->init("PayPal_Express", 100, "NZD");
- /**
- * Assign the member id to the payment
- */
- $payment->MemberID = $memberID;
- /**
- * Go to payment Gateway
- * TODO: Customise Message
- */
- $response = PurchaseService::create($payment)
- ->setReturnUrl($this->Link('complete'))
- ->setCancelUrl($this->Link()."?message=payment cancelled")
- ->purchase($form->getData());
- $response->redirect();
- }
- /**
- * Redirect the user once a payment has been made.
- *
- * @return SS_HTTPResponse
- */
- public function complete() {
- //TODO: redirect to member page
- return $this->redirect(Director::absoluteBaseURL());
- }
- }
- <?php
- class PaymentExtension extends DataExtension {
- private static $has_one = array(
- 'Member' => 'Member'
- );
- /**
- * When a payment has been captured. Update the linked Member.
- *
- * @param $response
- */
- public function onCaptured($response) {
- /**
- * Check the member exists.
- */
- $member = $this->owner->Member();
- if($member->exists()){
- /**
- * Update the {ExpiryDate} of the Member to the payments edited time plus one year.
- * TODO: Add different time-frames.
- * TODO: Check for valid time.
- */
- $newExpiryDate = date('Y-m-d H:i:s.u', strtotime('+1 year', strtotime($this->owner->LastEdited)));
- $member->ExpiryDate = $newExpiryDate;
- $member->write();
- } else {
- // TODO: If there's no user found, email the admin
- }
- }
- }
- <?php
- /**
- * Class MemberExtension
- */
- class MemberExtension extends DataExtension {
- private static $db = array(
- 'ExpiryDate' => 'Varchar(30)'
- );
- /**
- * @param FieldList $fields
- */
- public function updateCMSFields(FieldList $fields) {
- $fields->addFieldToTab('Root.Main', new TextField('ExpiryDate'));
- /* -----------------------------------------
- * Payments
- *
- * Just for reference purposes in individual members.
- ------------------------------------------*/
- $payments = Payment::get()->filter(array('MemberID' => $this->owner->ID));
- $config = GridFieldConfig_RelationEditor::create(100);
- $config->removeComponentsByType('GridFieldDeleteAction');
- $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
- 'Created' => 'Created',
- 'Money' => 'Money',
- 'Gateway' => 'Gateway',
- 'Status' => 'Status'
- ));
- $gridField = new GridField(
- 'Payments',
- 'Payments',
- $payments,
- $config
- );
- $fields->addFieldToTab('Root.Payments', $gridField);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement