Skip to content

Instantly share code, notes, and snippets.

@jedateach
Created May 17, 2012 03:34
Show Gist options
  • Save jedateach/2716041 to your computer and use it in GitHub Desktop.
Save jedateach/2716041 to your computer and use it in GitHub Desktop.
Payment Example Draft Code
<?php
/**
* Catch gateway-triggered requests
*/
class PaymentController extends Controller{
function init(){
$this->processor = $this->getProcessorForPayment(); //somehow get the right processor to continue handling
//Perhaps the processor class is sent in the url, eg: mysite/paymentcontrol/PayPal/?extradata=....
}
function success($request){
$response = $this->processor->handleGatewayResponse($request); //creates a new Paypal_Response object to parse body of request
$payment->updateStatus($response->getStatus());
//redirect to somewhere? perhaps originalcontroller->Link('success') ??
}
}
<?php
//processing occurring within a controller
class PaymentPage_Controller extends Page_Controller{
function index(){
//payment is a dataobject of it's own
$payment = new Payment();
$payment->setAmount(10,'NZD');
$payment->write();
//processor is a class of it's own, not inheriting from anything - borrowing heavily from active merchant architecture
$processor = new PaypalProcessor_Test($apikey = "AHSE#N$KAGKAEGAWEFGAS",$username = "BLAH"); //PaypalProcessor_Test extends PaypalProcessor, and subsequently PaymentProcessor
$processor->setPayment($payment); //extracts amount, currency, ID. May also be used to store data (but that's coupling processor to model :S)
$processor->setData(array( //set any extra data that might be needed, field names are standardized because of pre-defined mapping in the processor.
'street' => '4 Macarini Street',
'city' => '5 blah street'
));
$processor->validate(); //has the processor got everything it needs
//authorize, capture, void?
$processor->process(); //either performs payment, or triggers redirect?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment