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 class Mage_Sales_Model_Order_Payment extends Mage_Payment_Model_Info
00031 {
00032
00033
00034
00035
00036
00037 protected $_order;
00038
00039
00040
00041
00042 protected function _construct()
00043 {
00044 $this->_init('sales/order_payment');
00045 }
00046
00047
00048
00049
00050
00051
00052
00053 public function setOrder(Mage_Sales_Model_Order $order)
00054 {
00055 $this->_order = $order;
00056 return $this;
00057 }
00058
00059
00060
00061
00062
00063
00064 public function getOrder()
00065 {
00066 return $this->_order;
00067 }
00068
00069
00070
00071
00072
00073
00074 public function canCapture()
00075 {
00076
00077
00078
00079 return $this->getMethodInstance()->canCapture();
00080 }
00081
00082 public function canRefund()
00083 {
00084 return $this->getMethodInstance()->canRefund();
00085 }
00086
00087 public function canRefundPartialPerInvoice()
00088 {
00089 return $this->getMethodInstance()->canRefundPartialPerInvoice();
00090 }
00091
00092 public function canCapturePartial()
00093 {
00094 return $this->getMethodInstance()->canCapturePartial();
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 public function place()
00105 {
00106 Mage::dispatchEvent('sales_order_payment_place_start', array('payment' => $this));
00107
00108 $this->setAmountOrdered($this->getOrder()->getTotalDue());
00109 $this->setBaseAmountOrdered($this->getOrder()->getBaseTotalDue());
00110
00111 $this->setShippingAmount($this->getOrder()->getShippingAmount());
00112 $this->setBaseShippingAmount($this->getOrder()->getBaseShippingAmount());
00113
00114 $methodInstance = $this->getMethodInstance()->setStore($this->getOrder()->getStoreId());
00115
00116 $orderState = Mage_Sales_Model_Order::STATE_NEW;
00117 $orderStatus= false;
00118
00119 $stateObject = new Varien_Object();
00120
00121
00122
00123
00124 $methodInstance->validate();
00125 if ($action = $methodInstance->getConfigData('payment_action')) {
00126
00127
00128
00129
00130 if ($methodInstance->isInitializeNeeded()) {
00131 $methodInstance->initialize($action, $stateObject);
00132 } else {
00133 switch ($action) {
00134 case Mage_Payment_Model_Method_Abstract::ACTION_AUTHORIZE:
00135 case Mage_Paypal_Model_Api_Abstract::PAYMENT_TYPE_AUTH:
00136 $methodInstance->authorize($this, $this->getOrder()->getBaseTotalDue());
00137
00138 $this->setAmountAuthorized($this->getOrder()->getTotalDue());
00139 $this->setBaseAmountAuthorized($this->getOrder()->getBaseTotalDue());
00140
00141 $orderState = Mage_Sales_Model_Order::STATE_PROCESSING;
00142 break;
00143 case Mage_Payment_Model_Method_Abstract::ACTION_AUTHORIZE_CAPTURE:
00144 case Mage_Paypal_Model_Api_Abstract::PAYMENT_TYPE_SALE:
00145 $invoice = $this->_invoice();
00146
00147 $this->setAmountAuthorized($this->getOrder()->getTotalDue());
00148 $this->setBaseAmountAuthorized($this->getOrder()->getBaseTotalDue());
00149
00150 $orderState = Mage_Sales_Model_Order::STATE_PROCESSING;
00151 break;
00152 default:
00153 break;
00154 }
00155 }
00156 }
00157
00158 $orderIsNotified = null;
00159 if ($stateObject->getState() && $stateObject->getStatus()) {
00160 $orderState = $stateObject->getState();
00161 $orderStatus = $stateObject->getStatus();
00162 $orderIsNotified = $stateObject->getIsNotified();
00163 } else {
00164
00165
00166
00167 if ($this->getFraudFlag()) {
00168 $orderStatus = $methodInstance->getConfigData('fraud_order_status');
00169 $orderState = Mage_Sales_Model_Order::STATE_HOLDED;
00170 } else {
00171
00172
00173
00174 $orderStatus = $methodInstance->getConfigData('order_status');
00175 }
00176
00177 if (!$orderStatus) {
00178 $orderStatus = $this->getOrder()->getConfig()->getStateDefaultStatus($orderState);
00179 }
00180 }
00181
00182 $this->getOrder()->setState($orderState);
00183 $this->getOrder()->addStatusToHistory(
00184 $orderStatus,
00185 $this->getOrder()->getCustomerNote(),
00186 (null !== $orderIsNotified ? $orderIsNotified : $this->getOrder()->getCustomerNoteNotify())
00187 );
00188
00189 Mage::dispatchEvent('sales_order_payment_place_end', array('payment' => $this));
00190
00191 return $this;
00192 }
00193
00194
00195
00196
00197
00198
00199 public function capture($invoice)
00200 {
00201 if (is_null($invoice)) {
00202 $invoice = $this->_invoice();
00203 }
00204
00205 Mage::dispatchEvent('sales_order_payment_capture', array('payment' => $this, 'invoice' => $invoice));
00206
00207 $this->getMethodInstance()
00208 ->setStore($this->getOrder()->getStoreId())
00209 ->capture($this, sprintf('%.2f', $invoice->getBaseGrandTotal()));
00210 $this->getMethodInstance()->processInvoice($invoice, $this);
00211 return $this;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221 public function pay($invoice)
00222 {
00223 $this->setAmountPaid($this->getAmountPaid()+$invoice->getGrandTotal());
00224 $this->setBaseAmountPaid($this->getBaseAmountPaid()+$invoice->getBaseGrandTotal());
00225
00226 $this->setShippingCaptured($this->getShippingCaptured()+$invoice->getShippingAmount());
00227 $this->setBaseShippingCaptured($this->getBaseShippingCaptured()+$invoice->getBaseShippingAmount());
00228
00229 Mage::dispatchEvent('sales_order_payment_pay', array('payment' => $this, 'invoice' => $invoice));
00230
00231 return $this;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240 public function cancelInvoice($invoice)
00241 {
00242 $this->setAmountPaid($this->getAmountPaid()-$invoice->getGrandTotal());
00243 $this->setBaseAmountPaid($this->getBaseAmountPaid()-$invoice->getBaseGrandTotal());
00244
00245 $this->setShippingCaptured($this->getShippingCaptured()-$invoice->getShippingAmount());
00246 $this->setBaseShippingCaptured($this->getBaseShippingCaptured()-$invoice->getBaseShippingAmount());
00247
00248 Mage::dispatchEvent('sales_order_payment_cancel_invoice', array('payment' => $this, 'invoice' => $invoice));
00249
00250 return $this;
00251 }
00252
00253
00254
00255
00256
00257
00258
00259 protected function _invoice()
00260 {
00261 $invoice = $this->getOrder()->prepareInvoice();
00262
00263 $invoice->register();
00264 if ($this->getMethodInstance()->canCapture()) {
00265 $invoice->capture();
00266 }
00267
00268 $this->getOrder()->addRelatedObject($invoice);
00269 return $invoice;
00270 }
00271
00272
00273
00274
00275
00276
00277 public function canVoid(Varien_Object $document)
00278 {
00279 return $this->getMethodInstance()->canVoid($document);
00280 }
00281
00282 public function void(Varien_Object $document)
00283 {
00284 $this->getMethodInstance()
00285 ->setStore($this->getOrder()->getStoreId())
00286 ->processBeforeVoid($document, $this);
00287
00288 $this->getMethodInstance()->void($this);
00289
00290 Mage::dispatchEvent('sales_order_payment_void', array('payment' => $this, 'invoice' => $document));
00291
00292 return $this;
00293 }
00294
00295 public function refund($creditmemo)
00296 {
00297 if ($this->getMethodInstance()->canRefund() && $creditmemo->getDoTransaction()) {
00298 $this->setCreditmemo($creditmemo);
00299 if ($creditmemo->getInvoice()) {
00300 $this->getMethodInstance()->setStore($this->getOrder()->getStoreId());
00301 $this->getMethodInstance()->processBeforeRefund($creditmemo->getInvoice(), $this);
00302 $this->getMethodInstance()->refund($this, $creditmemo->getBaseGrandTotal());
00303 $this->getMethodInstance()->processCreditmemo($creditmemo, $this);
00304 }
00305 }
00306
00307 $this->setAmountRefunded($this->getAmountRefunded()+$creditmemo->getGrandTotal());
00308 $this->setBaseAmountRefunded($this->getBaseAmountRefunded()+$creditmemo->getBaseGrandTotal());
00309
00310 $this->setShippingRefunded($this->getShippingRefunded()+$creditmemo->getShippingAmount());
00311 $this->setBaseShippingRefunded($this->getBaseShippingRefunded()+$creditmemo->getBaseShippingAmount());
00312
00313 Mage::dispatchEvent('sales_order_payment_refund', array('payment' => $this, 'creditmemo' => $creditmemo));
00314
00315 return $this;
00316 }
00317
00318 public function cancelCreditmemo($creditmemo)
00319 {
00320 $this->setAmountRefunded($this->getAmountRefunded()-$creditmemo->getGrandTotal());
00321 $this->setBaseAmountRefunded($this->getBaseAmountRefunded()-$creditmemo->getBaseGrandTotal());
00322
00323 $this->setShippingRefunded($this->getShippingRefunded()-$creditmemo->getShippingAmount());
00324 $this->setBaseShippingRefunded($this->getBaseShippingRefunded()-$creditmemo->getBaseShippingAmount());
00325
00326 Mage::dispatchEvent('sales_order_payment_cancel_creditmemo', array('payment' => $this, 'creditmemo' => $creditmemo));
00327
00328 return $this;
00329 }
00330
00331 public function cancel()
00332 {
00333 $this->getMethodInstance()
00334 ->setStore($this->getOrder()->getStoreId())
00335 ->cancel($this);
00336
00337 Mage::dispatchEvent('sales_order_payment_cancel', array('payment' => $this));
00338
00339 return $this;
00340 }
00341 }