1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Paypal
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: *
29: * PayPal Standard Checkout Module
30: *
31: * @author Magento Core Team <core@magentocommerce.com>
32: */
33: class Mage_Paypal_Model_Standard extends Mage_Payment_Model_Method_Abstract
34: {
35: protected $_code = Mage_Paypal_Model_Config::METHOD_WPS;
36: protected $_formBlockType = 'paypal/standard_form';
37: protected $_infoBlockType = 'paypal/payment_info';
38: protected $_isInitializeNeeded = true;
39: protected $_canUseInternal = false;
40: protected $_canUseForMultishipping = false;
41:
42: /**
43: * Config instance
44: * @var Mage_Paypal_Model_Config
45: */
46: protected $_config = null;
47:
48: /**
49: * Whether method is available for specified currency
50: *
51: * @param string $currencyCode
52: * @return bool
53: */
54: public function canUseForCurrency($currencyCode)
55: {
56: return $this->getConfig()->isCurrencyCodeSupported($currencyCode);
57: }
58:
59: /**
60: * Get paypal session namespace
61: *
62: * @return Mage_Paypal_Model_Session
63: */
64: public function getSession()
65: {
66: return Mage::getSingleton('paypal/session');
67: }
68:
69: /**
70: * Get checkout session namespace
71: *
72: * @return Mage_Checkout_Model_Session
73: */
74: public function getCheckout()
75: {
76: return Mage::getSingleton('checkout/session');
77: }
78:
79: /**
80: * Get current quote
81: *
82: * @return Mage_Sales_Model_Quote
83: */
84: public function getQuote()
85: {
86: return $this->getCheckout()->getQuote();
87: }
88:
89: /**
90: * Create main block for standard form
91: *
92: */
93: public function createFormBlock($name)
94: {
95: $block = $this->getLayout()->createBlock('paypal/standard_form', $name)
96: ->setMethod('paypal_standard')
97: ->setPayment($this->getPayment())
98: ->setTemplate('paypal/standard/form.phtml');
99:
100: return $block;
101: }
102:
103: /**
104: * Return Order place redirect url
105: *
106: * @return string
107: */
108: public function getOrderPlaceRedirectUrl()
109: {
110: return Mage::getUrl('paypal/standard/redirect', array('_secure' => true));
111: }
112:
113: /**
114: * Return form field array
115: *
116: * @return array
117: */
118: public function getStandardCheckoutFormFields()
119: {
120: $orderIncrementId = $this->getCheckout()->getLastRealOrderId();
121: $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
122: /* @var $api Mage_Paypal_Model_Api_Standard */
123: $api = Mage::getModel('paypal/api_standard')->setConfigObject($this->getConfig());
124: $api->setOrderId($orderIncrementId)
125: ->setCurrencyCode($order->getBaseCurrencyCode())
126: //->setPaymentAction()
127: ->setOrder($order)
128: ->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
129: ->setReturnUrl(Mage::getUrl('paypal/standard/success'))
130: ->setCancelUrl(Mage::getUrl('paypal/standard/cancel'));
131:
132: // export address
133: $isOrderVirtual = $order->getIsVirtual();
134: $address = $isOrderVirtual ? $order->getBillingAddress() : $order->getShippingAddress();
135: if ($isOrderVirtual) {
136: $api->setNoShipping(true);
137: } elseif ($address->validate()) {
138: $api->setAddress($address);
139: }
140:
141: // add cart totals and line items
142: $api->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
143: ->setIsLineItemsEnabled($this->_config->lineItemsEnabled)
144: ;
145: $api->setCartSummary($this->_getAggregatedCartSummary());
146: $api->setLocale($api->getLocaleCode());
147: $result = $api->getStandardCheckoutRequest();
148: return $result;
149: }
150:
151: /**
152: * Instantiate state and set it to state object
153: * @param string $paymentAction
154: * @param Varien_Object
155: */
156: public function initialize($paymentAction, $stateObject)
157: {
158: $state = Mage_Sales_Model_Order::STATE_PENDING_PAYMENT;
159: $stateObject->setState($state);
160: $stateObject->setStatus('pending_payment');
161: $stateObject->setIsNotified(false);
162: }
163:
164: /**
165: * Config instance getter
166: * @return Mage_Paypal_Model_Config
167: */
168: public function getConfig()
169: {
170: if (null === $this->_config) {
171: $params = array($this->_code);
172: if ($store = $this->getStore()) {
173: $params[] = is_object($store) ? $store->getId() : $store;
174: }
175: $this->_config = Mage::getModel('paypal/config', $params);
176: }
177: return $this->_config;
178: }
179:
180: /**
181: * Check whether payment method can be used
182: * @param Mage_Sales_Model_Quote
183: * @return bool
184: */
185: public function isAvailable($quote = null)
186: {
187: if (parent::isAvailable($quote) && $this->getConfig()->isMethodAvailable()) {
188: return true;
189: }
190: return false;
191: }
192:
193: /**
194: * Custom getter for payment configuration
195: *
196: * @param string $field
197: * @param int $storeId
198: * @return mixed
199: */
200: public function getConfigData($field, $storeId = null)
201: {
202: return $this->getConfig()->$field;
203: }
204:
205: /**
206: * Aggregated cart summary label getter
207: *
208: * @return string
209: */
210: private function _getAggregatedCartSummary()
211: {
212: if ($this->_config->lineItemsSummary) {
213: return $this->_config->lineItemsSummary;
214: }
215: return Mage::app()->getStore($this->getStore())->getFrontendName();
216: }
217: }
218: