Mage_Paypal_Model_Express Class Reference

Inheritance diagram for Mage_Paypal_Model_Express:

Mage_Payment_Model_Method_Abstract Varien_Object

List of all members.

Public Member Functions

 canUseForCurrency ($currencyCode)
 getApi ()
 getSession ()
 getCheckout ()
 getQuote ()
 getRedirectUrl ()
 getCountryRegionId ()
 canUseInternal ()
 canUseForMultishipping ()
 catchError ()
 throwError ()
 createFormBlock ($name)
 createInfoBlock ($name)
 getOrderPlaceRedirectUrl ()
 getPaymentAction ()
 shortcutSetExpressCheckout ()
 returnFromPaypal ()
 authorize (Varien_Object $payment, $amount)
 capture (Varien_Object $payment, $amount)
 placeOrder (Varien_Object $payment)
 void (Varien_Object $payment)
 refund (Varien_Object $payment, $amount)
 initialize ($paymentAction, $stateObject)
 isInitializeNeeded ()

Protected Member Functions

 _getExpressCheckoutDetails ()

Protected Attributes

 $_code = 'paypal_express'
 $_formBlockType = 'paypal/express_form'
 $_infoBlockType = 'paypal/express_info'
 $_isGateway = false
 $_canAuthorize = true
 $_canCapture = true
 $_canCapturePartial = false
 $_canRefund = false
 $_canVoid = true
 $_canUseInternal = false
 $_canUseCheckout = true
 $_canUseForMultishipping = false
 $_isInitializeNeeded = true
 $_allowCurrencyCode = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD')


Detailed Description

PayPal Express Checkout Module

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 33 of file Express.php.


Member Function Documentation

_getExpressCheckoutDetails (  )  [protected]

Requesting details for current transaction and saving data

Definition at line 301 of file Express.php.

00302     {
00303         $api = $this->getApi();
00304         $api->setPayment($this->getPayment());
00305         if (!$api->callGetExpressCheckoutDetails()) {
00306             Mage::throwException(Mage::helper('paypal')->__('Problem during communication with PayPal'));
00307         }
00308         $q = $this->getQuote();
00309         $a = $api->getShippingAddress();
00310 
00311         $a->setCountryId(
00312             Mage::getModel('directory/country')->loadByCode($a->getCountry())->getId()
00313         );
00314         $a->setRegionId(
00315             Mage::getModel('directory/region')->loadByCode($a->getRegion(), $a->getCountryId())->getId()
00316         );
00317 
00318         /*
00319         we want to set the billing information
00320         only if the customer checkout from shortcut(shopping cart) or
00321         if the customer checkout from mark(one page) and guest
00322         */
00323 
00324         if ($this->getSession()->getExpressCheckoutMethod()=='shortcut'
00325         || ($this->getSession()->getExpressCheckoutMethod()!='shortcut' && $q->getCheckoutMethod()!=Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER)){
00326             $q->getBillingAddress()
00327                 ->setPrefix($a->getPrefix())
00328                 ->setFirstname($a->getFirstname())
00329                 ->setMiddlename($a->getMiddlename())
00330                 ->setLastname($a->getLastname())
00331                 ->setSuffix($a->getSuffix())
00332                 ->setEmail($a->getEmail());
00333         }
00334 
00335         $q->getShippingAddress()
00336             ->importCustomerAddress($a)
00337             ->setCollectShippingRates(true);
00338 
00339         //$q->setCheckoutMethod('paypal_express');
00340 
00341         $q->getPayment()
00342             ->setMethod('paypal_express')
00343             ->setPaypalCorrelationId($api->getCorrelationId())
00344             ->setPaypalPayerId($api->getPayerId())
00345             ->setPaypalPayerStatus($api->getPayerStatus())
00346             ->setAdditionalData($api->getPaypalPayerEmail())
00347         ;
00348 
00349         $q->collectTotals()->save();
00350 
00351     }

authorize ( Varien_Object payment,
amount 
)

Authorize

Parameters:
Varien_Object $orderPayment
Returns:
Mage_Payment_Model_Abstract

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 359 of file Express.php.

00360     {
00361         $this->placeOrder($payment);
00362         return $this;
00363     }

canUseForCurrency ( currencyCode  ) 

Check method for processing with base currency

Parameters:
string $currencyCode
Returns:
boolean

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 63 of file Express.php.

00064     {
00065         if (!in_array($currencyCode, $this->_allowCurrencyCode)) {
00066             return false;
00067         }
00068         return true;
00069     }

canUseForMultishipping (  ) 

Using for multiple shipping address

Returns:
bool

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 142 of file Express.php.

00143     {
00144         return false;
00145     }

canUseInternal (  ) 

Using internal pages for input payment data

Returns:
bool

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 132 of file Express.php.

00133     {
00134         return false;
00135     }

capture ( Varien_Object payment,
amount 
)

Capture payment

Parameters:
Varien_Object $orderPayment
Returns:
Mage_Payment_Model_Abstract

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 371 of file Express.php.

00372     {
00373         if ($payment->getCcTransId()) {
00374             $api = $this->getApi()
00375                 ->setPaymentType(Mage_Paypal_Model_Api_Nvp::PAYMENT_TYPE_SALE)
00376                 ->setAmount($amount)
00377                 ->setBillingAddress($payment->getOrder()->getBillingAddress())
00378                 ->setPayment($payment);
00379 
00380             $api->setAuthorizationId($payment->getCcTransId())
00381                 ->setCompleteType('NotComplete');
00382             $result = $api->callDoCapture()!==false;
00383 
00384             if ($result) {
00385                 $payment->setStatus('APPROVED');
00386                 //$payment->setCcTransId($api->getTransactionId());
00387                 $payment->setLastTransId($api->getTransactionId());
00388             } else {
00389                 $e = $api->getError();
00390                 if (isset($e['short_message'])) {
00391                     $message = $e['short_message'];
00392                 } else {
00393                     $message = Mage::helper('paypal')->__("Unknown PayPal API error: %s", $e['code']);
00394                 }
00395                 if (isset($e['long_message'])) {
00396                     $message .= ': '.$e['long_message'];
00397                 }
00398                 Mage::throwException($message);
00399             }
00400         } else {
00401             $this->placeOrder($payment);
00402         }
00403         return $this;
00404     }

catchError (  ) 

Processing error from paypal

Returns:
Mage_Paypal_Model_Express

Definition at line 152 of file Express.php.

00153     {
00154         if ($this->getApi()->getError()) {
00155             $s = $this->getCheckout();
00156             $e = $this->getApi()->getError();
00157             switch ($e['type']) {
00158                 case 'CURL':
00159                     $s->addError(Mage::helper('paypal')->__('There was an error connecting to the Paypal server: %s', $e['message']));
00160                     break;
00161 
00162                 case 'API':
00163                     $s->addError(Mage::helper('paypal')->__('There was an error during communication with Paypal: %s - %s', $e['short_message'], $e['long_message']));
00164                     break;
00165             }
00166         }
00167         return $this;
00168     }

createFormBlock ( name  ) 

Preparing form block

Parameters:
string $name
Returns:
Mage_Core_Block_Abstract

Definition at line 199 of file Express.php.

00200     {
00201         $block = $this->getLayout()->createBlock('paypal/express_form', $name)
00202             ->setMethod('paypal_express')
00203             ->setPayment($this->getPayment())
00204             ->setTemplate('paypal/express/form.phtml');
00205 
00206         return $block;
00207     }

createInfoBlock ( name  ) 

Preparing info block

Parameters:
string $name
Returns:
Mage_Core_Block_Abstract

Definition at line 215 of file Express.php.

00216     {
00217         $block = $this->getLayout()->createBlock('paypal/express_info', $name)
00218             ->setPayment($this->getPayment())
00219             ->setTemplate('paypal/express/info.phtml');
00220         return $block;
00221     }

getApi (  ) 

Get Paypal API Model

Returns:
Mage_Paypal_Model_Api_Nvp

Definition at line 76 of file Express.php.

00077     {
00078         return Mage::getSingleton('paypal/api_nvp');
00079     }

getCheckout (  ) 

Get checkout session namespace

Returns:
Mage_Checkout_Model_Session

Definition at line 96 of file Express.php.

00097     {
00098         return Mage::getSingleton('checkout/session');
00099     }

getCountryRegionId (  ) 

Definition at line 121 of file Express.php.

00122     {
00123         $a = $this->getApi()->getShippingAddress();
00124         return $this;
00125     }

getOrderPlaceRedirectUrl (  ) 

Definition at line 223 of file Express.php.

00224     {
00225         return $this->getRedirectUrl();
00226     }

getPaymentAction (  ) 

Getting paypal action url

Returns:
string

Definition at line 233 of file Express.php.

00234     {
00235         $paymentAction = $this->getConfigData('payment_action');
00236         if (!$paymentAction) {
00237             $paymentAction = Mage_Paypal_Model_Api_Nvp::PAYMENT_TYPE_AUTH;
00238         }
00239         return $paymentAction;
00240     }

getQuote (  ) 

Get current quote

Returns:
Mage_Sales_Model_Quote

Definition at line 106 of file Express.php.

00107     {
00108         return $this->getCheckout()->getQuote();
00109     }

getRedirectUrl (  ) 

Retrieve redirect url

Returns:
string

Definition at line 116 of file Express.php.

00117     {
00118         return $this->getApi()->getRedirectUrl();
00119     }

getSession (  ) 

Get paypal session namespace

Returns:
Mage_Paypal_Model_Session

Definition at line 86 of file Express.php.

00087     {
00088         return Mage::getSingleton('paypal/session');
00089     }

initialize ( paymentAction,
stateObject 
)

initialize payment transaction in case we doing checkout through onepage checkout

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 506 of file Express.php.

00507     {
00508         if ($this->getQuote()->isVirtual()) {
00509             $address = $this->getQuote()->getBillingAddress();
00510         } else {
00511             $address = $this->getQuote()->getShippingAddress();
00512         }
00513 
00514         $this->getApi()
00515             ->setPayment($this->getPayment())
00516             ->setPaymentType($paymentAction)
00517             ->setAmount($address->getBaseGrandTotal())
00518             ->setCurrencyCode($this->getQuote()->getBaseCurrencyCode())
00519             ->setShippingAddress($address)
00520             ->setInvNum($this->getQuote()->getReservedOrderId())
00521             ->callSetExpressCheckout();
00522 
00523         $this->throwError();
00524 
00525         $stateObject->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT);
00526         $stateObject->setStatus('pending_paypal');
00527         $stateObject->setIsNotified(false);
00528 
00529         Mage::getSingleton('paypal/session')->unsExpressCheckoutMethod();
00530 
00531         return $this;
00532     }

isInitializeNeeded (  ) 

Rewrite standard logic

Returns:
bool

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 539 of file Express.php.

00540     {
00541         return is_object(Mage::registry('_singleton/checkout/type_onepage'));
00542     }

placeOrder ( Varien_Object payment  ) 

Making right API call for current trasaction

Parameters:
Varien_Object $payment
Returns:
Mage_Paypal_Model_Express

Definition at line 412 of file Express.php.

00413     {
00414         $api = $this->getApi();
00415 
00416         $api->setAmount($payment->getOrder()->getBaseGrandTotal())
00417             ->setPayment($payment)
00418             ->setCurrencyCode($payment->getOrder()->getBaseCurrencyCode())
00419             ->setInvNum($this->getQuote()->getReservedOrderId());
00420 
00421         if ($api->callDoExpressCheckoutPayment()!==false) {
00422             $payment->setStatus('APPROVED')
00423                 ->setPayerId($api->getPayerId());
00424             if ($this->getPaymentAction()== Mage_Paypal_Model_Api_Nvp::PAYMENT_TYPE_AUTH) {
00425                 $payment->setCcTransId($api->getTransactionId());
00426             } else {
00427                 $payment->setLastTransId($api->getTransactionId());
00428             }
00429         } else {
00430             $e = $api->getError();
00431             die($e['short_message'].': '.$e['long_message']);
00432         }
00433         return $this;
00434     }

refund ( Varien_Object payment,
amount 
)

refund the amount with transaction id

public

Parameters:
string $payment Varien_Object object
Returns:
Mage_Payment_Model_Abstract

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 474 of file Express.php.

00475     {
00476         $error = false;
00477         if ($payment->getRefundTransactionId() && $amount>0) {
00478             $api = $this->getApi();
00479             //we can refund the amount full or partial so it is good to set up as partial refund
00480             $api->setPayment($payment)
00481                 ->setTransactionId($payment->getRefundTransactionId())
00482                 ->setRefundType(Mage_Paypal_Model_Api_Nvp::REFUND_TYPE_PARTIAL)
00483                 ->setAmount($amount);
00484 
00485             if ($api->callRefundTransaction()!==false){
00486                 $payment->setStatus('SUCCESS')
00487                     ->setCcTransId($api->getTransactionId());
00488             } else {
00489                 $e = $api->getError();
00490                 $error = $e['short_message'].': '.$e['long_message'];
00491             }
00492         }else{
00493             $error = Mage::helper('paypal')->__('Error in refunding the payment');
00494         }
00495 
00496         if ($error !== false) {
00497             Mage::throwException($error);
00498         }
00499         return $this;
00500     }

returnFromPaypal (  ) 

Making API call to check transaction's status when customer returns from paypal

Returns:
Mage_Paypal_Model_Express

Definition at line 270 of file Express.php.

00271     {
00272         $error = '';
00273 
00274         try {
00275             $this->_getExpressCheckoutDetails();
00276         } catch (Exception $e) {
00277             $error=$e->getMessage();
00278             $this->getSession()->addError($e->getMessage());
00279             $this->getApi()->setRedirectUrl('paypal/express/review');
00280         }
00281         switch ($this->getApi()->getUserAction()) {
00282             case Mage_Paypal_Model_Api_Nvp::USER_ACTION_CONTINUE:
00283                 $this->getApi()->setRedirectUrl(Mage::getUrl('paypal/express/review'));
00284                 break;
00285 
00286             case Mage_Paypal_Model_Api_Nvp::USER_ACTION_COMMIT:
00287                 if ($this->getSession()->getExpressCheckoutMethod() == 'shortcut') {
00288                     $this->getApi()->setRedirectUrl(Mage::getUrl('paypal/express/saveOrder'));
00289                 } else {
00290                     $this->getApi()->setRedirectUrl(Mage::getUrl('paypal/express/updateOrder'));
00291                 }
00292                 break;
00293         }
00294         return $this;
00295     }

shortcutSetExpressCheckout (  ) 

Making API call to start transaction from shopping cart

Returns:
Mage_Paypal_Model_Express

Definition at line 247 of file Express.php.

00248     {
00249         $this->getQuote()->reserveOrderId()->save();
00250         $this->getApi()
00251             ->setPayment($this->getPayment())
00252             ->setPaymentType($this->getPaymentAction())
00253             ->setAmount($this->getQuote()->getBaseGrandTotal())
00254             ->setCurrencyCode($this->getQuote()->getBaseCurrencyCode())
00255             ->setInvNum($this->getQuote()->getReservedOrderId())
00256             ->callSetExpressCheckout();
00257 
00258         $this->catchError();
00259 
00260         $this->getSession()->setExpressCheckoutMethod('shortcut');
00261 
00262         return $this;
00263     }

throwError (  ) 

Works same as catchError method but instead of saving error message in session throws exception

Returns:
Mage_Paypal_Model_Express

Definition at line 175 of file Express.php.

00176     {
00177         if ($this->getApi()->getError()) {
00178             $s = $this->getCheckout();
00179             $e = $this->getApi()->getError();
00180             switch ($e['type']) {
00181                 case 'CURL':
00182                     Mage::throwException(Mage::helper('paypal')->__('There was an error connecting to the Paypal server: %s', $e['message']));
00183                     break;
00184 
00185                 case 'API':
00186                     Mage::throwException(Mage::helper('paypal')->__('There was an error during communication with Paypal: %s - %s', $e['short_message'], $e['long_message']));
00187                     break;
00188             }
00189         }
00190         return $this;
00191     }

void ( Varien_Object payment  ) 

void

public

Parameters:
string $payment Varien_Object object
Returns:
Mage_Payment_Model_Abstract

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 443 of file Express.php.

00444     {
00445         $error = false;
00446         if($payment->getVoidTransactionId()){
00447             $api = $this->getApi();
00448             $api->setPayment($payment);
00449             $api->setAuthorizationId($payment->getVoidTransactionId());
00450 
00451              if ($api->callDoVoid()!==false){
00452                  $payment->setStatus('SUCCESS')
00453                     ->setCcTransId($api->getTransactionId());
00454              }else{
00455                $e = $api->getError();
00456                $error = $e['short_message'].': '.$e['long_message'];
00457              }
00458         }else{
00459             $error = Mage::helper('paypal')->__('Invalid transaction id');
00460         }
00461         if ($error !== false) {
00462             Mage::throwException($error);
00463         }
00464         return $this;
00465     }


Member Data Documentation

$_allowCurrencyCode = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD') [protected]

Definition at line 55 of file Express.php.

$_canAuthorize = true [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 43 of file Express.php.

$_canCapture = true [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 44 of file Express.php.

$_canCapturePartial = false [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 45 of file Express.php.

$_canRefund = false [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 46 of file Express.php.

$_canUseCheckout = true [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 49 of file Express.php.

$_canUseForMultishipping = false [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 50 of file Express.php.

$_canUseInternal = false [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 48 of file Express.php.

$_canVoid = true [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 47 of file Express.php.

$_code = 'paypal_express' [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 35 of file Express.php.

$_formBlockType = 'paypal/express_form' [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 36 of file Express.php.

$_infoBlockType = 'paypal/express_info' [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 37 of file Express.php.

$_isGateway = false [protected]

Availability options

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 42 of file Express.php.

$_isInitializeNeeded = true [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 53 of file Express.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:33 2009 for Magento by  doxygen 1.5.8