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
00035 class Mage_AmazonPayments_Model_Api_Cba_Document extends Varien_Object
00036 {
00037 const MESSAGE_TYPE_ADJUSTMENT = '_POST_PAYMENT_ADJUSTMENT_DATA_';
00038 const MESSAGE_TYPE_FULFILLMENT = '_POST_ORDER_FULFILLMENT_DATA_';
00039 const MESSAGE_TYPE_ACKNOWLEDGEMENT = '_POST_ORDER_ACKNOWLEDGEMENT_DATA_';
00040
00041 protected $_wsdlUri = null;
00042 protected $_merchantInfo = array();
00043 protected $_client = null;
00044 protected $_result = null;
00045 protected $_proccessFailed = false;
00046 protected $_options = array(
00047 'trace' => true,
00048 'timeout' => '20',
00049 );
00050
00051 protected function _construct()
00052 {
00053 parent::_construct();
00054 }
00055
00056
00057
00058
00059
00060
00061
00062 public function setWsdlUri($wsdlUri)
00063 {
00064 $this->_wsdlUri = $wsdlUri;
00065 return $this;
00066 }
00067
00068
00069
00070
00071
00072
00073 public function getWsdlUri()
00074 {
00075 return $this->_wsdlUri;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084 public function setMerchantInfo(array $merchantInfo = array())
00085 {
00086 $this->_merchantInfo = $merchantInfo;
00087 return $this;
00088 }
00089
00090
00091
00092
00093
00094
00095 public function getMerchantInfo()
00096 {
00097 return $this->_merchantInfo;
00098 }
00099
00100
00101
00102
00103
00104
00105 public function getMerchantIdentifier()
00106 {
00107 if (array_key_exists('merchantIdentifier', $this->_merchantInfo)) {
00108 return $this->_merchantInfo['merchantIdentifier'];
00109 }
00110 return null;
00111 }
00112
00113
00114
00115
00116
00117
00118 public function getClient()
00119 {
00120 return $this->_client;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130 public function init($login, $password)
00131 {
00132 if ($this->getWsdlUri()) {
00133 $this->_client = null;
00134 $auth = array('user' => $login, 'pass' => $password);
00135 try {
00136 set_include_path(
00137 BP . DS . 'lib' . DS . 'PEAR' . PS . get_include_path()
00138 );
00139 require_once 'SOAP/Client.php';
00140 $this->_client = new SOAP_Client($this->getWsdlUri(), true, false, $auth, false);
00141 } catch (Exception $e) {
00142 Zend_Debug::dump($e->getMessage());
00143 }
00144 }
00145 return $this;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154 protected function _createAttachment($document)
00155 {
00156 require_once 'SOAP/Value.php';
00157 $attachment = new SOAP_Attachment('doc', 'application/binary', null, $document);
00158 $attachment->options['attachment']['encoding'] = '8bit';
00159 $this->_options['attachments'] = 'Mime';
00160 return $attachment;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170 protected function _proccessRequest($method, $params)
00171 {
00172 if ($this->getClient()) {
00173 $this->_result = null;
00174 $this->_proccessFailed = false;
00175 try {
00176 $this->_result = $this->getClient()
00177 ->call($method, $params, $this->_options);
00178 } catch (Exception $e) {
00179 $this->_proccessFailed = true;
00180 }
00181 }
00182 return $this;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191 public function formatAmount($amount)
00192 {
00193 return Mage::helper('amazonpayments')->formatAmount($amount);
00194 }
00195
00196
00197
00198
00199
00200
00201
00202 public function getDocument($aOrderId)
00203 {
00204 $params = array(
00205 'merchant' => $this->getMerchantInfo(),
00206 'documentIdentifier' => $aOrderId
00207 );
00208 $this->_proccessRequest('getDocument', $params);
00209
00210 require_once 'Mail/mimeDecode.php';
00211 $decoder = new Mail_mimeDecode($this->getClient()->xml);
00212 $decoder->decode(array(
00213 'include_bodies' => true,
00214 'decode_bodies' => true,
00215 'decode_headers' => true,
00216 ));
00217 $xml = $decoder->_body;
00218
00219
00220 $boundaryIndex = strripos($xml, '--xxx-WASP-CPP-MIME-Boundary-xxx');
00221 if (!($boundaryIndex === false)) {
00222 $xml = substr($xml, 0, $boundaryIndex);
00223 }
00224
00225 return simplexml_load_string($xml, 'Varien_Simplexml_Element');
00226 }
00227
00228
00229
00230
00231
00232
00233 public function getPendingDocuments()
00234 {
00235 $params = array(
00236 'merchant' => $this->getMerchantInfo(),
00237 'messageType' => '_GET_ORDERS_DATA_'
00238 );
00239 $this->_proccessRequest('getAllPendingDocumentInfo', $params);
00240 if (!is_array($this->_result)) {
00241 $this->_result = array($this->_result);
00242 }
00243 return $this->_result;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 public function sendAcknowledgement($order)
00253 {
00254 $_document = '<?xml version="1.0" encoding="UTF-8"?>
00255 <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
00256 <Header>
00257 <DocumentVersion>1.01</DocumentVersion>
00258 <MerchantIdentifier>' . $this->getMerchantIdentifier() . '</MerchantIdentifier>
00259 </Header>
00260 <MessageType>OrderAcknowledgement</MessageType>
00261 <Message>
00262 <MessageID>1</MessageID>
00263 <OperationType>Update</OperationType>
00264 <OrderAcknowledgement>
00265 <AmazonOrderID>' . $order->getExtOrderId() . '</AmazonOrderID>
00266 <MerchantOrderID>' . $order->getRealOrderId() . '</MerchantOrderID>
00267 <StatusCode>Success</StatusCode>
00268 </OrderAcknowledgement>
00269 </Message>
00270 </AmazonEnvelope>';
00271
00272 $params = array(
00273 'merchant' => $this->getMerchantInfo(),
00274 'messageType' => self::MESSAGE_TYPE_ACKNOWLEDGEMENT,
00275 'doc' => $this->_createAttachment($_document)
00276 );
00277
00278 $this->_proccessRequest('postDocument', $params);
00279 return $this->_result;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288 public function cancel($order)
00289 {
00290 $_document = '<?xml version="1.0" encoding="UTF-8"?>
00291 <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
00292 <Header>
00293 <DocumentVersion>1.01</DocumentVersion>
00294 <MerchantIdentifier>' . $this->getMerchantIdentifier() . '</MerchantIdentifier>
00295 </Header>
00296 <MessageType>OrderAcknowledgement</MessageType>
00297 <Message>
00298 <MessageID>1</MessageID>
00299 <OperationType>Update</OperationType>
00300 <OrderAcknowledgement>
00301 <AmazonOrderID>' . $order->getExtOrderId() . '</AmazonOrderID>
00302 <StatusCode>Failure</StatusCode>
00303 </OrderAcknowledgement>
00304 </Message>
00305 </AmazonEnvelope>';
00306
00307 $params = array(
00308 'merchant' => $this->getMerchantInfo(),
00309 'messageType' => self::MESSAGE_TYPE_ACKNOWLEDGEMENT,
00310 'doc' => $this->_createAttachment($_document)
00311 );
00312
00313 $this->_proccessRequest('postDocument', $params);
00314 return $this->_result;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324 public function refund($payment, $amount)
00325 {
00326 $_document = '<?xml version="1.0" encoding="UTF-8"?>
00327 <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
00328 <Header>
00329 <DocumentVersion>1.01</DocumentVersion>
00330 <MerchantIdentifier>' . $this->getMerchantIdentifier() . '</MerchantIdentifier>
00331 </Header>
00332 <MessageType>OrderAdjustment</MessageType>';
00333
00334 $_shippingAmount = $payment->getCreditmemo()->getShippingAmount();
00335 $_messageId = 1;
00336 foreach ($payment->getCreditmemo()->getAllItems() as $item) {
00337
00338 if ($item->getOrderItem()->getParentItemId()) {
00339 continue;
00340 }
00341
00342 $shipping = 0;
00343 $amazon_amounts = unserialize($item->getOrderItem()->getProductOptionByCode('amazon_amounts'));
00344 if ($amazon_amounts['shipping'] > $_shippingAmount) {
00345 $shipping = $_shippingAmount;
00346 } else {
00347 $shipping = $amazon_amounts['shipping'];
00348 }
00349 $_shippingAmount -= $shipping;
00350
00351 $_document .= '<Message>
00352 <MessageID>' . $_messageId . '</MessageID>
00353 <OrderAdjustment>
00354 <AmazonOrderID>' . $payment->getOrder()->getExtOrderId() . '</AmazonOrderID>
00355 <AdjustedItem>
00356 <AmazonOrderItemCode>'. $item->getOrderItem()->getExtOrderItemId() . '</AmazonOrderItemCode>
00357 <AdjustmentReason>GeneralAdjustment</AdjustmentReason>
00358 <ItemPriceAdjustments>
00359 <Component>
00360 <Type>Principal</Type>
00361 <Amount currency="USD">' . $this->formatAmount($item->getBaseRowTotal()) . '</Amount>
00362 </Component>
00363 <Component>
00364 <Type>Tax</Type>
00365 <Amount currency="USD">' . $this->formatAmount($item->getBaseTaxAmount()) . '</Amount>
00366 </Component>'
00367 .'<Component>
00368 <Type>Shipping</Type>
00369 <Amount currency="USD">' . $this->formatAmount($shipping) . '</Amount>
00370 </Component>'
00371 .'</ItemPriceAdjustments>';
00372 $_document .= '</AdjustedItem>
00373 </OrderAdjustment>
00374 </Message>';
00375 $_messageId++;
00376 }
00377
00378 $_document .= '</AmazonEnvelope>';
00379 $params = array(
00380 'merchant' => $this->getMerchantInfo(),
00381 'messageType' => self::MESSAGE_TYPE_ADJUSTMENT,
00382 'doc' => $this->_createAttachment($_document)
00383 );
00384 $this->_proccessRequest('postDocument', $params);
00385 return $this->_result;
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 public function confirmShipment($aOrderId, $carrierName, $shippingMethod, $items, $trackNumber = '')
00399 {
00400 $fulfillmentDate = gmdate('Y-m-d\TH:i:s');
00401 $_document = '<?xml version="1.0" encoding="UTF-8"?>
00402 <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
00403 <Header>
00404 <DocumentVersion>1.01</DocumentVersion>
00405 <MerchantIdentifier>' . $this->getMerchantIdentifier() . '</MerchantIdentifier>
00406 </Header>
00407 <MessageType>OrderFulfillment</MessageType>
00408 <Message>
00409 <MessageID>1</MessageID>
00410 <OrderFulfillment>
00411 <AmazonOrderID>' . $aOrderId . '</AmazonOrderID>
00412 <FulfillmentDate>' . $fulfillmentDate . '</FulfillmentDate>
00413 <FulfillmentData>
00414 <CarrierName>' . strtoupper($carrierName) . '</CarrierName>
00415 <ShippingMethod>' . $shippingMethod . '</ShippingMethod>
00416 <ShipperTrackingNumber>' . $trackNumber .'</ShipperTrackingNumber>
00417 </FulfillmentData>';
00418 foreach ($items as $item) {
00419 $_document .= '<Item>
00420 <AmazonOrderItemCode>' . $item['id'] . '</AmazonOrderItemCode>
00421 <Quantity>' . $item['qty'] . '</Quantity>
00422 </Item>';
00423 }
00424 $_document .= '</OrderFulfillment>
00425 </Message>
00426 </AmazonEnvelope>';
00427 $params = array(
00428 'merchant' => $this->getMerchantInfo(),
00429 'messageType' => self::MESSAGE_TYPE_FULFILLMENT,
00430 'doc' => $this->_createAttachment($_document)
00431 );
00432 $this->_proccessRequest('postDocument', $params);
00433 return $this->_result;
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 public function sendTrackNumber($order, $carrierCode, $carrierMethod, $trackNumber)
00446 {
00447 $fulfillmentDate = gmdate('Y-m-d\TH:i:s');
00448 $_document = '<?xml version="1.0" encoding="UTF-8"?>
00449 <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
00450 <Header>
00451 <DocumentVersion>1.01</DocumentVersion>
00452 <MerchantIdentifier>' . $this->getMerchantIdentifier() . '</MerchantIdentifier>
00453 </Header>
00454 <MessageType>OrderFulfillment</MessageType>';
00455 $_document .= '<Message>
00456 <MessageID>1</MessageID>
00457 <OrderFulfillment>
00458 <AmazonOrderID>' . $order->getExtOrderId() . '</AmazonOrderID>
00459 <FulfillmentDate>' . $fulfillmentDate . '</FulfillmentDate>
00460 <FulfillmentData>
00461 <CarrierCode>' . $carrierCode . '</CarrierCode>
00462 <ShippingMethod>' . $carrierMethod . '</ShippingMethod>
00463 <ShipperTrackingNumber>' . $trackNumber .'</ShipperTrackingNumber>
00464 </FulfillmentData>
00465 </OrderFulfillment>
00466 </Message>';
00467 $_document .= '</AmazonEnvelope>';
00468 $params = array(
00469 'merchant' => $this->getMerchantInfo(),
00470 'messageType' => self::MESSAGE_TYPE_FULFILLMENT,
00471 'doc' => $this->_createAttachment($_document)
00472 );
00473 $this->_proccessRequest('postDocument', $params);
00474 return $this->_result;
00475 }
00476 }