11
Paypal + symfony payments made simple Massimiliano Arione September, 27th 2011

Paypal + symfony

Embed Size (px)

DESCRIPTION

Paypal + symfony integration. it'

Citation preview

Page 1: Paypal + symfony

Paypal + symfonypayments made simple

Massimiliano Arione

September, 27th 2011

Page 2: Paypal + symfony

About me

• 2001: PHP developer• 2006: GrUSP member• 2009: member of GrUSP steering committee • 2010: PUG Rome president :-)

Page 3: Paypal + symfony

the flowmy server Paypal server

Page 4: Paypal + symfony

the REAL flow

my server Paypal server

Page 5: Paypal + symfony

plugins

• sfPaymentPlugin• sfPaymentPayPalPlugin• sfWebBrowserPlugin

Page 6: Paypal + symfony

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

Page 7: Paypal + symfony

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;}

Page 8: Paypal + symfony

Controller// apps/frontend/modules/my_module/actions/actions.class.php

public function executeBuy(){  $purchase = new Purchase;  $this->transaction = $purchase->getTransaction();}

Page 9: Paypal + symfony

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>

Page 10: Paypal + symfony

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;}

Page 11: Paypal + symfony

Thanks!

Massimiliano Arione@garakkio

blog.garak.it