00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class Mage_PaypalUk_Model_Express extends Mage_Payment_Model_Method_Abstract
00034 {
00035 protected $_code = 'paypaluk_express';
00036 protected $_formBlockType = 'paypaluk/express_form';
00037 protected $_infoBlockType = 'paypaluk/express_info';
00038
00039
00040
00041
00042 protected $_isGateway = false;
00043 protected $_canAuthorize = true;
00044 protected $_canCapture = true;
00045 protected $_canCapturePartial = false;
00046 protected $_canRefund = false;
00047 protected $_canVoid = true;
00048 protected $_canUseInternal = false;
00049 protected $_canUseCheckout = true;
00050 protected $_canUseForMultishipping = false;
00051
00052
00053 protected $_isInitializeNeeded = true;
00054
00055 protected $_allowCurrencyCode = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD');
00056
00057
00058
00059
00060
00061
00062
00063 public function canUseForCurrency($currencyCode)
00064 {
00065 if (!in_array($currencyCode, $this->_allowCurrencyCode)) {
00066 return false;
00067 }
00068 return true;
00069 }
00070
00071
00072
00073
00074
00075
00076 public function getApi()
00077 {
00078 return Mage::getSingleton('paypalUk/api_pro');
00079 }
00080
00081
00082
00083
00084
00085
00086 public function getRedirectUrl()
00087 {
00088 return $this->getApi()->getRedirectUrl();
00089 }
00090
00091
00092
00093
00094
00095
00096 public function getSession()
00097 {
00098 return Mage::getSingleton('paypaluk/session');
00099 }
00100
00101
00102
00103
00104
00105
00106 public function getCheckout()
00107 {
00108 return Mage::getSingleton('checkout/session');
00109 }
00110
00111
00112
00113
00114
00115
00116 public function getQuote()
00117 {
00118 return $this->getCheckout()->getQuote();
00119 }
00120
00121
00122
00123
00124
00125
00126 public function getPaymentAction($paymentAction=null)
00127 {
00128 if (is_null($paymentAction)) {
00129 $paymentAction = $this->getConfigData('payment_action');
00130 }
00131 if (!$paymentAction) {
00132 $paymentAction = Mage_PaypalUk_Model_Api_Pro::TRXTYPE_AUTH_ONLY;
00133 } else {
00134 if ($paymentAction==Mage_PaypalUk_Model_Api_Abstract::PAYMENT_TYPE_AUTH) {
00135 $paymentAction = Mage_PaypalUk_Model_Api_Pro::TRXTYPE_AUTH_ONLY;
00136 } else {
00137 $paymentAction = Mage_PaypalUk_Model_Api_Pro::TRXTYPE_SALE;
00138 }
00139 }
00140 return $paymentAction;
00141 }
00142
00143
00144
00145
00146 public function getOrderPlaceRedirectUrl()
00147 {
00148 return $this->getRedirectUrl();
00149 }
00150
00151
00152
00153
00154 public function createFormBlock($name)
00155 {
00156 $block = $this->getLayout()->createBlock('paypaluk/express_form', $name)
00157 ->setMethod('paypaluk_express')
00158 ->setPayment($this->getPayment())
00159 ->setTemplate('paypaluk/express/form.phtml');
00160
00161 return $block;
00162 }
00163
00164
00165
00166
00167
00168 public function shortcutSetExpressCheckout()
00169 {
00170 $this->getQuote()->reserveOrderId();
00171 $this->getApi()
00172 ->setPayment($this->getPayment())
00173 ->setTrxtype($this->getPaymentAction())
00174 ->setAmount($this->getQuote()->getBaseGrandTotal())
00175 ->setCurrencyCode($this->getQuote()->getBaseCurrencyCode())
00176 ->callSetExpressCheckout();
00177
00178 $this->catchError();
00179
00180 $this->getSession()->setExpressCheckoutMethod('shortcut');
00181
00182 return $this;
00183 }
00184
00185
00186
00187
00188 public function catchError()
00189 {
00190 if ($this->getApi()->hasError() || !$this->getRedirectUrl()) {
00191 $s = $this->getCheckout();
00192 $e = $this->getApi()->getError();
00193 $s->addError(Mage::helper('paypalUk')->__('There was an error connecting to the Paypal server: %s', $e['message']));
00194 $this->getApi()->setRedirectUrl(Mage::getUrl('checkout/cart'));
00195 }
00196 return $this;
00197 }
00198
00199
00200
00201
00202
00203
00204
00205 public function throwError()
00206 {
00207 if ($this->getApi()->hasError() || !$this->getRedirectUrl()) {
00208 $s = $this->getCheckout();
00209 $e = $this->getApi()->getError();
00210 Mage::throwException(Mage::helper('paypalUk')->__('There was an error connecting to the Paypal server: %s', $e['message']));
00211 }
00212 return $this;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221 public function returnFromPaypal()
00222 {
00223 $error='';
00224 try {
00225 $this->_getExpressCheckoutDetails();
00226 } catch (Exception $e) {
00227 $error=$e->getMessage();
00228 $this->getSession()->addError($e->getMessage());
00229 $this->_redirect('paypaluk/express/review');
00230 }
00231
00232 switch ($this->getApi()->getUserAction()) {
00233 case Mage_Paypal_Model_Api_Nvp::USER_ACTION_CONTINUE:
00234 $this->getApi()->setRedirectUrl(Mage::getUrl('paypaluk/express/review'));
00235 break;
00236 case Mage_Paypal_Model_Api_Nvp::USER_ACTION_COMMIT:
00237 if ($this->getSession()->getExpressCheckoutMethod() == 'shortcut') {
00238 $this->getApi()->setRedirectUrl(Mage::getUrl('paypaluk/express/saveOrder'));
00239 } else {
00240 $this->getApi()->setRedirectUrl(Mage::getUrl('paypaluk/express/updateOrder'));
00241 }
00242
00243 break;
00244 }
00245 return $this;
00246 }
00247
00248
00249
00250
00251 protected function _getExpressCheckoutDetails()
00252 {
00253 $api = $this->getApi()
00254 ->setPayment($this->getPayment())
00255 ->setTrxtype($this->getPaymentAction());
00256
00257 if ($api->callGetExpressCheckoutDetails()===false) {
00258
00259 Mage::throwException(Mage::helper('paypalUk')->__('There has been an error processing your payment. Please try later or contact us for help.'));
00260 }
00261
00262 $q = $this->getQuote();
00263 $a = $api->getShippingAddress();
00264
00265 $a->setCountryId(
00266 Mage::getModel('directory/country')->loadByCode($a->getCountry())->getId()
00267 );
00268 $a->setRegionId(
00269 Mage::getModel('directory/region')->loadByCode($a->getRegion(), $a->getCountryId())->getId()
00270 );
00271
00272
00273
00274
00275
00276
00277 if ($this->getSession()->getExpressCheckoutMethod()=='shortcut' ||
00278 ($this->getSession()->getExpressCheckoutMethod()!='shortcut' && $q->getCheckoutMethod()!=Mage_Sales_Model_Quote::CHECKOUT_METHOD_REGISTER)){
00279 $q->getBillingAddress()
00280 ->setFirstname($a->getFirstname())
00281 ->setLastname($a->getLastname())
00282 ->setEmail($a->getEmail());
00283 }
00284
00285 $q->getShippingAddress()
00286 ->importCustomerAddress($a)
00287 ->setCollectShippingRates(true);
00288
00289
00290
00291 $q->getPayment()
00292 ->setMethod('paypaluk_express')
00293 ->setPaypalCorrelationId($api->getCorrelationId())
00294 ->setPaypalPayerId($api->getPayerId())
00295 ->setPaypalPayerStatus($api->getPayerStatus())
00296 ->setAdditionalData($api->getPaypalPayerEmail())
00297 ;
00298
00299 $q->collectTotals()->save();
00300 }
00301
00302 public function placeOrder(Varien_Object $payment)
00303 {
00304 $api = $this->getApi()
00305 ->setPayment($payment)
00306 ->setAmount($payment->getOrder()->getBaseGrandTotal())
00307 ->setTrxtype($this->getPaymentAction())
00308 ->setCurrencyCode($payment->getOrder()->getBaseCurrencyCode());
00309
00310 if ($api->callDoExpressCheckoutPayment()!==false) {
00311 $payment->setStatus('APPROVED')
00312 ->setPayerId($api->getPayerId());
00313 if ($this->getPaymentAction()==Mage_PaypalUk_Model_Api_Pro::TRXTYPE_AUTH_ONLY) {
00314 $payment->setCcTransId($api->getTransactionId());
00315 } else {
00316 $payment->setLastTransId($api->getTransactionId());
00317 }
00318 } else {
00319 $e = $api->getError();
00320 die($e['message']);
00321 }
00322 return $this;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332 public function authorize(Varien_Object $payment, $amount)
00333 {
00334 $this->placeOrder($payment);
00335 return $this;
00336 }
00337
00338
00339
00340
00341
00342
00343
00344 public function capture(Varien_Object $payment, $amount)
00345 {
00346 if ($payment->getCcTransId()) {
00347 $trxType=Mage_PaypalUk_Model_Api_Pro::TRXTYPE_DELAYED_CAPTURE;
00348 $api = $this->getApi()
00349 ->setTrxtype($trxType)
00350 ->setAmount($amount)
00351 ->setTransactionId($payment->getCcTransId())
00352 ->setBillingAddress($payment->getOrder()->getBillingAddress())
00353 ->setPayment($payment);
00354
00355 if ($api->callDoDirectPayment()!==false) {
00356 $payment
00357 ->setStatus('APPROVED')
00358 ->setPaymentStatus('CAPTURE')
00359
00360 ->setLastTransId($api->getTransactionId())
00361 ->setCcAvsStatus($api->getAvsCode())
00362 ->setCcCidStatus($api->getCvv2Match());
00363 } else {
00364 $e = $api->getError();
00365 Mage::throwException($e['message']?$e['message']:Mage::helper('paypalUk')->__('Error in capture payment'));
00366 }
00367 } else {
00368 $this->placeOrder($payment);
00369 }
00370 return $this;
00371 }
00372
00373 public function canVoid(Varien_Object $payment)
00374 {
00375 if ($payment->getCcTransId()) {
00376 $api = $this->getApi()
00377 ->setTransactionId($payment->getCcTransId())
00378 ->setPayment($payment);
00379 if ($api->canVoid()!==false) {
00380 $payment->setStatus(self::STATUS_VOID);
00381 } else {
00382 $e = $api->getError();
00383 $payment->setStatus(self::STATUS_ERROR);
00384 $payment->setStatusDescription($e['message']);
00385 }
00386 } else {
00387 $payment->setStatus(self::STATUS_ERROR);
00388 $payment->setStatusDescription(Mage::helper('paypalUk')->__('Invalid transaction id'));
00389 }
00390 return $this;
00391 }
00392
00393 public function void(Varien_Object $payment)
00394 {
00395 $error = false;
00396 if ($payment->getVoidTransactionId()) {
00397 $api = $this->getApi()
00398 ->setTransactionId($payment->getVoidTransactionId())
00399 ->setPayment($payment);
00400
00401 if ($api->void()!==false) {
00402 $payment->setCcTransId($api->getTransactionId());
00403 $payment->setStatus(self::STATUS_VOID);
00404 } else {
00405 $e = $api->getError();
00406 $error = $e['message'];
00407 }
00408 } else {
00409 $error = Mage::helper('paypalUk')->__('Invalid transaction id');
00410 }
00411 if ($error !== false) {
00412 Mage::throwException($error);
00413 }
00414 return $this;
00415 }
00416
00417 public function refund(Varien_Object $payment, $amount)
00418 {
00419 $error = false;
00420 if (($payment->getRefundTransactionId() && $amount>0)) {
00421 $api = $this->getApi()
00422 ->setTransactionId($payment->getRefundTransactionId())
00423 ->setPayment($payment)
00424 ->setAmount($amount);
00425 if ($api->refund()!==false) {
00426 $payment->setCcTransId($api->getTransactionId());
00427 $payment->setStatus(self::STATUS_SUCCESS);
00428 } else {
00429 $e = $api->getError();
00430 $error = $e['message'];
00431 }
00432
00433 } else {
00434 $error = Mage::helper('paypalUk')->__('Error in refunding the payment');
00435 }
00436 if ($error !== false) {
00437 Mage::throwException($error);
00438 }
00439 return $this;
00440 }
00441
00442
00443
00444
00445
00446 public function initialize($paymentAction, $stateObject)
00447 {
00448 if ($this->getQuote()->isVirtual()) {
00449 $address = $this->getQuote()->getBillingAddress();
00450 } else {
00451 $address = $this->getQuote()->getShippingAddress();
00452 }
00453
00454 $this->getApi()
00455 ->setPayment($this->getPayment())
00456 ->setTrxtype($this->getPaymentAction($paymentAction))
00457 ->setAmount($address->getBaseGrandTotal())
00458 ->setCurrencyCode($this->getQuote()->getBaseCurrencyCode())
00459 ->setShippingAddress($address)
00460 ->setInvNum($this->getQuote()->getReservedOrderId())
00461 ->callSetExpressCheckout();
00462
00463 $this->throwError();
00464
00465 $stateObject->setState(Mage_Sales_Model_Order::STATE_PENDING_PAYMENT);
00466 $stateObject->setStatus('pending_paypal');
00467 $stateObject->setIsNotified(false);
00468
00469 Mage::getSingleton('paypal/session')->unsExpressCheckoutMethod();
00470
00471 return $this;
00472 }
00473
00474
00475
00476
00477
00478
00479 public function isInitializeNeeded()
00480 {
00481 return is_object(Mage::registry('_singleton/checkout/type_onepage'));
00482 }
00483 }