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
00034 class Mage_AmazonPayments_Model_Payment_Asp extends Mage_Payment_Model_Method_Abstract
00035 {
00036
00037
00038
00039 protected $_isGateway = false;
00040 protected $_canAuthorize = false;
00041 protected $_canCapture = true;
00042 protected $_canCapturePartial = false;
00043 protected $_canRefund = true;
00044 protected $_canVoid = true;
00045 protected $_canUseInternal = false;
00046 protected $_canUseCheckout = true;
00047 protected $_canUseForMultishipping = false;
00048 protected $_isInitializeNeeded = true;
00049
00050
00051
00052
00053 protected $_formBlockType = 'amazonpayments/asp_form';
00054
00055
00056
00057
00058 protected $_code = 'amazonpayments_asp';
00059
00060
00061
00062
00063 protected $_order;
00064
00065
00066
00067
00068
00069
00070
00071 public function getConfig($path)
00072 {
00073 return Mage::getStoreConfig('payment/' . $this->_code . '/' . $path);
00074 }
00075
00076
00077
00078
00079 public function isAvailable($quote=null)
00080 {
00081 return $this->getConfig('active');
00082 }
00083
00084
00085
00086
00087
00088
00089 public function getApi()
00090 {
00091 return Mage::getSingleton('amazonpayments/api_asp');
00092 }
00093
00094
00095
00096
00097
00098
00099 public function getNotification()
00100 {
00101 return Mage::getSingleton('amazonpayments/payment_asp_notification');
00102 }
00103
00104
00105
00106
00107
00108
00109
00110 public function setOrder($order)
00111 {
00112 $this->_order = $order;
00113 return $this;
00114 }
00115
00116
00117
00118
00119
00120
00121 public function getOrder()
00122 {
00123 if (!$this->_order) {
00124 $paymentInfo = $this->getInfoInstance();
00125 $this->_order = Mage::getModel('sales/order')->loadByIncrementId(
00126 $paymentInfo->getOrder()->getRealOrderId()
00127 );
00128 }
00129 return $this->_order;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 protected function _log($request, $response = '')
00140 {
00141 $debug = Mage::getModel('amazonpayments/api_debug')
00142 ->setRequestBody($request)
00143 ->setResponseBody($response)
00144 ->save();
00145 return $this;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155 protected function _mail($template, array $variables = array())
00156 {
00157 $mailTemplate = Mage::getModel('core/email_template');
00158 $mailTemplate->setDesignConfig(array('area' => 'frontend'))
00159 ->sendTransactional(
00160 $this->getConfig($template),
00161 $this->getConfig('email_sender_identity'),
00162 $this->getConfig('report_email'),
00163 null,
00164 $variables
00165 );
00166 return $this;
00167 }
00168
00169
00170
00171
00172 public function getOrderPlaceRedirectUrl()
00173 {
00174 return Mage::getUrl('amazonpayments/asp/pay');
00175 }
00176
00177
00178
00179
00180
00181
00182 public function getPayRedirectUrl()
00183 {
00184 return $this->getApi()->getPayUrl();
00185 }
00186
00187
00188
00189
00190
00191
00192 public function getPayRedirectParams()
00193 {
00194 $orderId = $this->getOrder()->getRealOrderId();
00195 $amount = Mage::app()->getStore()->roundPrice($this->getOrder()->getBaseGrandTotal());
00196 $currencyCode = $this->getOrder()->getBaseCurrency();
00197
00198 $urlModel = Mage::getModel('core/url')
00199 ->setUseSession(false);
00200
00201 return $this->getApi()->getPayParams(
00202 $orderId,
00203 $amount,
00204 $currencyCode,
00205 $urlModel->getUrl('amazonpayments/asp/returnCancel'),
00206 $urlModel->getUrl('amazonpayments/asp/returnSuccess'),
00207 $urlModel->getUrl('amazonpayments/asp/notification')
00208 );
00209 }
00210
00211
00212
00213
00214
00215
00216 public function processEventRedirect()
00217 {
00218 $this->getOrder()->addStatusToHistory(
00219 $this->getOrder()->getStatus(),
00220 Mage::helper('amazonpayments')->__('Customer was redirected to Amazon Simple Pay site')
00221 )->save();
00222 return $this;
00223 }
00224
00225
00226
00227
00228
00229
00230 public function processEventReturnSuccess()
00231 {
00232 $this->getOrder()->sendNewOrderEmail();
00233 $this->getOrder()->addStatusToHistory(
00234 $this->getOrder()->getStatus(),
00235 Mage::helper('amazonpayments')->__('Customer successfully returned from Amazon Simple Pay site')
00236 )->save();
00237 return $this;
00238 }
00239
00240
00241
00242
00243
00244
00245 public function processEventReturnCancel()
00246 {
00247 $this->getOrder()->addStatusToHistory(
00248 $this->getOrder()->getStatus(),
00249 Mage::helper('amazonpayments')->__('Customer canceled payment and successfully returned from Amazon Simple Pay site')
00250 )->save();
00251 return $this;
00252 }
00253
00254
00255
00256
00257 public function initialize($paymentAction, $stateObject)
00258 {
00259 $state = Mage_Sales_Model_Order::STATE_NEW;
00260 $stateObject->setState($state);
00261 $stateObject->setStatus(Mage::getSingleton('sales/order_config')->getStateDefaultStatus($state));
00262 $stateObject->setIsNotified(false);
00263 return $this;
00264 }
00265
00266
00267
00268
00269
00270
00271
00272 public function processNotification($requestParams)
00273 {
00274 if ($this->getConfig('debug_log')) {
00275 $this->_log('DEBUG ASP notification: ' . print_r($requestParams, 1));
00276 }
00277
00278 try {
00279 $this->getNotification()->setPayment($this)->process($requestParams);
00280 } catch(Exception $e) {
00281 if ($this->getConfig('error_log')) {
00282 $this->_log('ERROR ASP notification: ' . print_r($requestParams, 1), $e->getMessage());
00283 }
00284
00285 if ($this->getConfig('report_error_to_email')) {
00286 $variables = array();
00287 $variables['request'] = print_r($requestParams, 1);
00288 $variables['error'] = $e->getMessage();
00289 $this->_mail('email_template_notofication_error', $variables);
00290 }
00291 }
00292
00293 return $this;
00294 }
00295
00296
00297
00298
00299 public function capture(Varien_Object $payment, $amount)
00300 {
00301 if (is_null($payment->getCcTransId())) {
00302 Mage::throwException(
00303 Mage::helper('amazonpayments')->__('Order was not captured online. Authorization confirmation is required.')
00304 );
00305 }
00306 return $this;
00307 }
00308
00309
00310
00311
00312 public function processInvoice($invoice, $payment)
00313 {
00314 if (!is_null($payment->getCcTransId()) &&
00315 is_null($payment->getLastTransId()) &&
00316 is_null($invoice->getTransactionId())) {
00317
00318 $amount = Mage::app()->getStore()->roundPrice($invoice->getBaseGrandTotal());
00319 $currencyCode = $payment->getOrder()->getBaseCurrency();
00320 $transactionId = $payment->getCcTransId();
00321 $response = $this->getApi()
00322 ->setStoreId($payment->getOrder()->getStoreId())
00323 ->capture($transactionId, $amount, $currencyCode);
00324
00325 if ($response->getStatus() == Mage_AmazonPayments_Model_Api_Asp_Fps_Response_Abstract::STATUS_ERROR) {
00326 Mage::throwException(
00327 Mage::helper('amazonpayments')->__('Order was not captured. Amazon Simple Pay service error: [%s] %s', $response->getCode(), $response->getMessage())
00328 );
00329 }
00330
00331 if ($response->getStatus() == Mage_AmazonPayments_Model_Api_Asp_Fps_Response_Abstract::STATUS_SUCCESS ||
00332 $response->getStatus() == Mage_AmazonPayments_Model_Api_Asp_Fps_Response_Abstract::STATUS_PENDING) {
00333
00334 $payment->setForcedState(Mage_Sales_Model_Order_Invoice::STATE_OPEN);
00335 $payment->setLastTransId($response->getTransactionId());
00336
00337 $invoice->setTransactionId($response->getTransactionId());
00338 $invoice->addComment(Mage::helper('amazonpayments')->__('Invoice was created (online capture). Waiting for capture confirmation from Amazon Simple Pay service.'));
00339
00340 $payment->getOrder()->addStatusToHistory(
00341 $payment->getOrder()->getStatus(),
00342 Mage::helper('amazonpayments')->__('Payment was captured online with Amazon Simple Pay service. Invoice was created. Waiting for capture confirmation from payment service.')
00343 )->save();
00344
00345 }
00346 }
00347 return $this;
00348 }
00349
00350
00351
00352
00353 public function processCreditmemo($creditmemo, $payment)
00354 {
00355
00356 $transactionId = $creditmemo->getInvoice()->getTransactionId();
00357
00358 if (!is_null($transactionId) &&
00359 is_null($creditmemo->getTransactionId())) {
00360
00361 $amount = Mage::app()->getStore()->roundPrice($creditmemo->getBaseGrandTotal());
00362 $currencyCode = $payment->getOrder()->getBaseCurrency();
00363 $referenseID = $creditmemo->getInvoice()->getIncrementId();
00364 $response = $this->getApi()
00365 ->setStoreId($payment->getOrder()->getStoreId())
00366 ->refund($transactionId, $amount, $currencyCode, $referenseID);
00367
00368 if ($response->getStatus() == Mage_AmazonPayments_Model_Api_Asp_Fps_Response_Abstract::STATUS_ERROR) {
00369 Mage::throwException(
00370 Mage::helper('amazonpayments')->__('Invoice was not refunded. Amazon Simple Pay service error: [%s] %s', $response->getCode(), $response->getMessage())
00371 );
00372 }
00373
00374 if ($response->getStatus() == Mage_AmazonPayments_Model_Api_Asp_Fps_Response_Abstract::STATUS_SUCCESS ||
00375 $response->getStatus() == Mage_AmazonPayments_Model_Api_Asp_Fps_Response_Abstract::STATUS_PENDING) {
00376
00377 $creditmemo->setTransactionId($response->getTransactionId());
00378 $creditmemo->addComment(Mage::helper('amazonpayments')->__('Payment refunded online. Waiting for refund confirmation from Amazon Simple Pay service.'));
00379 $creditmemo->setState(Mage_Sales_Model_Order_Creditmemo::STATE_OPEN);
00380
00381 $payment->getOrder()->addStatusToHistory(
00382 $payment->getOrder()->getStatus(),
00383 Mage::helper('amazonpayments')->__('Payment refunded online with Amazon Simple Pay service. Creditmemo was created. Waiting for refund confirmation from Amazon Simple Pay service.')
00384 )->save();
00385 }
00386 }
00387 return $this;
00388 }
00389
00390
00391
00392
00393 public function cancel(Varien_Object $payment)
00394 {
00395 if (!is_null($payment->getCcTransId()) &&
00396 is_null($payment->getLastTransId())) {
00397
00398 $transactionId = $payment->getCcTransId();
00399 $response = $this->getApi()
00400 ->setStoreId($payment->getOrder()->getStoreId())
00401 ->cancel($transactionId);
00402
00403 if ($response->getStatus() == Mage_AmazonPayments_Model_Api_Asp_Fps_Response_Abstract::STATUS_ERROR) {
00404 Mage::throwException(
00405 Mage::helper('amazonpayments')->__('Order was not cancelled. Amazon Simple Pay service error: [%s] %s', $response->getCode(), $response->getMessage())
00406 );
00407 }
00408
00409 if ($response->getStatus() == Mage_AmazonPayments_Model_Api_Asp_Fps_Response_Abstract::STATUS_CANCELLED) {
00410 $payment->getOrder()->setState(
00411 Mage_Sales_Model_Order::STATE_CANCELED,
00412 true,
00413 Mage::helper('amazonpayments')->__('Payment authorization cancelled with Amazon Simple Pay service.'),
00414 $notified = false
00415 )->save();
00416 }
00417 }
00418 return $this;
00419 }
00420 }