1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32:
33: class Mage_Paypal_Model_Direct extends Mage_Payment_Model_Method_Cc
34: {
35: protected $_code = Mage_Paypal_Model_Config::METHOD_WPP_DIRECT;
36: protected $_infoBlockType = 'paypal/payment_info';
37:
38: 39: 40:
41: protected $_isGateway = true;
42: protected $_canAuthorize = true;
43: protected $_canCapture = true;
44: protected $_canCapturePartial = true;
45: protected $_canRefund = true;
46: protected $_canRefundInvoicePartial = true;
47: protected $_canVoid = true;
48: protected $_canUseInternal = true;
49: protected $_canUseCheckout = true;
50: protected $_canUseForMultishipping = true;
51: protected $_canSaveCc = false;
52: protected $_canFetchTransactionInfo = true;
53: protected $_canReviewPayment = true;
54:
55: 56: 57: 58: 59:
60: protected $_pro = null;
61:
62: 63: 64: 65: 66:
67: protected $_proType = 'paypal/pro';
68:
69: public function __construct($params = array())
70: {
71: $proInstance = array_shift($params);
72: if ($proInstance && ($proInstance instanceof Mage_Paypal_Model_Pro)) {
73: $this->_pro = $proInstance;
74: } else {
75: $this->_pro = Mage::getModel($this->_proType);
76: }
77: $this->_pro->setMethod($this->_code);
78: }
79:
80: 81: 82: 83: 84: 85:
86: public function setStore($store)
87: {
88: $this->setData('store', $store);
89: if (null === $store) {
90: $store = Mage::app()->getStore()->getId();
91: }
92: $this->_pro->getConfig()->setStoreId(is_object($store) ? $store->getId() : $store);
93: return $this;
94: }
95:
96: 97: 98: 99: 100: 101:
102: public function canUseForCurrency($currencyCode)
103: {
104: return $this->_pro->getConfig()->isCurrencyCodeSupported($currencyCode);
105: }
106:
107: 108: 109: 110: 111: 112:
113: public function getConfigPaymentAction()
114: {
115: return $this->_pro->getConfig()->getPaymentAction();
116: }
117:
118: 119: 120: 121: 122:
123: public function getAllowedCcTypes()
124: {
125: $ccTypes = explode(',', $this->_pro->getConfig()->cctypes);
126: $country = $this->_pro->getConfig()->getMerchantCountry();
127:
128: if ($country == 'GB') {
129: $ccTypes = array_intersect(array('SM', 'SO', 'MC', 'DI', 'VI'), $ccTypes);
130: } elseif ($country == 'CA') {
131: $ccTypes = array_intersect(array('MC', 'VI'), $ccTypes);
132: }
133: return implode(',', $ccTypes);
134: }
135:
136: 137: 138: 139: 140:
141: public function isAvailable($quote = null)
142: {
143: if (parent::isAvailable($quote) && $this->_pro->getConfig()->isMethodAvailable()) {
144: return true;
145: }
146: return false;
147: }
148:
149: 150: 151: 152: 153: 154: 155:
156: public function getConfigData($field, $storeId = null)
157: {
158: $value = null;
159: switch ($field)
160: {
161: case 'cctypes':
162: $value = $this->getAllowedCcTypes();
163: break;
164: default:
165: $value = $this->_pro->getConfig()->$field;
166: }
167: return $value;
168: }
169:
170: 171: 172: 173: 174: 175:
176: public function authorize(Varien_Object $payment, $amount)
177: {
178: return $this->_placeOrder($payment, $amount);
179: }
180:
181: 182: 183: 184: 185: 186:
187: public function void(Varien_Object $payment)
188: {
189: $this->_pro->void($payment);
190: return $this;
191: }
192:
193: 194: 195: 196: 197: 198:
199: public function capture(Varien_Object $payment, $amount)
200: {
201: if (false === $this->_pro->capture($payment, $amount)) {
202: $this->_placeOrder($payment, $amount);
203: }
204: return $this;
205: }
206:
207: 208: 209: 210: 211: 212:
213: public function refund(Varien_Object $payment, $amount)
214: {
215: $this->_pro->refund($payment, $amount);
216: return $this;
217: }
218:
219: 220: 221: 222: 223: 224:
225: public function cancel(Varien_Object $payment)
226: {
227: $this->void($payment);
228:
229: return $this;
230: }
231:
232: 233: 234: 235: 236: 237:
238: public function canReviewPayment(Mage_Payment_Model_Info $payment)
239: {
240: return parent::canReviewPayment($payment) && $this->_pro->canReviewPayment($payment);
241: }
242:
243: 244: 245: 246: 247: 248:
249: public function acceptPayment(Mage_Payment_Model_Info $payment)
250: {
251: parent::acceptPayment($payment);
252: return $this->_pro->reviewPayment($payment, Mage_Paypal_Model_Pro::PAYMENT_REVIEW_ACCEPT);
253: }
254:
255: 256: 257: 258: 259: 260:
261: public function denyPayment(Mage_Payment_Model_Info $payment)
262: {
263: parent::denyPayment($payment);
264: return $this->_pro->reviewPayment($payment, Mage_Paypal_Model_Pro::PAYMENT_REVIEW_DENY);
265: }
266:
267: 268: 269: 270: 271:
272: public function getCentinelValidator()
273: {
274: $validator = parent::getCentinelValidator();
275: if (!$validator->getCustomApiEndpointUrl()) {
276: $validator->setCustomApiEndpointUrl($this->_pro->getConfig()->centinelDefaultApiUrl);
277: }
278: return $validator;
279: }
280:
281: 282: 283: 284: 285: 286: 287:
288: public function fetchTransactionInfo(Mage_Payment_Model_Info $payment, $transactionId)
289: {
290: return $this->_pro->fetchTransactionInfo($payment, $transactionId);
291: }
292:
293: 294: 295: 296: 297: 298: 299:
300: protected function _placeOrder(Mage_Sales_Model_Order_Payment $payment, $amount)
301: {
302: $order = $payment->getOrder();
303: $api = $this->_pro->getApi()
304: ->setPaymentAction($this->_pro->getConfig()->paymentAction)
305: ->setIpAddress(Mage::app()->getRequest()->getClientIp(false))
306: ->setAmount($amount)
307: ->setCurrencyCode($order->getBaseCurrencyCode())
308: ->setInvNum($order->getIncrementId())
309: ->setEmail($order->getCustomerEmail())
310: ->setNotifyUrl(Mage::getUrl('paypal/ipn/'))
311: ->setCreditCardType($payment->getCcType())
312: ->setCreditCardNumber($payment->getCcNumber())
313: ->setCreditCardExpirationDate(
314: $this->_getFormattedCcExpirationDate($payment->getCcExpMonth(), $payment->getCcExpYear())
315: )
316: ->setCreditCardCvv2($payment->getCcCid())
317: ->setMaestroSoloIssueNumber($payment->getCcSsIssue())
318: ;
319: if ($payment->getCcSsStartMonth() && $payment->getCcSsStartYear()) {
320: $year = sprintf('%02d', substr($payment->getCcSsStartYear(), -2, 2));
321: $api->setMaestroSoloIssueDate(
322: $this->_getFormattedCcExpirationDate($payment->getCcSsStartMonth(), $year)
323: );
324: }
325: if ($this->getIsCentinelValidationEnabled()) {
326: $this->getCentinelValidator()->exportCmpiData($api);
327: }
328:
329:
330: if ($order->getIsVirtual()) {
331: $api->setAddress($order->getBillingAddress())->setSuppressShipping(true);
332: } else {
333: $api->setAddress($order->getShippingAddress());
334: $api->setBillingAddress($order->getBillingAddress());
335: }
336:
337:
338: $api->setPaypalCart(Mage::getModel('paypal/cart', array($order)))
339: ->setIsLineItemsEnabled($this->_pro->getConfig()->lineItemsEnabled)
340: ;
341:
342:
343: $api->callDoDirectPayment();
344: $this->_importResultToPayment($api, $payment);
345:
346: try {
347: $api->callGetTransactionDetails();
348: } catch (Mage_Core_Exception $e) {
349:
350: $payment->setIsTransactionPending(true);
351: }
352: $this->_importResultToPayment($api, $payment);
353: return $this;
354: }
355:
356: 357: 358: 359: 360: 361: 362: 363:
364: protected function _getFormattedCcExpirationDate($month, $year)
365: {
366: return sprintf('%02d%02d', $month, $year);
367: }
368:
369: 370: 371: 372: 373: 374:
375: protected function _importResultToPayment($api, $payment)
376: {
377: $payment->setTransactionId($api->getTransactionId())->setIsTransactionClosed(0);
378: $this->_pro->importPaymentInfo($api, $payment);
379: }
380:
381: 382: 383: 384: 385: 386:
387: public function canVoid(Varien_Object $payment)
388: {
389: if ($payment instanceof Mage_Sales_Model_Order_Invoice
390: || $payment instanceof Mage_Sales_Model_Order_Creditmemo
391: ) {
392: return false;
393: }
394:
395: return $this->_canVoid;
396: }
397: }
398: