Mage_Paypal_Model_Standard Class Reference

Inheritance diagram for Mage_Paypal_Model_Standard:

Mage_Payment_Model_Method_Abstract Varien_Object

List of all members.

Public Member Functions

 canUseForCurrency ($currencyCode)
 getSession ()
 getCheckout ()
 getQuote ()
 canUseInternal ()
 canUseForMultishipping ()
 createFormBlock ($name)
 validate ()
 onOrderValidate (Mage_Sales_Model_Order_Payment $payment)
 onInvoiceCreate (Mage_Sales_Model_Invoice_Payment $payment)
 canCapture ()
 getOrderPlaceRedirectUrl ()
 getStandardCheckoutFormFields ()
 getPaypalUrl ()
 getDebug ()
 ipnPostSubmit ()
 isInitializeNeeded ()
 initialize ($paymentAction, $stateObject)

Public Attributes

const PAYMENT_TYPE_AUTH = 'AUTHORIZATION'
const PAYMENT_TYPE_SALE = 'SALE'

Protected Attributes

 $_code = 'paypal_standard'
 $_formBlockType = 'paypal/standard_form'
 $_allowCurrencyCode = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD')
 $_debugReplacePrivateDataKeys = array('business')


Detailed Description

PayPal Standard Checkout Module

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

Definition at line 33 of file Standard.php.


Member Function Documentation

canCapture (  ) 

Check capture availability

Returns:
bool

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 145 of file Standard.php.

00146     {
00147         return true;
00148     }

canUseForCurrency ( currencyCode  ) 

Check method for processing with base currency

Parameters:
string $currencyCode
Returns:
boolean

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 49 of file Standard.php.

00050     {
00051         if (!in_array($currencyCode, $this->_allowCurrencyCode)) {
00052             return false;
00053         }
00054         return true;
00055     }

canUseForMultishipping (  ) 

Using for multiple shipping address

Returns:
bool

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 109 of file Standard.php.

00110     {
00111         return false;
00112     }

canUseInternal (  ) 

Using internal pages for input payment data

Returns:
bool

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 99 of file Standard.php.

00100     {
00101         return false;
00102     }

createFormBlock ( name  ) 

Definition at line 114 of file Standard.php.

00115     {
00116         $block = $this->getLayout()->createBlock('paypal/standard_form', $name)
00117             ->setMethod('paypal_standard')
00118             ->setPayment($this->getPayment())
00119             ->setTemplate('paypal/standard/form.phtml');
00120 
00121         return $block;
00122     }

getCheckout (  ) 

Get checkout session namespace

Returns:
Mage_Checkout_Model_Session

Definition at line 79 of file Standard.php.

00080     {
00081         return Mage::getSingleton('checkout/session');
00082     }

getDebug (  ) 

Definition at line 321 of file Standard.php.

00322     {
00323         return Mage::getStoreConfig('paypal/wps/debug_flag');
00324     }

getOrderPlaceRedirectUrl (  ) 

Definition at line 150 of file Standard.php.

00151     {
00152           return Mage::getUrl('paypal/standard/redirect', array('_secure' => true));
00153     }

getPaypalUrl (  ) 

Definition at line 311 of file Standard.php.

00312     {
00313          if (Mage::getStoreConfig('paypal/wps/sandbox_flag')==1) {
00314              $url='https://www.sandbox.paypal.com/cgi-bin/webscr';
00315          } else {
00316              $url='https://www.paypal.com/cgi-bin/webscr';
00317          }
00318          return $url;
00319     }

getQuote (  ) 

Get current quote

Returns:
Mage_Sales_Model_Quote

Definition at line 89 of file Standard.php.

00090     {
00091         return $this->getCheckout()->getQuote();
00092     }

getSession (  ) 

Get paypal session namespace

Returns:
Mage_Paypal_Model_Session

Definition at line 69 of file Standard.php.

00070     {
00071         return Mage::getSingleton('paypal/session');
00072     }

getStandardCheckoutFormFields (  ) 

Definition at line 155 of file Standard.php.

00156     {
00157         if ($this->getQuote()->getIsVirtual()) {
00158             $a = $this->getQuote()->getBillingAddress();
00159             $b = $this->getQuote()->getShippingAddress();
00160         } else {
00161             $a = $this->getQuote()->getShippingAddress();
00162             $b = $this->getQuote()->getBillingAddress();
00163         }
00164         //getQuoteCurrencyCode
00165         $currency_code = $this->getQuote()->getBaseCurrencyCode();
00166         /*
00167         //we validate currency before sending paypal so following code is obsolete
00168 
00169         if (!in_array($currency_code,$this->_allowCurrencyCode)) {
00170             //if currency code is not allowed currency code, use USD as default
00171             $storeCurrency = Mage::getSingleton('directory/currency')
00172                 ->load($this->getQuote()->getStoreCurrencyCode());
00173             $amount = $storeCurrency->convert($amount, 'USD');
00174             $currency_code='USD';
00175         }
00176         */
00177 
00178         $sArr = array(
00179             'business'          => Mage::getStoreConfig('paypal/wps/business_account'),
00180             'return'            => Mage::getUrl('paypal/standard/success',array('_secure' => true)),
00181             'cancel_return'     => Mage::getUrl('paypal/standard/cancel',array('_secure' => false)),
00182             'notify_url'        => Mage::getUrl('paypal/standard/ipn'),
00183             'invoice'           => $this->getCheckout()->getLastRealOrderId(),
00184             'currency_code'     => $currency_code,
00185             'address_override'  => 1,
00186             'first_name'        => $a->getFirstname(),
00187             'last_name'         => $a->getLastname(),
00188             'address1'          => $a->getStreet(1),
00189             'address2'          => $a->getStreet(2),
00190             'city'              => $a->getCity(),
00191             'state'             => $a->getRegionCode(),
00192             'country'           => $a->getCountry(),
00193             'zip'               => $a->getPostcode(),
00194         );
00195 
00196         $logoUrl = Mage::getStoreConfig('paypal/wps/logo_url');
00197         if($logoUrl){
00198              $sArr = array_merge($sArr, array(
00199                   'cpp_header_image' => $logoUrl
00200              ));
00201         }
00202 
00203         if($this->getConfigData('payment_action')==self::PAYMENT_TYPE_AUTH){
00204              $sArr = array_merge($sArr, array(
00205                   'paymentaction' => 'authorization'
00206              ));
00207         }
00208 
00209         $transaciton_type = $this->getConfigData('transaction_type');
00210         /*
00211         O=aggregate cart amount to paypal
00212         I=individual items to paypal
00213         */
00214         if ($transaciton_type=='O') {
00215             $businessName = Mage::getStoreConfig('paypal/wps/business_name');
00216             $storeName = Mage::getStoreConfig('store/system/name');
00217             $amount = ($a->getBaseSubtotal()+$b->getBaseSubtotal())-($a->getBaseDiscountAmount()+$b->getBaseDiscountAmount());
00218             $sArr = array_merge($sArr, array(
00219                     'cmd'           => '_ext-enter',
00220                     'redirect_cmd'  => '_xclick',
00221                     'item_name'     => $businessName ? $businessName : $storeName,
00222                     'amount'        => sprintf('%.2f', $amount),
00223                 ));
00224             $_shippingTax = $this->getQuote()->getShippingAddress()->getBaseTaxAmount();
00225             $_billingTax = $this->getQuote()->getBillingAddress()->getBaseTaxAmount();
00226             $tax = sprintf('%.2f', $_shippingTax + $_billingTax);
00227             if ($tax>0) {
00228                   $sArr = array_merge($sArr, array(
00229                         'tax' => $tax
00230                   ));
00231             }
00232 
00233         } else {
00234             $sArr = array_merge($sArr, array(
00235                 'cmd'       => '_cart',
00236                 'upload'       => '1',
00237             ));
00238             $items = $this->getQuote()->getAllItems();
00239             if ($items) {
00240                 $i = 1;
00241                 foreach($items as $item){
00242                     if ($item->getParentItem()) {
00243                         continue;
00244                     }
00245                     //echo "<pre>"; print_r($item->getData()); echo"</pre>";
00246                     $sArr = array_merge($sArr, array(
00247                         'item_name_'.$i      => $item->getName(),
00248                         'item_number_'.$i      => $item->getSku(),
00249                         'quantity_'.$i      => $item->getQty(),
00250                         'amount_'.$i      => sprintf('%.2f', ($item->getBaseCalculationPrice() - $item->getBaseDiscountAmount())),
00251                     ));
00252                     if($item->getBaseTaxAmount()>0){
00253                         $sArr = array_merge($sArr, array(
00254                         'tax_'.$i      => sprintf('%.2f',$item->getBaseTaxAmount()/$item->getQty()),
00255                         ));
00256                     }
00257                     $i++;
00258                 }
00259            }
00260         }
00261 
00262         $totalArr = $a->getTotals();
00263         $shipping = sprintf('%.2f', $this->getQuote()->getShippingAddress()->getBaseShippingAmount());
00264         if ($shipping>0 && !$this->getQuote()->getIsVirtual()) {
00265           if ($transaciton_type=='O') {
00266               $sArr = array_merge($sArr, array(
00267                     'shipping' => $shipping
00268               ));
00269           } else {
00270               $shippingTax = $this->getQuote()->getShippingAddress()->getBaseShippingTaxAmount();
00271               $sArr = array_merge($sArr, array(
00272                     'item_name_'.$i   => $totalArr['shipping']->getTitle(),
00273                     'quantity_'.$i    => 1,
00274                     'amount_'.$i      => sprintf('%.2f',$shipping),
00275                     'tax_'.$i         => sprintf('%.2f',$shippingTax),
00276               ));
00277               $i++;
00278           }
00279         }
00280 
00281         $sReq = '';
00282         $sReqDebug = '';
00283         $rArr = array();
00284 
00285 
00286         foreach ($sArr as $k=>$v) {
00287             /*
00288             replacing & char with and. otherwise it will break the post
00289             */
00290             $value =  str_replace("&","and",$v);
00291             $rArr[$k] =  $value;
00292             $sReq .= '&'.$k.'='.$value;
00293             $sReqDebug .= '&'.$k.'=';
00294             if (in_array($k, $this->_debugReplacePrivateDataKeys)) {
00295                 $sReqDebug .= '***';
00296             } else  {
00297                 $sReqDebug .= $value;
00298             }
00299         }
00300 
00301         if ($this->getDebug() && $sReq) {
00302             $sReq = substr($sReq, 1);
00303             $debug = Mage::getModel('paypal/api_debug')
00304                     ->setApiEndpoint($this->getPaypalUrl())
00305                     ->setRequestBody($sReq)
00306                     ->save();
00307         }
00308         return $rArr;
00309     }

initialize ( paymentAction,
stateObject 
)

Method that will be executed instead of authorize or capture if flag isInitilizeNeeded set to true

Parameters:
string $paymentAction
Returns:
Mage_Payment_Model_Abstract

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 487 of file Standard.php.

00488     {
00489         $state = Mage_Sales_Model_Order::STATE_PENDING_PAYMENT;
00490         $stateObject->setState($state);
00491         $stateObject->setStatus(Mage::getSingleton('sales/order_config')->getStateDefaultStatus($state));
00492         $stateObject->setIsNotified(false);
00493     }

ipnPostSubmit (  ) 

Definition at line 327 of file Standard.php.

00328     {
00329         $sReq = '';
00330         $sReqDebug = '';
00331         foreach($this->getIpnFormData() as $k=>$v) {
00332             $sReq .= '&'.$k.'='.urlencode(stripslashes($v));
00333             $sReqDebug .= '&'.$k.'=';
00334 
00335         }
00336         //append ipn commdn
00337         $sReq .= "&cmd=_notify-validate";
00338         $sReq = substr($sReq, 1);
00339 
00340         if ($this->getDebug()) {
00341             $debug = Mage::getModel('paypal/api_debug')
00342                     ->setApiEndpoint($this->getPaypalUrl())
00343                     ->setRequestBody($sReq)
00344                     ->save();
00345         }
00346         $http = new Varien_Http_Adapter_Curl();
00347         $http->write(Zend_Http_Client::POST,$this->getPaypalUrl(), '1.1', array(), $sReq);
00348         $response = $http->read();
00349         $response = preg_split('/^\r?$/m', $response, 2);
00350         $response = trim($response[1]);
00351         if ($this->getDebug()) {
00352             $debug->setResponseBody($response)->save();
00353         }
00354 
00355          //when verified need to convert order into invoice
00356         $id = $this->getIpnFormData('invoice');
00357         $order = Mage::getModel('sales/order');
00358         $order->loadByIncrementId($id);
00359 
00360         if ($response=='VERIFIED') {
00361             if (!$order->getId()) {
00362                 /*
00363                 * need to have logic when there is no order with the order id from paypal
00364                 */
00365 
00366             } else {
00367 
00368                 if ($this->getIpnFormData('mc_gross')!=$order->getBaseGrandTotal()) {
00369                     //when grand total does not equal, need to have some logic to take care
00370                     $order->addStatusToHistory(
00371                         $order->getStatus(),//continue setting current order status
00372                         Mage::helper('paypal')->__('Order total amount does not match paypal gross total amount')
00373                     );
00374                     $order->save();
00375                 } else {
00376                     /*
00377                     //quote id
00378                     $quote_id = $order->getQuoteId();
00379                     //the customer close the browser or going back after submitting payment
00380                     //so the quote is still in session and need to clear the session
00381                     //and send email
00382                     if ($this->getQuote() && $this->getQuote()->getId()==$quote_id) {
00383                         $this->getCheckout()->clear();
00384                         $order->sendNewOrderEmail();
00385                     }
00386                     */
00387 
00388                     // get from config order status to be set
00389                     $newOrderStatus = $this->getConfigData('order_status', $order->getStoreId());
00390                     if (empty($newOrderStatus)) {
00391                         $newOrderStatus = $order->getStatus();
00392                     }
00393 
00394                     /*
00395                     if payer_status=verified ==> transaction in sale mode
00396                     if transactin in sale mode, we need to create an invoice
00397                     otherwise transaction in authorization mode
00398                     */
00399                     if ($this->getIpnFormData('payment_status') == 'Completed') {
00400                        if (!$order->canInvoice()) {
00401                            //when order cannot create invoice, need to have some logic to take care
00402                            $order->addStatusToHistory(
00403                                 $order->getStatus(), // keep order status/state
00404                                 Mage::helper('paypal')->__('Error in creating an invoice', true),
00405                                 $notified = true
00406                            );
00407 
00408                        } else {
00409                            //need to save transaction id
00410                            $order->getPayment()->setTransactionId($this->getIpnFormData('txn_id'));
00411                            //need to convert from order into invoice
00412                            $invoice = $order->prepareInvoice();
00413                            $invoice->register()->capture();
00414                            Mage::getModel('core/resource_transaction')
00415                                ->addObject($invoice)
00416                                ->addObject($invoice->getOrder())
00417                                ->save();
00418                            $order->setState(
00419                                Mage_Sales_Model_Order::STATE_PROCESSING, $newOrderStatus,
00420                                Mage::helper('paypal')->__('Invoice #%s created', $invoice->getIncrementId()),
00421                                $notified = true
00422                            );
00423                        }
00424                     } else {
00425                         $order->setState(
00426                             Mage_Sales_Model_Order::STATE_PROCESSING, $newOrderStatus,
00427                             Mage::helper('paypal')->__('Received IPN verification'),
00428                             $notified = true
00429                         );
00430                     }
00431 
00432                     $ipnCustomerNotified = true;
00433                     if (!$order->getPaypalIpnCustomerNotified()) {
00434                         $ipnCustomerNotified = false;
00435                         $order->setPaypalIpnCustomerNotified(1);
00436                     }
00437 
00438                     $order->save();
00439 
00440                     if (!$ipnCustomerNotified) {
00441                         $order->sendNewOrderEmail();
00442                     }
00443 
00444                 }//else amount the same and there is order obj
00445                 //there are status added to order
00446             }
00447         }else{
00448             /*
00449             Canceled_Reversal
00450             Completed
00451             Denied
00452             Expired
00453             Failed
00454             Pending
00455             Processed
00456             Refunded
00457             Reversed
00458             Voided
00459             */
00460             $payment_status= $this->getIpnFormData('payment_status');
00461             $comment = $payment_status;
00462             if ($payment_status == 'Pending') {
00463                 $comment .= ' - ' . $this->getIpnFormData('pending_reason');
00464             } elseif ( ($payment_status == 'Reversed') || ($payment_status == 'Refunded') ) {
00465                 $comment .= ' - ' . $this->getIpnFormData('reason_code');
00466             }
00467             //response error
00468             if (!$order->getId()) {
00469                 /*
00470                 * need to have logic when there is no order with the order id from paypal
00471                 */
00472             } else {
00473                 $order->addStatusToHistory(
00474                     $order->getStatus(),//continue setting current order status
00475                     Mage::helper('paypal')->__('Paypal IPN Invalid %s.', $comment)
00476                 );
00477                 $order->save();
00478             }
00479         }
00480     }

isInitializeNeeded (  ) 

flag if we need to run payment initialize while order place

Returns:
bool

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 482 of file Standard.php.

00483     {
00484         return true;
00485     }

onInvoiceCreate ( Mage_Sales_Model_Invoice_Payment $  payment  ) 

Definition at line 140 of file Standard.php.

00141     {
00142 
00143     }

onOrderValidate ( Mage_Sales_Model_Order_Payment payment  ) 

Definition at line 135 of file Standard.php.

00136     {
00137        return $this;
00138     }

validate (  ) 

Validate payment method information object

Parameters:
Varien_Object $info
Returns:
Mage_Payment_Model_Abstract

to validate paymene method is allowed for billing country or not

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 125 of file Standard.php.

00126     {
00127         parent::validate();
00128         $currency_code = $this->getQuote()->getBaseCurrencyCode();
00129         if (!in_array($currency_code,$this->_allowCurrencyCode)) {
00130             Mage::throwException(Mage::helper('paypal')->__('Selected currency code ('.$currency_code.') is not compatible with PayPal'));
00131         }
00132         return $this;
00133     }


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 41 of file Standard.php.

$_code = 'paypal_standard' [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 39 of file Standard.php.

$_debugReplacePrivateDataKeys = array('business') [protected]

Definition at line 62 of file Standard.php.

$_formBlockType = 'paypal/standard_form' [protected]

Reimplemented from Mage_Payment_Model_Method_Abstract.

Definition at line 40 of file Standard.php.

const PAYMENT_TYPE_AUTH = 'AUTHORIZATION'

Definition at line 36 of file Standard.php.

const PAYMENT_TYPE_SALE = 'SALE'

Definition at line 37 of file Standard.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