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_Api_Asp extends Mage_AmazonPayments_Model_Api_Asp_Abstract
00035 {
00036
00037
00038
00039 protected $_collectShippingAddress = 0;
00040
00041
00042
00043
00044 protected $_ipnRequest = 'amazonpayments/api_asp_ipn_request';
00045
00046
00047
00048
00049 protected $_fpsModel = 'amazonpayments/api_asp_fps';
00050
00051
00052
00053
00054
00055
00056 protected function _getFps()
00057 {
00058 return Mage::getSingleton($this->_fpsModel)->setStoreId($this->getStoreId());
00059 }
00060
00061
00062
00063
00064
00065
00066 protected function _getIpnRequest()
00067 {
00068 return Mage::getSingleton($this->_ipnRequest);
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 public function getPayUrl ()
00078 {
00079 if ($this->_isSandbox()) {
00080 return $this->_getConfig('pay_service_url_sandbox');
00081 }
00082 return $this->_getConfig('pay_service_url');
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 public function getPayParams($referenceId, $amountValue, $currencyCode, $abandonUrl, $returnUrl, $ipnUrl)
00097 {
00098 $amount = Mage::getSingleton('amazonpayments/api_asp_amount')
00099 ->setValue($amountValue)
00100 ->setCurrencyCode($currencyCode);
00101
00102 $requestParams = array();
00103 $requestParams['referenceId'] = $referenceId;
00104 $requestParams['amount'] = $amount->toString();
00105 $requestParams['description'] = $this->_getConfig('pay_description');
00106
00107 $requestParams['accessKey'] = $this->_getConfig('access_key');
00108 $requestParams['processImmediate'] = $this->_getConfig('pay_process_immediate');
00109 $requestParams['immediateReturn'] = $this->_getConfig('pay_immediate_return');
00110 $requestParams['collectShippingAddress'] = $this->_collectShippingAddress;
00111 $requestParams['abandonUrl'] = $abandonUrl;
00112 $requestParams['returnUrl'] = $returnUrl;
00113 $requestParams['ipnUrl'] = $ipnUrl;
00114
00115 $signature = $this->_getSignatureForArray($requestParams, $this->_getConfig('secret_key'));
00116 $requestParams['signature'] = $signature;
00117
00118 return $requestParams;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127 public function processNotification($requestParams)
00128 {
00129 $requestSignature = false;
00130
00131 if (isset($requestParams['signature'])) {
00132 $requestSignature = $requestParams['signature'];
00133 unset($requestParams['signature']);
00134 }
00135
00136 $originalSignature = $this->_getSignatureForArray($requestParams, $this->_getConfig('secret_key'));
00137 if ($requestSignature != $originalSignature) {
00138 Mage::throwException(Mage::helper('amazonpayments')->__('Request signed an incorrect or missing signature'));
00139 }
00140
00141 $ipnRequest = $this->_getIpnRequest();
00142
00143 if(!$ipnRequest->init($requestParams)) {
00144 Mage::throwException(Mage::helper('amazonpayments')->__('Request is not a valid IPN request'));
00145 }
00146
00147 return $ipnRequest;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156 public function cancel($transactionId)
00157 {
00158 $fps = $this->_getFps();
00159
00160 $request = $fps->getRequest(Mage_AmazonPayments_Model_Api_Asp_Fps::ACTION_CODE_CANCEL)
00161 ->setTransactionId($transactionId)
00162 ->setDescription($this->_getConfig('cancel_description'));
00163
00164 $response = $fps->process($request);
00165 return $response;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 public function capture($transactionId, $amount, $currencyCode)
00177 {
00178 $fps = $this->_getFps();
00179 $amount = $this->_getAmount()
00180 ->setValue($amount)
00181 ->setCurrencyCode($currencyCode);
00182
00183 $request = $fps->getRequest(Mage_AmazonPayments_Model_Api_Asp_Fps::ACTION_CODE_SETTLE)
00184 ->setTransactionId($transactionId)
00185 ->setAmount($amount);
00186
00187 $response = $fps->process($request);
00188 return $response;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 public function refund($transactionId, $amount, $currencyCode, $referenceId)
00201 {
00202 $fps = $this->_getFps();
00203
00204 $amount = $this->_getAmount()
00205 ->setValue($amount)
00206 ->setCurrencyCode($currencyCode);
00207
00208 $request = $fps->getRequest(Mage_AmazonPayments_Model_Api_Asp_Fps::ACTION_CODE_REFUND)
00209 ->setTransactionId($transactionId)
00210 ->setReferenceId($referenceId)
00211 ->setAmount($amount)
00212 ->setDescription($this->_getConfig('refund_description'));
00213
00214 $response = $fps->process($request);
00215 return $response;
00216 }
00217 }