ClientTest.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. <?php
  2. namespace AmazonPay;
  3. require_once 'AmazonPay/Client.php';
  4. require_once 'AmazonPay/ResponseParser.php';
  5. require_once 'Signature.php';
  6. class ClientTest extends \PHPUnit_Framework_TestCase
  7. {
  8. private $configParams = array(
  9. 'merchant_id' => 'MERCHANT1234567',
  10. 'access_key' => 'ABCDEFGHI1JKLMN2O7',
  11. 'secret_key' => "abc123Def456gHi789jKLmpQ987rstu6vWxyz",
  12. 'currency_code' => 'usd',
  13. 'client_id' => 'amzn1.application-oa2-client.45789c45a8f34927830be1d9e029f480',
  14. 'region' => 'us',
  15. 'sandbox' => true,
  16. 'platform_id' => 'test',
  17. 'application_name' => 'sdk testing',
  18. 'application_version' => '1.0',
  19. 'proxy_host' => null,
  20. 'proxy_port' => -1,
  21. 'proxy_username' => null,
  22. 'proxy_Password' => null
  23. );
  24. public function testConfigArray()
  25. {
  26. // Test that trimmimg isn't converting the Boolean to a string
  27. $client = new Client($this->configParams);
  28. $this->assertTrue((bool)$client->__get('sandbox'));
  29. // Test four cases in which sandbox is in constructor with an array
  30. $client = new Client(array('sandbox' => false));
  31. $this->assertFalse((bool)$client->__get('sandbox'));
  32. try {
  33. $client = new Client(array('sandbox' => 'false'));
  34. } catch (\Exception $expected) {
  35. $this->assertRegExp('/should be a boolean value/i', strval($expected));
  36. }
  37. $client = new Client(array('sandbox' => true));
  38. $this->assertTrue((bool)$client->__get('sandbox'));
  39. try {
  40. $client = new Client(array('sandbox' => 'true'));
  41. } catch (\Exception $expected) {
  42. $this->assertRegExp('/should be a boolean value/i', strval($expected));
  43. }
  44. // Test that string trimming is working as intended
  45. $client = new Client(array(
  46. 'region' => 'us ', // two spaces at end
  47. 'currency_code' => ' usd', // two spaces at beginning
  48. 'client_id' => ' A113 ' // two spaces and beginning and end
  49. ));
  50. $this->assertEquals('us', $client->__get('region'));
  51. $this->assertEquals('usd', $client->__get('currency_code'));
  52. $this->assertEquals('A113', $client->__get('client_id'));
  53. $this->assertFalse((bool)$client->__get('sandbox'));
  54. // Unclear what is is actually doing, exception doesn't get thrown, consider removing
  55. try {
  56. $client = new Client($this->configParams);
  57. } catch (\Exception $expected) {
  58. $this->assertRegExp('/is not a Json File or the Json File./i', strval($expected));
  59. }
  60. // Test passing in invalid keys to constructor
  61. try {
  62. $configParams = array(
  63. 'a' => 'A',
  64. 'b' => 'B'
  65. );
  66. $client = new Client($configParams);
  67. } catch (\Exception $expected) {
  68. $this->assertRegExp('/is either not part of the configuration or has incorrect Key name./i', strval($expected));
  69. }
  70. // Test passing in override service URL for MWS API endpoint
  71. $client = new Client(array('override_service_url' => 'https://over.ride'));
  72. $this->assertEquals('https://over.ride', $client->__get('override_service_url'));
  73. // Test passing in an empty array to construtor
  74. try {
  75. $configParams = array();
  76. $client = new Client($configParams);
  77. } catch (\Exception $expected) {
  78. $this->assertRegExp('/$config cannot be null./i', strval($expected));
  79. }
  80. }
  81. public function testJsonFile()
  82. {
  83. $configParams = "tst/unit/config/sandbox_true_bool.json";
  84. $client = new Client($configParams);
  85. $this->assertTrue((bool)$client->__get('sandbox'));
  86. $this->assertEquals('test_merchant_id', $client->__get('merchant_id'));
  87. $this->assertEquals('test_access_key', $client->__get('access_key'));
  88. $this->assertEquals('test_secret_key', $client->__get('secret_key'));
  89. $this->assertEquals('USD', $client->__get('currency_code'));
  90. $this->assertEquals('test_client_id', $client->__get('client_id'));
  91. $this->assertEquals('us', $client->__get('region'));
  92. $this->assertEquals('sdk testing', $client->__get('application_name'));
  93. $this->assertEquals('1.0', $client->__get('application_version'));
  94. try {
  95. $configParams = "tst/unit/config/sandbox_true_string.json";
  96. $client = new Client($configParams);
  97. } catch (\Exception $expected) {
  98. $this->assertRegExp('/should be a boolean value/i', strval($expected));
  99. }
  100. $configParams = "tst/unit/config/sandbox_false_bool.json";
  101. $client = new Client($configParams);
  102. $this->assertFalse((bool)$client->__get('sandbox'));
  103. $configParams = "tst/unit/config/sandbox_none.json";
  104. $client = new Client($configParams);
  105. $this->assertFalse((bool)$client->__get('sandbox'));
  106. try {
  107. $configParams = "tst/unit/config/sandbox_false_string.json";
  108. $client = new Client($configParams);
  109. } catch (\Exception $expected) {
  110. $this->assertRegExp('/should be a boolean value/i', strval($expected));
  111. }
  112. try {
  113. $configParams = "abc.json";
  114. $client = new Client($configParams);
  115. } catch (\Exception $expected) {
  116. $this->assertRegExp('/is not a Json File path or the Json File./i', strval($expected));
  117. }
  118. }
  119. public function testSandboxSetter()
  120. {
  121. $client = new Client($this->configParams);
  122. try {
  123. $client->setSandbox(true);
  124. } catch (\Exception $expected) {
  125. $this->assertRegExp('/and should be a boolean value./i', strval($expected));
  126. }
  127. try {
  128. $client->setSandbox('string value');
  129. } catch (\Exception $expected) {
  130. $this->assertRegExp('/and should be a boolean value./i', strval($expected));
  131. }
  132. }
  133. public function testGetOrderReferenceDetails()
  134. {
  135. $client = new Client($this->configParams);
  136. $fieldMappings = array(
  137. 'merchant_id' => 'SellerId',
  138. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  139. 'address_consent_token' => 'AddressConsentToken',
  140. 'access_token' => 'AccessToken',
  141. 'mws_auth_token' => 'MWSAuthToken'
  142. );
  143. $action = 'GetOrderReferenceDetails';
  144. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  145. $expectedParameters = $parameters['expectedParameters'];
  146. $apiCallParams = $parameters['apiCallParams'];
  147. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  148. $response = $client->getOrderReferenceDetails($apiCallParams);
  149. $apiParametersString = $client->getParameters();
  150. $this->assertEquals($apiParametersString, $expectedStringParams);
  151. }
  152. public function testListOrderReference()
  153. {
  154. $client = new Client($this->configParams);
  155. $fieldMappings = array(
  156. 'merchant_id' => 'SellerId',
  157. 'mws_auth_token' => 'MWSAuthToken',
  158. 'query_id' => 'QueryId',
  159. 'query_id_type' => 'QueryIdType',
  160. 'page_size' => 'PageSize',
  161. 'created_start_time' => 'CreatedTimeRange.StartTime',
  162. 'created_end_time' => 'CreatedTimeRange.EndTime',
  163. 'sort_order' => 'SortOrder',
  164. 'order_status_list' => array()
  165. );
  166. $action = 'ListOrderReference';
  167. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  168. $expectedParameters = $parameters['expectedParameters'];
  169. $expectedParameters['OrderReferenceStatusListFilter.OrderReferenceStatus.1'] = 'Open';
  170. $expectedParameters['OrderReferenceStatusListFilter.OrderReferenceStatus.2'] = 'Closed';
  171. $apiCallParams = $parameters['apiCallParams'];
  172. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  173. $response = $client->listOrderReference($apiCallParams);
  174. $apiParametersString = $client->getParameters();
  175. // Hack to remove mismatched Signature (due to param mismatch), then remove Signature from both to eliminate mismatch
  176. $apiParametersString = preg_replace("/&PaymentDomain=[^&]*/", "", $apiParametersString);
  177. $apiParametersString = preg_replace("/&Signature=[^&]*/", "", $apiParametersString);
  178. $expectedStringParams = preg_replace("/&Signature=[^&]*/", "", $expectedStringParams);
  179. $this->assertEquals($apiParametersString, $expectedStringParams);
  180. }
  181. public function testListOrderReferenceByNextToken()
  182. {
  183. $client = new Client($this->configParams);
  184. $fieldMappings = array(
  185. 'merchant_id' => 'SellerId',
  186. 'mws_auth_token' => 'MWSAuthToken',
  187. 'next_page_token' => 'NextPageToken'
  188. );
  189. $action = 'ListOrderReferenceByNextToken';
  190. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  191. $expectedParameters = $parameters['expectedParameters'];
  192. $apiCallParams = $parameters['apiCallParams'];
  193. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  194. $response = $client->listOrderReferenceByNextToken($apiCallParams);
  195. $apiParametersString = $client->getParameters();
  196. $this->assertEquals($apiParametersString, $expectedStringParams);
  197. }
  198. public function testSetOrderReferenceDetails()
  199. {
  200. $client = new Client($this->configParams);
  201. $fieldMappings = array(
  202. 'Merchant_Id' => 'SellerId',
  203. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  204. 'amount' => 'OrderReferenceAttributes.OrderTotal.Amount',
  205. 'currency_code' => 'OrderReferenceAttributes.OrderTotal.CurrencyCode',
  206. 'platform_id' => 'OrderReferenceAttributes.PlatformId',
  207. 'seller_note' => 'OrderReferenceAttributes.SellerNote',
  208. 'seller_order_id' => 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId',
  209. 'store_name' => 'OrderReferenceAttributes.SellerOrderAttributes.StoreName',
  210. 'custom_information' => 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation',
  211. 'supplementary_data' => 'OrderReferenceAttributes.SellerOrderAttributes.SupplementaryData',
  212. 'request_payment_authorization' => 'OrderReferenceAttributes.RequestPaymentAuthorization',
  213. 'mws_auth_token' => 'MWSAuthToken'
  214. );
  215. $action = 'SetOrderReferenceDetails';
  216. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  217. $expectedParameters = $parameters['expectedParameters'];
  218. $apiCallParams = $parameters['apiCallParams'];
  219. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  220. $response = $client->setOrderReferenceDetails($apiCallParams);
  221. $apiParametersString = $client->getParameters();
  222. $this->assertEquals($apiParametersString, $expectedStringParams);
  223. }
  224. public function testSetOrderAttributesBeforeConfirm()
  225. {
  226. $client = new Client($this->configParams);
  227. $fieldMappings = array(
  228. 'merchant_id' => 'SellerId',
  229. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  230. 'amount' => 'OrderAttributes.OrderTotal.Amount',
  231. 'currency_code' => 'OrderAttributes.OrderTotal.CurrencyCode',
  232. 'platform_id' => 'OrderAttributes.PlatformId',
  233. 'seller_note' => 'OrderAttributes.SellerNote',
  234. 'seller_order_id' => 'OrderAttributes.SellerOrderAttributes.SellerOrderId',
  235. 'store_name' => 'OrderAttributes.SellerOrderAttributes.StoreName',
  236. 'custom_information' => 'OrderAttributes.SellerOrderAttributes.CustomInformation',
  237. 'supplementary_data' => 'OrderAttributes.SellerOrderAttributes.SupplementaryData',
  238. 'request_payment_authorization' => 'OrderAttributes.RequestPaymentAuthorization',
  239. 'payment_service_provider_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderId',
  240. 'payment_service_provider_order_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderOrderId',
  241. 'order_item_categories' => array(),
  242. 'mws_auth_token' => 'MWSAuthToken'
  243. );
  244. $action = 'SetOrderAttributes';
  245. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  246. $expectedParameters = $parameters['expectedParameters'];
  247. $expectedParameters['OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.1'] = 'Antiques';
  248. $expectedParameters['OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.2'] = 'Electronics';
  249. $apiCallParams = $parameters['apiCallParams'];
  250. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  251. $response = $client->setOrderAttributes($apiCallParams);
  252. $apiParametersString = $client->getParameters();
  253. $this->assertEquals($apiParametersString, $expectedStringParams);
  254. }
  255. /* Call is same as BeforeConfirm call except the amount and currency_code fields are omitted */
  256. public function testSetOrderAttributesAfterConfirm()
  257. {
  258. $client = new Client($this->configParams);
  259. $fieldMappings = array(
  260. 'merchant_id' => 'SellerId',
  261. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  262. 'platform_id' => 'OrderAttributes.PlatformId',
  263. 'seller_note' => 'OrderAttributes.SellerNote',
  264. 'seller_order_id' => 'OrderAttributes.SellerOrderAttributes.SellerOrderId',
  265. 'store_name' => 'OrderAttributes.SellerOrderAttributes.StoreName',
  266. 'custom_information' => 'OrderAttributes.SellerOrderAttributes.CustomInformation',
  267. 'request_payment_authorization' => 'OrderAttributes.RequestPaymentAuthorization',
  268. 'payment_service_provider_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderId',
  269. 'payment_service_provider_order_id' => 'OrderAttributes.PaymentServiceProviderAttributes.PaymentServiceProviderOrderId',
  270. 'order_item_categories' => array(),
  271. 'mws_auth_token' => 'MWSAuthToken'
  272. );
  273. $action = 'SetOrderAttributes';
  274. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  275. $expectedParameters = $parameters['expectedParameters'];
  276. $expectedParameters['OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.1'] = 'Antiques';
  277. $expectedParameters['OrderAttributes.SellerOrderAttributes.OrderItemCategories.OrderItemCategory.2'] = 'Electronics';
  278. $apiCallParams = $parameters['apiCallParams'];
  279. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  280. $response = $client->setOrderAttributes($apiCallParams);
  281. $apiParametersString = $client->getParameters();
  282. $this->assertEquals($apiParametersString, $expectedStringParams);
  283. }
  284. public function testConfirmOrderReferenceWithAllSCA()
  285. {
  286. $client = new Client($this->configParams);
  287. $fieldMappings = array(
  288. 'merchant_id' => 'SellerId',
  289. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  290. 'mws_auth_token' => 'MWSAuthToken',
  291. 'success_url' => 'SuccessUrl',
  292. 'failure_url' => 'FailureUrl',
  293. 'authorization_amount' => 'AuthorizationAmount.Amount',
  294. 'currency_code' => 'AuthorizationAmount.CurrencyCode'
  295. );
  296. $action = 'ConfirmOrderReference';
  297. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  298. $expectedParameters = $parameters['expectedParameters'];
  299. $apiCallParams = $parameters['apiCallParams'];
  300. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  301. $response = $client->confirmOrderReference($apiCallParams);
  302. $apiParametersString = $client->getParameters();
  303. $this->assertEquals($apiParametersString, $expectedStringParams);
  304. }
  305. public function testConfirmOrderReferenceWithAllButCurrencyCodeSCA()
  306. {
  307. $client = new Client($this->configParams);
  308. $fieldMappings = array(
  309. 'merchant_id' => 'SellerId',
  310. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  311. 'mws_auth_token' => 'MWSAuthToken',
  312. 'success_url' => 'SuccessUrl',
  313. 'failure_url' => 'FailureUrl',
  314. 'authorization_amount' => 'AuthorizationAmount.Amount'
  315. );
  316. $action = 'ConfirmOrderReference';
  317. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  318. $expectedParameters = $parameters['expectedParameters'];
  319. $apiCallParams = $parameters['apiCallParams'];
  320. $expectedParameters['AuthorizationAmount.CurrencyCode'] = 'USD'; # default from client
  321. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  322. $response = $client->confirmOrderReference($apiCallParams);
  323. $apiParametersString = $client->getParameters();
  324. $this->assertEquals($apiParametersString, $expectedStringParams);
  325. }
  326. public function testConfirmOrderReferenceWithUrlSCA()
  327. {
  328. $client = new Client($this->configParams);
  329. $fieldMappings = array(
  330. 'merchant_id' => 'SellerId',
  331. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  332. 'mws_auth_token' => 'MWSAuthToken',
  333. 'success_url' => 'SuccessUrl',
  334. 'failure_url' => 'FailureUrl'
  335. );
  336. $action = 'ConfirmOrderReference';
  337. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  338. $expectedParameters = $parameters['expectedParameters'];
  339. $apiCallParams = $parameters['apiCallParams'];
  340. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  341. $response = $client->confirmOrderReference($apiCallParams);
  342. $apiParametersString = $client->getParameters();
  343. $this->assertEquals($apiParametersString, $expectedStringParams);
  344. }
  345. public function testConfirmOrderReferenceWithoutSCA()
  346. {
  347. $client = new Client($this->configParams);
  348. $fieldMappings = array(
  349. 'merchant_id' => 'SellerId',
  350. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  351. 'mws_auth_token' => 'MWSAuthToken'
  352. );
  353. $action = 'ConfirmOrderReference';
  354. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  355. $expectedParameters = $parameters['expectedParameters'];
  356. $apiCallParams = $parameters['apiCallParams'];
  357. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  358. $response = $client->confirmOrderReference($apiCallParams);
  359. $apiParametersString = $client->getParameters();
  360. $this->assertEquals($apiParametersString, $expectedStringParams);
  361. }
  362. public function testCancelOrderReference()
  363. {
  364. $client = new Client($this->configParams);
  365. $fieldMappings = array(
  366. 'merchant_id' => 'SellerId',
  367. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  368. 'cancelation_reason' => 'CancelationReason',
  369. 'mws_auth_token' => 'MWSAuthToken'
  370. );
  371. $action = 'CancelOrderReference';
  372. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  373. $expectedParameters = $parameters['expectedParameters'];
  374. $apiCallParams = $parameters['apiCallParams'];
  375. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  376. $response = $client->cancelOrderReference($apiCallParams);
  377. $apiParametersString = $client->getParameters();
  378. $this->assertEquals($apiParametersString, $expectedStringParams);
  379. }
  380. public function testCloseOrderReference()
  381. {
  382. $client = new Client($this->configParams);
  383. $fieldMappings = array(
  384. 'merchant_id' => 'SellerId',
  385. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  386. 'closure_reason' => 'ClosureReason',
  387. 'mws_auth_token' => 'MWSAuthToken'
  388. );
  389. $action = 'CloseOrderReference';
  390. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  391. $expectedParameters = $parameters['expectedParameters'];
  392. $apiCallParams = $parameters['apiCallParams'];
  393. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  394. $response = $client->closeOrderReference($apiCallParams);
  395. $apiParametersString = $client->getParameters();
  396. $this->assertEquals($apiParametersString, $expectedStringParams);
  397. }
  398. public function testCloseAuthorization()
  399. {
  400. $client = new Client($this->configParams);
  401. $fieldMappings = array(
  402. 'merchant_id' => 'SellerId',
  403. 'amazon_authorization_id' => 'AmazonAuthorizationId',
  404. 'closure_reason' => 'ClosureReason',
  405. 'mws_auth_token' => 'MWSAuthToken'
  406. );
  407. $action = 'CloseAuthorization';
  408. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  409. $expectedParameters = $parameters['expectedParameters'];
  410. $apiCallParams = $parameters['apiCallParams'];
  411. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  412. $response = $client->closeAuthorization($apiCallParams);
  413. $apiParametersString = $client->getParameters();
  414. $this->assertEquals($apiParametersString, $expectedStringParams);
  415. }
  416. public function testAuthorize()
  417. {
  418. $client = new Client($this->configParams);
  419. $fieldMappings = array(
  420. 'merchant_id' => 'SellerId',
  421. 'amazon_order_reference_id' => 'AmazonOrderReferenceId',
  422. 'authorization_amount' => 'AuthorizationAmount.Amount',
  423. 'currency_code' => 'AuthorizationAmount.CurrencyCode',
  424. 'authorization_reference_id' => 'AuthorizationReferenceId',
  425. 'capture_now' => 'CaptureNow',
  426. 'seller_authorization_note' => 'SellerAuthorizationNote',
  427. 'transaction_timeout' => 'TransactionTimeout',
  428. 'soft_descriptor' => 'SoftDescriptor',
  429. 'mws_auth_token' => 'MWSAuthToken'
  430. );
  431. $action = 'Authorize';
  432. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  433. $expectedParameters = $parameters['expectedParameters'];
  434. $apiCallParams = $parameters['apiCallParams'];
  435. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  436. $response = $client->authorize($apiCallParams);
  437. $apiParametersString = $client->getParameters();
  438. $this->assertEquals($apiParametersString, $expectedStringParams);
  439. }
  440. public function testGetAuthorizationDetails()
  441. {
  442. $client = new Client($this->configParams);
  443. $fieldMappings = array(
  444. 'merchant_id' => 'SellerId',
  445. 'amazon_authorization_id' => 'AmazonAuthorizationId',
  446. 'mws_auth_token' => 'MWSAuthToken'
  447. );
  448. $action = 'GetAuthorizationDetails';
  449. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  450. $expectedParameters = $parameters['expectedParameters'];
  451. $apiCallParams = $parameters['apiCallParams'];
  452. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  453. $response = $client->getAuthorizationDetails($apiCallParams);
  454. $apiParametersString = $client->getParameters();
  455. $this->assertEquals($apiParametersString, $expectedStringParams);
  456. }
  457. public function testCapture()
  458. {
  459. $client = new Client($this->configParams);
  460. $fieldMappings = array(
  461. 'merchant_id' => 'SellerId',
  462. 'amazon_authorization_id' => 'AmazonAuthorizationId',
  463. 'capture_amount' => 'CaptureAmount.Amount',
  464. 'currency_code' => 'CaptureAmount.CurrencyCode',
  465. 'capture_reference_id' => 'CaptureReferenceId',
  466. 'seller_capture_note' => 'SellerCaptureNote',
  467. 'soft_descriptor' => 'SoftDescriptor',
  468. 'mws_auth_token' => 'MWSAuthToken'
  469. );
  470. $action = 'Capture';
  471. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  472. $expectedParameters = $parameters['expectedParameters'];
  473. $apiCallParams = $parameters['apiCallParams'];
  474. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  475. $response = $client->capture($apiCallParams);
  476. $apiParametersString = $client->getParameters();
  477. $this->assertEquals($apiParametersString, $expectedStringParams);
  478. }
  479. public function testGetCaptureDetails()
  480. {
  481. $client = new Client($this->configParams);
  482. $fieldMappings = array(
  483. 'merchant_id' => 'SellerId',
  484. 'amazon_capture_id' => 'AmazonCaptureId',
  485. 'mws_auth_token' => 'MWSAuthToken'
  486. );
  487. $action = 'GetCaptureDetails';
  488. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  489. $expectedParameters = $parameters['expectedParameters'];
  490. $apiCallParams = $parameters['apiCallParams'];
  491. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  492. $response = $client->getCaptureDetails($apiCallParams);
  493. $apiParametersString = $client->getParameters();
  494. $this->assertEquals($apiParametersString, $expectedStringParams);
  495. }
  496. public function testRefund()
  497. {
  498. $client = new Client($this->configParams);
  499. $fieldMappings = array(
  500. 'merchant_id' => 'SellerId',
  501. 'amazon_capture_id' => 'AmazonCaptureId',
  502. 'refund_reference_id' => 'RefundReferenceId',
  503. 'refund_amount' => 'RefundAmount.Amount',
  504. 'currency_code' => 'RefundAmount.CurrencyCode',
  505. 'seller_refund_note' => 'SellerRefundNote',
  506. 'soft_descriptor' => 'SoftDescriptor',
  507. 'mws_auth_token' => 'MWSAuthToken'
  508. );
  509. $action = 'Refund';
  510. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  511. $expectedParameters = $parameters['expectedParameters'];
  512. $apiCallParams = $parameters['apiCallParams'];
  513. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  514. $response = $client->refund($apiCallParams);
  515. $apiParametersString = $client->getParameters();
  516. $this->assertEquals($apiParametersString, $expectedStringParams);
  517. }
  518. public function testGetRefundDetails()
  519. {
  520. $client = new Client($this->configParams);
  521. $fieldMappings = array(
  522. 'merchant_id' => 'SellerId',
  523. 'amazon_refund_id' => 'AmazonRefundId',
  524. 'mws_auth_token' => 'MWSAuthToken'
  525. );
  526. $action = 'GetRefundDetails';
  527. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  528. $expectedParameters = $parameters['expectedParameters'];
  529. $apiCallParams = $parameters['apiCallParams'];
  530. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  531. $response = $client->getRefundDetails($apiCallParams);
  532. $apiParametersString = $client->getParameters();
  533. $this->assertEquals($apiParametersString, $expectedStringParams);
  534. }
  535. public function testGetMerchantAccountStatus()
  536. {
  537. $client = new Client($this->configParams);
  538. $fieldMappings = array(
  539. 'merchant_id' => 'SellerId',
  540. 'mws_auth_token' => 'MWSAuthToken'
  541. );
  542. $action = 'GetMerchantAccountStatus';
  543. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  544. $expectedParameters = $parameters['expectedParameters'];
  545. $apiCallParams = $parameters['apiCallParams'];
  546. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  547. $response = $client->getMerchantAccountStatus($apiCallParams);
  548. $apiParametersString = $client->getParameters();
  549. $this->assertEquals($apiParametersString, $expectedStringParams);
  550. }
  551. public function testGetServiceStatus()
  552. {
  553. $client = new Client($this->configParams);
  554. $fieldMappings = array(
  555. 'merchant_id' => 'SellerId',
  556. 'mws_auth_token' => 'MWSAuthToken'
  557. );
  558. $action = 'GetServiceStatus';
  559. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  560. $expectedParameters = $parameters['expectedParameters'];
  561. $apiCallParams = $parameters['apiCallParams'];
  562. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  563. $response = $client->getServiceStatus($apiCallParams);
  564. $apiParametersString = $client->getParameters();
  565. $this->assertEquals($apiParametersString, $expectedStringParams);
  566. }
  567. public function testCreateOrderReferenceForId()
  568. {
  569. $client = new Client($this->configParams);
  570. $fieldMappings = array(
  571. 'merchant_id' => 'SellerId',
  572. 'id' => 'Id',
  573. 'id_type' => 'IdType',
  574. 'inherit_shipping_address' => 'InheritShippingAddress',
  575. 'confirm_now' => 'ConfirmNow',
  576. 'amount' => 'OrderReferenceAttributes.OrderTotal.Amount',
  577. 'currency_code' => 'OrderReferenceAttributes.OrderTotal.CurrencyCode',
  578. 'platform_id' => 'OrderReferenceAttributes.PlatformId',
  579. 'seller_note' => 'OrderReferenceAttributes.SellerNote',
  580. 'seller_order_id' => 'OrderReferenceAttributes.SellerOrderAttributes.SellerOrderId',
  581. 'store_name' => 'OrderReferenceAttributes.SellerOrderAttributes.StoreName',
  582. 'custom_information' => 'OrderReferenceAttributes.SellerOrderAttributes.CustomInformation',
  583. 'mws_auth_token' => 'MWSAuthToken'
  584. );
  585. $action = 'CreateOrderReferenceForId';
  586. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  587. $expectedParameters = $parameters['expectedParameters'];
  588. $apiCallParams = $parameters['apiCallParams'];
  589. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  590. $response = $client->createOrderReferenceForId($apiCallParams);
  591. $apiParametersString = $client->getParameters();
  592. $this->assertEquals($apiParametersString, $expectedStringParams);
  593. }
  594. public function testGetBillingAgreementDetails()
  595. {
  596. $client = new Client($this->configParams);
  597. $fieldMappings = array(
  598. 'merchant_id' => 'SellerId',
  599. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  600. 'address_consent_token' => 'AddressConsentToken',
  601. 'access_token' => 'AccessToken',
  602. 'mws_auth_token' => 'MWSAuthToken'
  603. );
  604. $action = 'GetBillingAgreementDetails';
  605. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  606. $expectedParameters = $parameters['expectedParameters'];
  607. $apiCallParams = $parameters['apiCallParams'];
  608. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  609. $response = $client->getBillingAgreementDetails($apiCallParams);
  610. $apiParametersString = $client->getParameters();
  611. $this->assertEquals($apiParametersString, $expectedStringParams);
  612. }
  613. public function testSetBillingAgreementDetailsWithoutSCA()
  614. {
  615. $client = new Client($this->configParams);
  616. $fieldMappings = array(
  617. 'merchant_id' => 'SellerId',
  618. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  619. 'platform_id' => 'BillingAgreementAttributes.PlatformId',
  620. 'seller_note' => 'BillingAgreementAttributes.SellerNote',
  621. 'seller_billing_agreement_id' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.SellerBillingAgreementId',
  622. 'custom_information' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.CustomInformation',
  623. 'store_name' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.StoreName',
  624. 'mws_auth_token' => 'MWSAuthToken'
  625. );
  626. $action = 'SetBillingAgreementDetails';
  627. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  628. $expectedParameters = $parameters['expectedParameters'];
  629. $apiCallParams = $parameters['apiCallParams'];
  630. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  631. $response = $client->setBillingAgreementDetails($apiCallParams);
  632. $apiParametersString = $client->getParameters();
  633. $this->assertEquals($apiParametersString, $expectedStringParams);
  634. }
  635. public function testSetBillingAgreementDetailsWithSCA()
  636. {
  637. $client = new Client($this->configParams);
  638. $fieldMappings = array(
  639. 'merchant_id' => 'SellerId',
  640. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  641. 'platform_id' => 'BillingAgreementAttributes.PlatformId',
  642. 'seller_note' => 'BillingAgreementAttributes.SellerNote',
  643. 'seller_billing_agreement_id' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.SellerBillingAgreementId',
  644. 'custom_information' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.CustomInformation',
  645. 'store_name' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.StoreName',
  646. 'billing_agreement_type' => 'BillingAgreementAttributes.BillingAgreementType',
  647. 'subscription_amount' => 'BillingAgreementAttributes.SubscriptionAmount.Amount',
  648. 'currency_code' => 'BillingAgreementAttributes.SubscriptionAmount.CurrencyCode',
  649. 'mws_auth_token' => 'MWSAuthToken'
  650. );
  651. $action = 'SetBillingAgreementDetails';
  652. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  653. $expectedParameters = $parameters['expectedParameters'];
  654. $apiCallParams = $parameters['apiCallParams'];
  655. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  656. $response = $client->setBillingAgreementDetails($apiCallParams);
  657. $apiParametersString = $client->getParameters();
  658. $this->assertEquals($apiParametersString, $expectedStringParams);
  659. }
  660. public function testSetBillingAgreementDetailsWithSCAExceptCurrencyCode()
  661. {
  662. $client = new Client($this->configParams);
  663. $fieldMappings = array(
  664. 'merchant_id' => 'SellerId',
  665. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  666. 'platform_id' => 'BillingAgreementAttributes.PlatformId',
  667. 'seller_note' => 'BillingAgreementAttributes.SellerNote',
  668. 'seller_billing_agreement_id' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.SellerBillingAgreementId',
  669. 'custom_information' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.CustomInformation',
  670. 'store_name' => 'BillingAgreementAttributes.SellerBillingAgreementAttributes.StoreName',
  671. 'billing_agreement_type' => 'BillingAgreementAttributes.BillingAgreementType',
  672. 'subscription_amount' => 'BillingAgreementAttributes.SubscriptionAmount.Amount',
  673. 'mws_auth_token' => 'MWSAuthToken'
  674. );
  675. $action = 'SetBillingAgreementDetails';
  676. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  677. $expectedParameters = $parameters['expectedParameters'];
  678. $apiCallParams = $parameters['apiCallParams'];
  679. $expectedParameters['BillingAgreementAttributes.SubscriptionAmount.CurrencyCode'] = 'USD'; # default from client
  680. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  681. $response = $client->setBillingAgreementDetails($apiCallParams);
  682. $apiParametersString = $client->getParameters();
  683. $this->assertEquals($apiParametersString, $expectedStringParams);
  684. }
  685. public function testConfirmBillingAgreementWithoutSCA()
  686. {
  687. $client = new Client($this->configParams);
  688. $fieldMappings = array(
  689. 'merchant_id' => 'SellerId',
  690. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  691. 'mws_auth_token' => 'MWSAuthToken'
  692. );
  693. $action = 'ConfirmBillingAgreement';
  694. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  695. $expectedParameters = $parameters['expectedParameters'];
  696. $apiCallParams = $parameters['apiCallParams'];
  697. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  698. $response = $client->confirmBillingAgreement($apiCallParams);
  699. $apiParametersString = $client->getParameters();
  700. $this->assertEquals($apiParametersString, $expectedStringParams);
  701. }
  702. public function testConfirmBillingAgreementWithSCA()
  703. {
  704. $client = new Client($this->configParams);
  705. $fieldMappings = array(
  706. 'merchant_id' => 'SellerId',
  707. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  708. 'success_url' => 'SuccessUrl',
  709. 'failure_url' => 'FailureUrl',
  710. 'mws_auth_token' => 'MWSAuthToken'
  711. );
  712. $action = 'ConfirmBillingAgreement';
  713. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  714. $expectedParameters = $parameters['expectedParameters'];
  715. $apiCallParams = $parameters['apiCallParams'];
  716. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  717. $response = $client->confirmBillingAgreement($apiCallParams);
  718. $apiParametersString = $client->getParameters();
  719. $this->assertEquals($apiParametersString, $expectedStringParams);
  720. }
  721. public function testValidateBillingAgreement()
  722. {
  723. $client = new Client($this->configParams);
  724. $fieldMappings = array(
  725. 'merchant_id' => 'SellerId',
  726. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  727. 'mws_auth_token' => 'MWSAuthToken'
  728. );
  729. $action = 'ValidateBillingAgreement';
  730. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  731. $expectedParameters = $parameters['expectedParameters'];
  732. $apiCallParams = $parameters['apiCallParams'];
  733. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  734. $response = $client->validateBillingAgreement($apiCallParams);
  735. $apiParametersString = $client->getParameters();
  736. $this->assertEquals($apiParametersString, $expectedStringParams);
  737. }
  738. public function testAuthorizeOnBillingAgreement()
  739. {
  740. $client = new Client($this->configParams);
  741. $fieldMappings = array(
  742. 'merchant_id' => 'SellerId',
  743. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  744. 'authorization_reference_id' => 'AuthorizationReferenceId',
  745. 'authorization_amount' => 'AuthorizationAmount.Amount',
  746. 'currency_code' => 'AuthorizationAmount.CurrencyCode',
  747. 'seller_authorization_note' => 'SellerAuthorizationNote',
  748. 'transaction_timeout' => 'TransactionTimeout',
  749. 'capture_now' => 'CaptureNow',
  750. 'soft_descriptor' => 'SoftDescriptor',
  751. 'seller_note' => 'SellerNote',
  752. 'platform_id' => 'PlatformId',
  753. 'custom_information' => 'SellerOrderAttributes.CustomInformation',
  754. 'seller_order_id' => 'SellerOrderAttributes.SellerOrderId',
  755. 'store_name' => 'SellerOrderAttributes.StoreName',
  756. 'inherit_shipping_address' => 'InheritShippingAddress',
  757. 'mws_auth_token' => 'MWSAuthToken'
  758. );
  759. $action = 'AuthorizeOnBillingAgreement';
  760. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  761. $expectedParameters = $parameters['expectedParameters'];
  762. $apiCallParams = $parameters['apiCallParams'];
  763. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  764. $response = $client->authorizeOnBillingAgreement($apiCallParams);
  765. $apiParametersString = $client->getParameters();
  766. $this->assertEquals($apiParametersString, $expectedStringParams);
  767. }
  768. public function testCloseBillingAgreement()
  769. {
  770. $client = new Client($this->configParams);
  771. $fieldMappings = array(
  772. 'merchant_id' => 'SellerId',
  773. 'amazon_billing_agreement_id' => 'AmazonBillingAgreementId',
  774. 'closure_reason' => 'ClosureReason',
  775. 'mws_auth_token' => 'MWSAuthToken'
  776. );
  777. $action = 'CloseBillingAgreement';
  778. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  779. $expectedParameters = $parameters['expectedParameters'];
  780. $apiCallParams = $parameters['apiCallParams'];
  781. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  782. $response = $client->closeBillingAgreement($apiCallParams);
  783. $apiParametersString = $client->getParameters();
  784. $this->assertEquals($apiParametersString, $expectedStringParams);
  785. }
  786. public function testGetMerchantNotificationConfiguration()
  787. {
  788. $client = new Client($this->configParams);
  789. $fieldMappings = array(
  790. 'merchant_id' => 'SellerId',
  791. 'mws_auth_token' => 'MWSAuthToken'
  792. );
  793. $action = 'GetMerchantNotificationConfiguration';
  794. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  795. $expectedParameters = $parameters['expectedParameters'];
  796. $apiCallParams = $parameters['apiCallParams'];
  797. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  798. $response = $client->getMerchantNotificationConfiguration($apiCallParams);
  799. $apiParametersString = $client->getParameters();
  800. $this->assertEquals($apiParametersString, $expectedStringParams);
  801. }
  802. public function testSetMerchantNotificationConfiguration()
  803. {
  804. $client = new Client($this->configParams);
  805. $fieldMappings = array(
  806. 'merchant_id' => 'SellerId',
  807. 'notification_configuration_list' => array(),
  808. 'mws_auth_token' => 'MWSAuthToken'
  809. );
  810. $action = 'SetMerchantNotificationConfiguration';
  811. $parameters = $this->setParametersAndPost($fieldMappings, $action);
  812. $expectedParameters = $parameters['expectedParameters'];
  813. $expectedParameters['NotificationConfigurationList.NotificationConfiguration.1.NotificationUrl'] = 'https://dev.null/one';
  814. $expectedParameters['NotificationConfigurationList.NotificationConfiguration.2.NotificationUrl'] = 'https://dev.null/two';
  815. $expectedParameters['NotificationConfigurationList.NotificationConfiguration.1.EventTypes.EventTypeList.1'] = 'ORDER_REFERENCE';
  816. $expectedParameters['NotificationConfigurationList.NotificationConfiguration.1.EventTypes.EventTypeList.2'] = 'PAYMENT_AUTHORIZE';
  817. $expectedParameters['NotificationConfigurationList.NotificationConfiguration.2.EventTypes.EventTypeList.1'] = 'ALL';
  818. $apiCallParams = $parameters['apiCallParams'];
  819. $expectedStringParams = $this->callPrivateMethod($client, 'calculateSignatureAndParametersToString', $expectedParameters);
  820. $response = $client->setMerchantNotificationConfiguration($apiCallParams);
  821. $apiParametersString = $client->getParameters();
  822. $this->assertEquals($apiParametersString, $expectedStringParams);
  823. }
  824. public function testCharge()
  825. {
  826. $client = new Client($this->configParams);
  827. $apiCallParams = array('amazon_reference_id' => 'S01-TEST');
  828. try {
  829. $client = new Client($this->configParams);
  830. $apiCallParams = array('amazon_reference_id' => '');
  831. $client->charge($apiCallParams);
  832. } catch (\Exception $expected) {
  833. $this->assertRegExp('/key amazon_order_reference_id or amazon_billing_agreement_id is null and is a required parameter./i', strval($expected));
  834. }
  835. try {
  836. $client = new Client($this->configParams);
  837. $apiCallParams = array('amazon_reference_id' => 'T01');
  838. $client->charge($apiCallParams);
  839. } catch (\Exception $expected) {
  840. $this->assertRegExp('/Invalid Amazon Reference ID./i', strval($expected));
  841. }
  842. }
  843. public function testGetUserInfo()
  844. {
  845. try {
  846. $this->configParams['region'] = '';
  847. $client = new Client($this->configParams);
  848. $client->getUserInfo('Atza');
  849. } catch (\Exception $expected) {
  850. $this->assertRegExp('/is a required parameter./i', strval($expected));
  851. }
  852. try {
  853. $this->configParams['region'] = 'us';
  854. $client = new Client($this->configParams);
  855. $client->getUserInfo(null);
  856. } catch (\Exception $expected) {
  857. $this->assertRegExp('/Access Token is a required parameter and is not set./i', strval($expected));
  858. }
  859. }
  860. public function testSignature()
  861. {
  862. $client = new Client($this->configParams);
  863. $parameters['SellerId'] = $this->configParams['merchant_id'];
  864. $parameters['AWSAccessKeyId'] = $this->configParams['access_key'];
  865. $parameters['Version'] = 'test';
  866. $parameters['SignatureMethod'] = 'HmacSHA256';
  867. $parameters['SignatureVersion'] = 2;
  868. $parameters['Timestamp'] = $this->getFormattedTimestamp();
  869. uksort($parameters, 'strcmp');
  870. $signatureObj = new Signature($this->configParams,$parameters);
  871. $expectedSignature = $signatureObj->getSignature();
  872. $this->callPrivateMethod($client,'createServiceUrl', null);
  873. $signature = $this->callPrivateMethod($client,'signParameters', $parameters);
  874. $this->assertEquals($signature, $expectedSignature);
  875. }
  876. public function test500or503()
  877. {
  878. try {
  879. $client = new Client($this->configParams);
  880. $url = 'https://www.amazon.com/OffAmazonPayments_Sandbox/2013-01-01';
  881. $client->setMwsServiceUrl($url);
  882. $this->callPrivateMethod($client, 'invokePost', null);
  883. } catch (\Exception $expected) {
  884. $this->assertRegExp('/Maximum number of retry attempts./i', strval($expected));
  885. }
  886. }
  887. public function testXmlResponse()
  888. {
  889. $response = array();
  890. $response['ResponseBody'] =
  891. '<GetOrderReferenceDetailsResponse xmlns="http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01">
  892. <AmazonOrderReferenceId>S01-5806490-2147504</AmazonOrderReferenceId>
  893. <ExpirationTimestamp>2015-09-27T02:18:33.408Z</ExpirationTimestamp>
  894. <SellerNote>This is testing API call</SellerNote>
  895. </GetOrderReferenceDetailsResponse>';
  896. $responseObj = new ResponseParser($response);
  897. $xmlResponse = $responseObj->toXml();
  898. $this->assertEquals($xmlResponse, $response['ResponseBody']);
  899. }
  900. public function testJsonResponse()
  901. {
  902. $response = array('Status' => '200');
  903. $response['ResponseBody'] =
  904. '<GetOrderReferenceDetailsResponse xmlns="http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01">
  905. <AmazonOrderReferenceId>S01-5806490-2147504</AmazonOrderReferenceId>
  906. <ExpirationTimestamp>2015-09-27T02:18:33.408Z</ExpirationTimestamp>
  907. <SellerNote>This is testing API call</SellerNote>
  908. </GetOrderReferenceDetailsResponse>';
  909. $json =
  910. '{"AmazonOrderReferenceId":"S01-5806490-2147504","ExpirationTimestamp":"2015-09-27T02:18:33.408Z","SellerNote":"This is testing API call","ResponseStatus":"200"}';
  911. $responseObj = new ResponseParser($response);
  912. $jsonResponse = $responseObj->toJson();
  913. $this->assertEquals($json, $jsonResponse);
  914. }
  915. public function testArrayResponse()
  916. {
  917. $response = array('Status' => '200');
  918. $response['ResponseBody'] =
  919. '<GetOrderReferenceDetailsResponse xmlns="http://mws.amazonservices.com/schema/OffAmazonPayments/2013-01-01">
  920. <AmazonOrderReferenceId>S01-5806490-2147504</AmazonOrderReferenceId>
  921. <ExpirationTimestamp>2015-09-27T02:18:33.408Z</ExpirationTimestamp>
  922. <SellerNote>This is testing API call</SellerNote>
  923. </GetOrderReferenceDetailsResponse>';
  924. $array = array('AmazonOrderReferenceId' => 'S01-5806490-2147504',
  925. 'ExpirationTimestamp' => '2015-09-27T02:18:33.408Z',
  926. 'SellerNote' => 'This is testing API call',
  927. 'ResponseStatus' => '200');
  928. $responseObj = new ResponseParser($response);
  929. $arrayResponse = $responseObj->toArray();
  930. $this->assertEquals($array, $arrayResponse);
  931. }
  932. private function setParametersAndPost($fieldMappings, $action)
  933. {
  934. $expectedParameters = array();
  935. $apiCallParams = array();
  936. $parameters = $this->setDefaultValues($fieldMappings);
  937. $expectedParameters = $parameters['expectedParameters'];
  938. $apiCallParams = $parameters['apiCallParams'];
  939. $expectedParameters['Action'] = $action;
  940. foreach ($fieldMappings as $parm => $value) {
  941. if ($parm === 'capture_now' || $parm === 'confirm_now' || $parm === 'inherit_shipping_address' || $parm === 'request_payment_authorization') {
  942. $expectedParameters[$value] = true;
  943. $apiCallParams[$parm] = true;
  944. } elseif ($parm === 'order_item_categories') {
  945. $apiCallParams[$parm] = array('Antiques', 'Electronics');
  946. } elseif ($parm === 'order_status_list') {
  947. $apiCallParams[$parm] = array('Open', 'Closed');
  948. } elseif ($parm === 'notification_configuration_list') {
  949. $notificationConfiguration['https://dev.null/one'] = array('ORDER_REFERENCE', 'PAYMENT_AUTHORIZE');
  950. $notificationConfiguration['https://dev.null/two'] = array('ALL');
  951. $apiCallParams[$parm] = $notificationConfiguration;
  952. } elseif (!isset($expectedParameters[$value])) {
  953. $unique_id = uniqid();
  954. $expectedParameters[$value] = $unique_id;
  955. $apiCallParams[$parm] = $unique_id;
  956. }
  957. }
  958. return array('expectedParameters' => $expectedParameters,
  959. 'apiCallParams' => $apiCallParams);
  960. }
  961. private function setDefaultValues($fieldMappings)
  962. {
  963. $expectedParameters = array();
  964. $apiCallParams = array();
  965. if (array_key_exists('platform_id', $fieldMappings)) {
  966. $expectedParameters[$fieldMappings['platform_id']] = $this->configParams['platform_id'];
  967. $apiCallParams['platform_id'] = $this->configParams['platform_id'];
  968. }
  969. if (array_key_exists('currency_code', $fieldMappings)) {
  970. $expectedParameters[$fieldMappings['currency_code']] = 'TEST';
  971. $apiCallParams['currency_code'] = 'TEST';
  972. }
  973. return array('expectedParameters' => $expectedParameters,
  974. 'apiCallParams' => $apiCallParams);
  975. }
  976. /* Formats date as ISO 8601 timestamp */
  977. private function getFormattedTimestamp()
  978. {
  979. return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
  980. }
  981. private function callPrivateMethod($client, $methodName, $parameters)
  982. {
  983. $reflectionClass = new \ReflectionClass("AmazonPay\Client");
  984. $reflectionMethod = $reflectionClass->getMethod($methodName);
  985. $reflectionMethod->setAccessible(true);
  986. $expectedStringParams = $reflectionMethod->invoke($client, $parameters);
  987. return $expectedStringParams;
  988. }
  989. }