Upload
massimiliano-arione
View
2.972
Download
1
Embed Size (px)
DESCRIPTION
Paypal + symfony integration. it'
Citation preview
Paypal + symfonypayments made simple
Massimiliano Arione
September, 27th 2011
About me
• 2001: PHP developer• 2006: GrUSP member• 2009: member of GrUSP steering committee • 2010: PUG Rome president :-)
the flowmy server Paypal server
the REAL flow
my server Paypal server
plugins
• sfPaymentPlugin• sfPaymentPayPalPlugin• sfWebBrowserPlugin
configuration# apps/frontend/config/app.ymlall: sf_payment_paypal_plugin: business: [email protected] test: business: [email protected] return: @paypal_paid cancel_return: @paypal_cancelled notify: @paypal_ipn
Model// lib/model/Purchase.class.phppublic function getTransaction(){ $gateway = new sfPaymentPayPal(); $gateway->fields['no_shipping'] = 1; $gateway->fields['item_number'] = $this->getId(); $transaction = new sfPaymentTransaction($gateway); if (sfConfig::get('sf_environment') != 'prod') { $transaction->enableTestMode(); } $transaction->setAmount($this->getPrice()); $transaction->setCurrency('EUR'); $transaction->setProductName($this->getName()); return $transaction;}
Controller// apps/frontend/modules/my_module/actions/actions.class.php
public function executeBuy(){ $purchase = new Purchase; $this->transaction = $purchase->getTransaction();}
View// apps/frontend/modules/my_module/templates/buySuccess.php <form method="post" action="<?php echo $transaction->getGateway()->gatewayUrl ?>"> <input type="submit" value="pay" /> <?php foreach ($transaction->getGateway()->fields as $fname => $fvalue): ?> <input type="hidden" name="<?php echo $fname ?>" value="<?php echo $fvalue ?>" /> <?php endforeach ?></form>
IPN controller// apps/frontend/modules/paypal/actions/actions.class.phppublic function executeIpn(sfWebRequest $request){ $gateway = new sfPaymentPayPal(); $transaction = new sfPaymentTransaction($gateway); if (sfConfig::get('sf_environment') != 'prod') $transaction->enableTestMode(); if ($transaction->validateIpn($request->getPostParameters())) { $purchase = PurchaseTable::getInstance()->find($request->getParameter('item_number')); $this->forward404Unless($purchase, 'purchase not found'); if (!$purchase->isComplete()) { if ($transaction->isCompleted()) $purchase->setComplete($request->getParameter('txn_id')); else $purchase->setIncomplete(); } } else { $purchase->setInvalid(); }
return sfView::NONE;}
Thanks!
Massimiliano Arione@garakkio
blog.garak.it