Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ($auth == false) if (!is_null($this->mockSubscriber)) $client->getEmitter()->attach($this->mockSubscriber->getMock($stream . '_' . $method));`
- class ClientTest extends PHPUnit_Framework_TestCase{
- protected $clientWallet;
- protected function setUp(){
- $url = "***";
- $userName = "***";
- $password = "***";
- $grantType = "***";
- $appKey = '***';
- $cfgData = new PayClientConfiguration(
- [
- 'url' => $url,
- 'username' => $userName,
- 'password' => $password,
- 'grantType' => $grantType,
- 'appKey' => $appKey
- ]
- );
- /*
- * WalletTopUp logger and mocks
- */
- // Initialize logger.
- $logger = new MonologLogger("WalletTopUp");
- $logger->pushHandler(new MonologHandlerStreamHandler("WalletTopUp_test.log"));
- // Initialize mock subscriber Wallet.
- $mockWallet = null;
- $useMockResponseWallet = true;
- if ($useMockResponseWallet) {
- $mockWallet = new MockSubscriber(
- __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' .
- DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'mocks');
- }
- // Create Wallet Top Up Client
- $this->clientWallet = new mavianceWalletTopUpClientWalletTopUpClient($cfgData, $mockWallet);
- $this->clientWallet->addLogger($logger);
- $this->clientWallet->init();
- }
- /*
- * Test Wallet Top Up client
- *
- */
- function testWalletPurchase()
- {
- $purchaseData = new PurchaseRequestData();
- $purchaseData->setCustomerPhonenumber('0712999267');
- $purchaseData->setAmount(50);
- $purchaseResponse = $this->clientWallet->purchase('wallet', 56438, '', $purchaseData);
- }
- }
- class WalletTopUpClient{
- function __construct(Configuration $config, MockSubscriber $mock = null)
- {
- $this->cfgData = $config;
- $this->mockSubscriber = $mock;
- $this->getAuthToken();
- }
- public function purchase($service, $destinationId, $srcRefId, PurchaseRequestData $inputData)
- {
- $body = [
- 'Stream' => $service,
- 'Amount' => $inputData->getAmount(),
- 'PhoneNumber' => $inputData->getCustomerPhonenumber(),
- 'PaymentTypeID' => $this::PAYMENT_TYPE_ID
- ];
- $response = $this->sendRequest($service, 'POST', $this->cfgData->getUrl() . '/api/payments/POST', $body);
- $data = json_decode($response->getBody()->getContents(), true);
- $body = [
- 'Stream' => $service,
- 'TransactionID' => $data['TransactionID'],
- 'PhoneNumber' => $inputData->getCustomerPhonenumber()
- ];
- $response = $this->sendRequest($service, 'PUT', $this->cfgData->getUrl() . '/api/payments/PUT', $body);
- $data = json_decode($response->getBody()->getContents(), true);
- return new PurchaseResponse(
- new PurchaseStatus(PurchaseStatus::SUCCESS),
- DateTime::createFromFormat('Y-m-d H:i:s.u', $data['TransactionDate']),
- $srcRefId,
- $data['ReceiptNumber'],
- $data['TransactionStatus']);
- }
- function sendRequest($stream, $method, $url, $body = null, $auth = false)
- {
- // Determine body.
- $body = is_array($body) && !$auth ? utilsUtils::toString($body) : $body;
- // Get http client
- $client = $this->getClient();
- if ($auth == false)
- if (!is_null($this->mockSubscriber))
- $client->getEmitter()->attach($this->mockSubscriber->getMock($stream . '_' . $method));
- // Create request.
- $request = $client->createRequest($method, $url, ['body' => $body]);
- if (!$auth) {
- $request->setHeader('Content-Type', 'application/x-www-form-urlencoded');
- $request->setHeader('Authorization', '**** ' . $this->getAuthToken()->access_token);
- $request->setHeader('app_key', $this->cfgData->getAppKey());
- }
- $response = null;
- try {
- $response = $client->send($request);
- } catch (RequestException $e) {
- $this->logger->error("Error sending request: " . $e->getMessage());
- throw new FatalErrorException($e->getMessage(), $e->getCode());
- } catch (Exception $e) {
- $this->logger->error("Error sending request: " . $e->getMessage());
- throw new FatalErrorException($e->getMessage());
- }
- return $response;
- }
- }
- function sendRequest($stream, $method, $url, $body = null, $auth = false)
- {
- // Determine body.
- $body = is_array($body) && !$auth ? utilsUtils::toString($body) : $body;
- // Get http client
- $client = $this->getClient();
- $mock = null;
- if ($auth == false)
- if (!is_null($this->mockSubscriber))
- $client->getEmitter()->attach(($mock = $this->mockSubscriber->getMock($stream . '_' . $method)));
- // Create request.
- $request = $client->createRequest($method, $url, ['body' => $body]);
- if (!$auth) {
- $request->setHeader('Content-Type', 'application/x-www-form-urlencoded');
- $request->setHeader('Authorization', 'bearer ' . $this->getAuthToken()->access_token);
- $request->setHeader('app_key', $this->cfgData->getAppKey());
- }
- $response = null;
- try {
- $response = $client->send($request);
- } catch (RequestException $e) {
- $this->logger->error("Error sending request: " . $e->getMessage());
- throw new FatalErrorException($e->getMessage(), $e->getCode());
- } catch (Exception $e) {
- $this->logger->error("Error sending request: " . $e->getMessage());
- throw new FatalErrorException($e->getMessage());
- }
- // Detach mock subscriber.
- if (isset($mock))
- $client->getEmitter()->detach($mock);
- return $response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement