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_Fps extends Mage_AmazonPayments_Model_Api_Asp_Abstract
00035 {
00036
00037
00038
00039 const SERVICE_VERSION = '2008-09-17';
00040
00041
00042
00043
00044 const ACTION_CODE_CANCEL = 'Cancel';
00045 const ACTION_CODE_SETTLE = 'Settle';
00046 const ACTION_CODE_REFUND = 'Refund';
00047
00048
00049
00050
00051 const EXCEPTION_INVALID_ACTION_CODE = 10031;
00052 const EXCEPTION_INVALID_REQUEST = 10031;
00053 const EXCEPTION_INVALID_RESPONSE = 10032;
00054
00055
00056
00057
00058
00059
00060
00061 public function getRequest($actionCode)
00062 {
00063 switch ($actionCode) {
00064 case self::ACTION_CODE_CANCEL:
00065 $requestModelPath = 'amazonpayments/api_asp_fps_request_cancel';
00066 break;
00067 case self::ACTION_CODE_SETTLE:
00068 $requestModelPath = 'amazonpayments/api_asp_fps_request_settle';
00069 break;
00070 case self::ACTION_CODE_REFUND:
00071 $requestModelPath = 'amazonpayments/api_asp_fps_request_refund';
00072 break;
00073 default: $this->_throwExeptionInvalidActionCode();
00074 }
00075
00076 return Mage::getSingleton($requestModelPath)->init($actionCode);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086 protected function _getResponse($requestActionCode, $responseBody)
00087 {
00088 switch ($requestActionCode) {
00089 case self::ACTION_CODE_CANCEL:
00090 $responseModelPath = 'amazonpayments/api_asp_fps_response_cancel';
00091 break;
00092 case self::ACTION_CODE_SETTLE:
00093 $responseModelPath = 'amazonpayments/api_asp_fps_response_settle';
00094 break;
00095 case self::ACTION_CODE_REFUND:
00096 $responseModelPath = 'amazonpayments/api_asp_fps_response_refund';
00097 break;
00098 default: $this->_throwExeptionInvalidActionCode();
00099 }
00100
00101 $actionResponse = Mage::getSingleton($responseModelPath);
00102 if ($actionResponse->init($responseBody)) {
00103 return $actionResponse;
00104 }
00105
00106 $errorResponse = Mage::getSingleton('amazonpayments/api_asp_fps_response_error');
00107 if ($errorResponse->init($responseBody)) {
00108 return $errorResponse;
00109 }
00110
00111 throw new Exception(
00112 Mage::helper('amazonpayments')->__('Response body is not valid FPS respons'),
00113 self::EXCEPTION_INVALID_RESPONSE
00114 );
00115 }
00116
00117
00118
00119
00120
00121
00122
00123 public function process($request)
00124 {
00125 if (!$request->isValid()) {
00126 throw new Exception(
00127 Mage::helper('amazonpayments')->__('Invalid request'),
00128 self::EXCEPTION_INVALID_REQUEST
00129 );
00130 }
00131
00132 $request = $this->_addRequiredParameters($request);
00133 $request = $this->_signRequest($request);
00134
00135 $responseBody = $this->_call($this->_getServiceUrl(), $request->getData());
00136 return $this->_getResponse($request->getActionCode(), $responseBody);
00137 }
00138
00139
00140
00141
00142
00143
00144 protected function _getServiceUrl()
00145 {
00146 if ($this->_isSandbox()) {
00147 return $this->_getConfig('fps_service_url_sandbox');
00148 }
00149 return $this->_getConfig('fps_service_url');
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 protected function _addRequiredParameters($request)
00159 {
00160 return $request->setData('AWSAccessKeyId', $this->_getConfig('access_key'))
00161 ->setData('Timestamp', gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()))
00162 ->setData('Version', self::SERVICE_VERSION)
00163 ->setData('SignatureVersion', '1');
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 protected function _signRequest($request)
00173 {
00174 $signature = $this->_getSignatureForArray($request->getData(), $this->_getConfig('secret_key'));
00175 return $request->setData('Signature', $signature);
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185 protected function _call($serviceUrl, $params)
00186 {
00187 $tmpArray = array();
00188 foreach ($params as $kay => $value) {
00189 $tmpArray[] = $kay . '=' . urlencode($value);
00190 }
00191 $requestBody = implode('&', $tmpArray);
00192
00193 $http = new Varien_Http_Adapter_Curl();
00194 $http->setConfig(array('timeout' => 30));
00195 $http->write(Zend_Http_Client::POST, $serviceUrl, '1.1', array(), $requestBody);
00196
00197 $responseBody = $http->read();
00198 $responseBody = preg_split('/^\r?$/m', $responseBody, 2);
00199 $responseBody = trim($responseBody[1]);
00200
00201 $responseBody = new Varien_Simplexml_Element($responseBody);
00202
00203 $http->close();
00204 return $responseBody;
00205 }
00206
00207
00208
00209
00210 protected function _throwExeptionInvalidActionCode()
00211 {
00212 throw new Exception(
00213 Mage::helper('amazonpayments')->__('Invalid action code'),
00214 self::EXCEPTION_INVALID_ACTION_CODE
00215 );
00216 }
00217 }