Advertisement
Guest User

Untitled

a guest
May 25th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. if ($auth == false) if (!is_null($this->mockSubscriber)) $client->getEmitter()->attach($this->mockSubscriber->getMock($stream . '_' . $method));`
  2.  
  3. class ClientTest extends PHPUnit_Framework_TestCase{
  4. protected $clientWallet;
  5.  
  6. protected function setUp(){
  7. $url = "***";
  8. $userName = "***";
  9. $password = "***";
  10. $grantType = "***";
  11. $appKey = '***';
  12.  
  13. $cfgData = new PayClientConfiguration(
  14. [
  15. 'url' => $url,
  16. 'username' => $userName,
  17. 'password' => $password,
  18. 'grantType' => $grantType,
  19. 'appKey' => $appKey
  20. ]
  21. );
  22.  
  23. /*
  24. * WalletTopUp logger and mocks
  25. */
  26.  
  27. // Initialize logger.
  28. $logger = new MonologLogger("WalletTopUp");
  29. $logger->pushHandler(new MonologHandlerStreamHandler("WalletTopUp_test.log"));
  30.  
  31. // Initialize mock subscriber Wallet.
  32. $mockWallet = null;
  33. $useMockResponseWallet = true;
  34. if ($useMockResponseWallet) {
  35. $mockWallet = new MockSubscriber(
  36. __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' .
  37. DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'mocks');
  38. }
  39. // Create Wallet Top Up Client
  40. $this->clientWallet = new mavianceWalletTopUpClientWalletTopUpClient($cfgData, $mockWallet);
  41. $this->clientWallet->addLogger($logger);
  42. $this->clientWallet->init();
  43. }
  44.  
  45.  
  46. /*
  47. * Test Wallet Top Up client
  48. *
  49. */
  50. function testWalletPurchase()
  51. {
  52. $purchaseData = new PurchaseRequestData();
  53. $purchaseData->setCustomerPhonenumber('0712999267');
  54. $purchaseData->setAmount(50);
  55. $purchaseResponse = $this->clientWallet->purchase('wallet', 56438, '', $purchaseData);
  56. }
  57. }
  58.  
  59. class WalletTopUpClient{
  60. function __construct(Configuration $config, MockSubscriber $mock = null)
  61. {
  62. $this->cfgData = $config;
  63. $this->mockSubscriber = $mock;
  64. $this->getAuthToken();
  65. }
  66. public function purchase($service, $destinationId, $srcRefId, PurchaseRequestData $inputData)
  67. {
  68. $body = [
  69. 'Stream' => $service,
  70. 'Amount' => $inputData->getAmount(),
  71. 'PhoneNumber' => $inputData->getCustomerPhonenumber(),
  72. 'PaymentTypeID' => $this::PAYMENT_TYPE_ID
  73. ];
  74. $response = $this->sendRequest($service, 'POST', $this->cfgData->getUrl() . '/api/payments/POST', $body);
  75. $data = json_decode($response->getBody()->getContents(), true);
  76.  
  77. $body = [
  78. 'Stream' => $service,
  79. 'TransactionID' => $data['TransactionID'],
  80. 'PhoneNumber' => $inputData->getCustomerPhonenumber()
  81. ];
  82.  
  83.  
  84. $response = $this->sendRequest($service, 'PUT', $this->cfgData->getUrl() . '/api/payments/PUT', $body);
  85. $data = json_decode($response->getBody()->getContents(), true);
  86. return new PurchaseResponse(
  87. new PurchaseStatus(PurchaseStatus::SUCCESS),
  88. DateTime::createFromFormat('Y-m-d H:i:s.u', $data['TransactionDate']),
  89. $srcRefId,
  90. $data['ReceiptNumber'],
  91. $data['TransactionStatus']);
  92. }
  93.  
  94.  
  95. function sendRequest($stream, $method, $url, $body = null, $auth = false)
  96. {
  97. // Determine body.
  98. $body = is_array($body) && !$auth ? utilsUtils::toString($body) : $body;
  99. // Get http client
  100. $client = $this->getClient();
  101. if ($auth == false)
  102. if (!is_null($this->mockSubscriber))
  103. $client->getEmitter()->attach($this->mockSubscriber->getMock($stream . '_' . $method));
  104. // Create request.
  105.  
  106. $request = $client->createRequest($method, $url, ['body' => $body]);
  107.  
  108. if (!$auth) {
  109. $request->setHeader('Content-Type', 'application/x-www-form-urlencoded');
  110. $request->setHeader('Authorization', '**** ' . $this->getAuthToken()->access_token);
  111. $request->setHeader('app_key', $this->cfgData->getAppKey());
  112. }
  113. $response = null;
  114. try {
  115. $response = $client->send($request);
  116. } catch (RequestException $e) {
  117. $this->logger->error("Error sending request: " . $e->getMessage());
  118. throw new FatalErrorException($e->getMessage(), $e->getCode());
  119. } catch (Exception $e) {
  120. $this->logger->error("Error sending request: " . $e->getMessage());
  121. throw new FatalErrorException($e->getMessage());
  122. }
  123.  
  124. return $response;
  125. }
  126.  
  127. }
  128.  
  129. function sendRequest($stream, $method, $url, $body = null, $auth = false)
  130. {
  131. // Determine body.
  132. $body = is_array($body) && !$auth ? utilsUtils::toString($body) : $body;
  133. // Get http client
  134. $client = $this->getClient();
  135. $mock = null;
  136. if ($auth == false)
  137. if (!is_null($this->mockSubscriber))
  138. $client->getEmitter()->attach(($mock = $this->mockSubscriber->getMock($stream . '_' . $method)));
  139.  
  140. // Create request.
  141.  
  142. $request = $client->createRequest($method, $url, ['body' => $body]);
  143.  
  144. if (!$auth) {
  145. $request->setHeader('Content-Type', 'application/x-www-form-urlencoded');
  146. $request->setHeader('Authorization', 'bearer ' . $this->getAuthToken()->access_token);
  147. $request->setHeader('app_key', $this->cfgData->getAppKey());
  148. }
  149. $response = null;
  150. try {
  151. $response = $client->send($request);
  152. } catch (RequestException $e) {
  153. $this->logger->error("Error sending request: " . $e->getMessage());
  154. throw new FatalErrorException($e->getMessage(), $e->getCode());
  155. } catch (Exception $e) {
  156. $this->logger->error("Error sending request: " . $e->getMessage());
  157. throw new FatalErrorException($e->getMessage());
  158. }
  159.  
  160. // Detach mock subscriber.
  161. if (isset($mock))
  162. $client->getEmitter()->detach($mock);
  163.  
  164. return $response;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement