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_Paypal_Model_Direct extends Mage_Payment_Model_Method_Cc
00034 {
00035 protected $_code = 'paypal_direct';
00036
00037
00038
00039
00040 protected $_isGateway = true;
00041 protected $_canAuthorize = true;
00042 protected $_canCapture = true;
00043 protected $_canCapturePartial = false;
00044 protected $_canRefund = false;
00045 protected $_canVoid = true;
00046 protected $_canUseInternal = true;
00047 protected $_canUseCheckout = true;
00048 protected $_canUseForMultishipping = true;
00049 protected $_canSaveCc = false;
00050
00051 protected $_allowCurrencyCode = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD');
00052
00053
00054
00055
00056
00057
00058
00059 public function canUseForCurrency($currencyCode)
00060 {
00061 if (!in_array($currencyCode, $this->_allowCurrencyCode)) {
00062 return false;
00063 }
00064 return true;
00065 }
00066
00067
00068
00069
00070
00071
00072 public function getApi()
00073 {
00074 return Mage::getSingleton('paypal/api_nvp');
00075 }
00076
00077
00078
00079
00080
00081
00082 public function getSession()
00083 {
00084 return Mage::getSingleton('paypal/session');
00085 }
00086
00087
00088
00089
00090
00091
00092 public function getCheckout()
00093 {
00094 return Mage::getSingleton('checkout/session');
00095 }
00096
00097
00098
00099
00100
00101
00102 public function getQuote()
00103 {
00104 return $this->getCheckout()->getQuote();
00105 }
00106
00107 public function getRedirectUrl()
00108 {
00109 return $this->getApi()->getRedirectUrl();
00110 }
00111
00112 public function getCountryRegionId()
00113 {
00114 $a = $this->getApi()->getShippingAddress();
00115 return $this;
00116 }
00117
00118 public function getPaymentAction()
00119 {
00120 $paymentAction = $this->getConfigData('payment_action');
00121 if (!$paymentAction) {
00122 $paymentAction = Mage_Paypal_Model_Api_Nvp::PAYMENT_TYPE_AUTH;
00123 }
00124 return $paymentAction;
00125 }
00126
00127 public function authorize(Varien_Object $payment, $amount)
00128 {
00129 $api = $this->getApi()
00130 ->setPaymentType($this->getPaymentAction())
00131 ->setAmount($amount)
00132 ->setBillingAddress($payment->getOrder()->getBillingAddress())
00133 ->setShippingAddress($payment->getOrder()->getShippingAddress())
00134 ->setEmail($payment->getOrder()->getCustomerEmail())
00135 ->setPayment($payment)
00136 ->setInvNum($payment->getOrder()->getIncrementId());
00137
00138 if ($api->callDoDirectPayment()!==false) {
00139 $payment
00140 ->setStatus('APPROVED')
00141 ->setCcTransId($api->getTransactionId())
00142 ->setCcAvsStatus($api->getAvsCode())
00143 ->setCcCidStatus($api->getCvv2Match());
00144
00145 #$payment->getOrder()->addStatusToHistory(Mage::getStoreConfig('payment/paypal_direct/order_status'));
00146 } else {
00147 $e = $api->getError();
00148 if (isset($e['short_message'])) {
00149 $message = $e['short_message'];
00150 } else {
00151 $message = Mage::helper('paypal')->__('There has been an error processing your payment. Please try later or contact us for help.');
00152 }
00153 if (isset($e['long_message'])) {
00154 $message .= ': '.$e['long_message'];
00155 }
00156 Mage::throwException($message);
00157 }
00158 return $this;
00159 }
00160
00161 public function capture(Varien_Object $payment, $amount)
00162 {
00163 $api = $this->getApi()
00164 ->setPaymentType(Mage_Paypal_Model_Api_Nvp::PAYMENT_TYPE_SALE)
00165 ->setAmount($amount)
00166 ->setBillingAddress($payment->getOrder()->getBillingAddress())
00167 ->setShippingAddress($payment->getOrder()->getShippingAddress())
00168 ->setEmail($payment->getOrder()->getCustomerEmail())
00169 ->setPayment($payment)
00170 ->setInvNum($payment->getOrder()->getIncrementId());
00171 ;
00172 if ($payment->getCcTransId()) {
00173 $api->setAuthorizationId($payment->getCcTransId())
00174 ->setCompleteType('NotComplete');
00175 $result = $api->callDoCapture()!==false;
00176 } else {
00177 $result = $api->callDoDirectPayment()!==false;
00178 }
00179 if ($result) {
00180 $payment
00181 ->setStatus('APPROVED')
00182
00183 ->setLastTransId($api->getTransactionId())
00184 ->setCcAvsStatus($api->getAvsCode())
00185 ->setCcCidStatus($api->getCvv2Match());
00186
00187 #$payment->getOrder()->addStatusToHistory(Mage::getStoreConfig('payment/paypal_direct/order_status'));
00188 } else {
00189 $e = $api->getError();
00190 if (isset($e['short_message'])) {
00191 $message = $e['short_message'];
00192 } else {
00193 $message = Mage::helper('paypal')->__('There has been an error processing your payment. Please try later or contact us for help.');
00194 }
00195 if (isset($e['long_message'])) {
00196 $message .= ': '.$e['long_message'];
00197 }
00198 Mage::throwException($message);
00199 }
00200 return $this;
00201 }
00202
00203 public function onOrderValidate(Mage_Sales_Model_Order_Payment $payment)
00204 {
00205 $api = $this->getApi()
00206 ->setPaymentType($this->getPaymentAction())
00207 ->setAmount($payment->getOrder()->getGrandTotal())
00208 ->setBillingAddress($payment->getOrder()->getBillingAddress())
00209 ->setPayment($payment)
00210 ->setInvNum($payment->getOrder()->getIncrementId());
00211
00212 if ($api->callDoDirectPayment()!==false) {
00213 $payment
00214 ->setStatus('APPROVED')
00215 ->setCcTransId($api->getTransactionId())
00216 ->setCcAvsStatus($api->getAvsCode())
00217 ->setCcCidStatus($api->getCvv2Match());
00218
00219 #$payment->getOrder()->addStatusToHistory(Mage::getStoreConfig('payment/paypal_direct/order_status'));
00220 } else {
00221 $e = $api->getError();
00222 if (isset($e['short_message'])) {
00223 $message = $e['short_message'];
00224 } else {
00225 $message = Mage::helper('paypal')->__('There has been an error processing your payment. Please try later or contact us for help.');
00226 }
00227 if (isset($e['long_message'])) {
00228 $message .= ': '.$e['long_message'];
00229 }
00230 $payment
00231 ->setStatus('ERROR')
00232 ->setStatusDescription($message);
00233 }
00234 return $this;
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244 public function void(Varien_Object $payment)
00245 {
00246 $error = false;
00247 if($payment->getVoidTransactionId()){
00248 $api = $this->getApi();
00249 $api->setPayment($payment);
00250 $api->setAuthorizationId($payment->getVoidTransactionId());
00251 if ($api->callDoVoid()!==false){
00252 $payment->setStatus('SUCCESS')
00253 ->setCcTransId($api->getTransactionId());
00254 }else{
00255 $e = $api->getError();
00256 $error = $e['short_message'].': '.$e['long_message'];
00257 }
00258 }else{
00259 $payment->setStatus('ERROR');
00260 $error = Mage::helper('paypal')->__('Invalid transaction id');
00261 }
00262 if ($error !== false) {
00263 Mage::throwException($error);
00264 }
00265 return $this;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275 public function refund(Varien_Object $payment, $amount)
00276 {
00277 $error = false;
00278 if($payment->getRefundTransactionId() && $amount>0){
00279 $api = $this->getApi();
00280 $api->setPayment($payment);
00281
00282 $api->setTransactionId($payment->getRefundTransactionId())
00283 ->setRefundType(Mage_Paypal_Model_Api_Nvp::REFUND_TYPE_PARTIAL)
00284 ->setAmount($amount);
00285
00286 if ($api->callRefundTransaction()!==false){
00287 $payment->setStatus('SUCCESS')
00288 ->setCcTransId($api->getTransactionId());
00289 }else{
00290 $e = $api->getError();
00291 $error = $e['short_message'].': '.$e['long_message'];
00292 }
00293 }else{
00294 $error = Mage::helper('paypal')->__('Error in refunding the payment');
00295 }
00296 if ($error !== false) {
00297 Mage::throwException($error);
00298 }
00299 return $this;
00300 }
00301
00302 }