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_Standard extends Mage_Payment_Model_Method_Abstract
00034 {
00035
00036 const PAYMENT_TYPE_AUTH = 'AUTHORIZATION';
00037 const PAYMENT_TYPE_SALE = 'SALE';
00038
00039 protected $_code = 'paypal_standard';
00040 protected $_formBlockType = 'paypal/standard_form';
00041 protected $_allowCurrencyCode = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD');
00042
00043
00044
00045
00046
00047
00048
00049 public function canUseForCurrency($currencyCode)
00050 {
00051 if (!in_array($currencyCode, $this->_allowCurrencyCode)) {
00052 return false;
00053 }
00054 return true;
00055 }
00056
00057
00058
00059
00060
00061
00062 protected $_debugReplacePrivateDataKeys = array('business');
00063
00064
00065
00066
00067
00068
00069 public function getSession()
00070 {
00071 return Mage::getSingleton('paypal/session');
00072 }
00073
00074
00075
00076
00077
00078
00079 public function getCheckout()
00080 {
00081 return Mage::getSingleton('checkout/session');
00082 }
00083
00084
00085
00086
00087
00088
00089 public function getQuote()
00090 {
00091 return $this->getCheckout()->getQuote();
00092 }
00093
00094
00095
00096
00097
00098
00099 public function canUseInternal()
00100 {
00101 return false;
00102 }
00103
00104
00105
00106
00107
00108
00109 public function canUseForMultishipping()
00110 {
00111 return false;
00112 }
00113
00114 public function createFormBlock($name)
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 }
00123
00124
00125 public function validate()
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 }
00134
00135 public function onOrderValidate(Mage_Sales_Model_Order_Payment $payment)
00136 {
00137 return $this;
00138 }
00139
00140 public function onInvoiceCreate(Mage_Sales_Model_Invoice_Payment $payment)
00141 {
00142
00143 }
00144
00145 public function canCapture()
00146 {
00147 return true;
00148 }
00149
00150 public function getOrderPlaceRedirectUrl()
00151 {
00152 return Mage::getUrl('paypal/standard/redirect', array('_secure' => true));
00153 }
00154
00155 public function getStandardCheckoutFormFields()
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
00165 $currency_code = $this->getQuote()->getBaseCurrencyCode();
00166
00167
00168
00169
00170
00171
00172
00173
00174
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
00212
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
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
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 }
00310
00311 public function getPaypalUrl()
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 }
00320
00321 public function getDebug()
00322 {
00323 return Mage::getStoreConfig('paypal/wps/debug_flag');
00324 }
00325
00326
00327 public function ipnPostSubmit()
00328 {
00329 $sReq = '';
00330 $sReqDebug = '';
00331 foreach($this->getIpnFormData() as $k=>$v) {
00332 $sReq .= '&'.$k.'='.urlencode(stripslashes($v));
00333 $sReqDebug .= '&'.$k.'=';
00334
00335 }
00336
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
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
00364
00365
00366 } else {
00367
00368 if ($this->getIpnFormData('mc_gross')!=$order->getBaseGrandTotal()) {
00369
00370 $order->addStatusToHistory(
00371 $order->getStatus(),
00372 Mage::helper('paypal')->__('Order total amount does not match paypal gross total amount')
00373 );
00374 $order->save();
00375 } else {
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 $newOrderStatus = $this->getConfigData('order_status', $order->getStoreId());
00390 if (empty($newOrderStatus)) {
00391 $newOrderStatus = $order->getStatus();
00392 }
00393
00394
00395
00396
00397
00398
00399 if ($this->getIpnFormData('payment_status') == 'Completed') {
00400 if (!$order->canInvoice()) {
00401
00402 $order->addStatusToHistory(
00403 $order->getStatus(),
00404 Mage::helper('paypal')->__('Error in creating an invoice', true),
00405 $notified = true
00406 );
00407
00408 } else {
00409
00410 $order->getPayment()->setTransactionId($this->getIpnFormData('txn_id'));
00411
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 }
00445
00446 }
00447 }else{
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
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
00468 if (!$order->getId()) {
00469
00470
00471
00472 } else {
00473 $order->addStatusToHistory(
00474 $order->getStatus(),
00475 Mage::helper('paypal')->__('Paypal IPN Invalid %s.', $comment)
00476 );
00477 $order->save();
00478 }
00479 }
00480 }
00481
00482 public function isInitializeNeeded()
00483 {
00484 return true;
00485 }
00486
00487 public function initialize($paymentAction, $stateObject)
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 }
00494 }