/**
  * This test shopuld return an API error
  * @expectedException Upg\Library\Api\Exception\ApiError
  */
 public function testApiCallThatReturnsAnAPIError()
 {
     if (is_null($this->config)) {
         $this->markTestSkipped('Config is not set, please set up the required environment variables');
         return false;
     }
     $configData = $this->config->getConfigData();
     $configData['storeID'] = "WRONGSTOREID";
     $config = new Config($configData);
     $request = new CreateTransactionRequest($config);
     //unique ID for the tests
     $orderId = hash('crc32b', microtime());
     $userId = "GUEST:" . hash('md5', microtime());
     $request->setOrderID($orderId)->setUserID($userId)->setIntegrationType(CreateTransactionRequest::INTEGRATION_TYPE_API)->setAutoCapture(true)->setContext(CreateTransactionRequest::CONTEXT_OFFLINE)->setMerchantReference("TEST")->setUserType(CreateTransactionRequest::USER_TYPE_PRIVATE)->setUserRiskClass(RiskClass::RISK_CLASS_DEFAULT)->setUserData($this->getUser())->setBillingAddress($this->getAddress())->setAmount($this->getAmount())->addBasketItem($this->getBasketItem())->setLocale(Codes::LOCALE_EN);
     $apiEndPoint = new CreateTransaction($config, $request);
     $apiEndPoint->sendRequest();
 }
 /**
  * Make an successful call
  * Create a transaction then do the reserve call
  */
 public function testSuccessfulApiCall()
 {
     if (is_null($this->config)) {
         $this->markTestSkipped('Config is not set, please set up the required environment variables');
         return false;
     }
     $request = new CreateTransactionRequest($this->config);
     //unique ID for the tests
     $orderId = hash('crc32b', microtime());
     $captureId = hash('crc32b', microtime());
     $userId = "GUEST:" . hash('md5', microtime());
     $paymentInstrumentId = hash('crc32b', microtime());
     $request->setOrderID($orderId)->setUserID($userId)->setIntegrationType(CreateTransactionRequest::INTEGRATION_TYPE_API)->setAutoCapture(false)->setContext(CreateTransactionRequest::CONTEXT_ONLINE)->setMerchantReference("TEST")->setUserType(CreateTransactionRequest::USER_TYPE_PRIVATE)->setUserRiskClass(RiskClass::RISK_CLASS_DEFAULT)->setUserData($this->getUser())->setBillingAddress($this->getAddress())->setAmount($this->getAmount())->addBasketItem($this->getBasketItem())->setLocale(Codes::LOCALE_EN);
     $apiEndPoint = new CreateTransaction($this->config, $request);
     $createTransactionResult = $apiEndPoint->sendRequest();
     $this->assertEquals(0, $createTransactionResult->getData('resultCode'));
     $allowedPayments = $createTransactionResult->getData('allowedPaymentMethods');
     $this->assertTrue(in_array('CC', $createTransactionResult->getData('allowedPaymentMethods')));
     if (!in_array('CC', $allowedPayments)) {
         $this->fail("Allowed payment from CreateTransaction lacks CC can not continue");
     }
     /**
      * Register payment instrument
      */
     $registerUserPaymentInstrumentRequest = new RegisterUserPaymentInstrumentRequest($this->config);
     $registerUserPaymentInstrumentRequest->setUserID($userId)->setPaymentInstrument($this->getPaymentInstrument());
     $registerUserPaymentInstrument = new RegisterUserPaymentInstrumentApi($this->config, $registerUserPaymentInstrumentRequest);
     $registerUserPaymentInstrumentResult = $registerUserPaymentInstrument->sendRequest();
     $paymentInstrumentId = $registerUserPaymentInstrumentResult->getData('paymentInstrumentID');
     $reserveRequest = new ReserveRequest($this->config);
     $reserveRequest->setOrderID($orderId)->setPaymentMethod(Methods::PAYMENT_METHOD_TYPE_CC)->setPaymentInstrumentID($paymentInstrumentId)->setCcv(123);
     $reserveApi = new ReserveApi($this->config, $reserveRequest);
     $reserveApi->sendRequest();
     $getTransactionStatusRequest = new GetTransactionStatusRequest($this->config);
     $getTransactionStatusRequest->setOrderID($orderId);
     /**
      * Now test the getTransactionStatus call
      */
     $getTransactionStatusApi = new GetTransactionStatusApi($this->config, $getTransactionStatusRequest);
     $result = $getTransactionStatusApi->sendRequest();
     $this->assertEquals(0, $result->getData('resultCode'));
     $this->assertEmpty($result->getData('message'));
     $this->assertNotEmpty($result->getData('salt'));
     $this->assertEquals('MERCHANTPENDING', $result->getData('transactionStatus'));
     $additionalData = $result->getData('additionalData');
     $this->assertEquals('CC', $additionalData['paymentMethod']);
     $this->assertEquals(100, $additionalData['transactionAmount']);
     $this->assertEquals($this->getUser()->getEmail(), $additionalData['customerEmail']);
 }
 /**
  * Test for API Error Exception
  * Note this wont use annotation as this exception has extra data that must be validated
  */
 public function testApiErrorException()
 {
     $errorCode = \Upg\Library\Error\Codes::ERROR_MAC;
     $message = "Invalid Mac Error from API";
     $exceptionRaised = false;
     $header = "HTTP/1.1 200 OK\n            \r\nDate: Wed, 18 Nov 2015 14:23:48 GMT\n            \r\nServer: Jetty(8.1.15.v20140411)\n            \r\nContent-Type: application/json;charset=UTF-8\n            \r\nAccess-Control-Allow-Origin: *\n            \r\nAccess-Control-Allow-Methods: POST\n            \r\nAccess-Control-Expose-Headers: X-Payco-TOKEN, X-Payco-HMAC\n            \r\nX-Payco-HMAC: d91d92a84c215dbbc045d7fdce0405a0ea14ae61\n            \r\nVia: 1.1 www.payco-sandbox.de\n            \r\nConnection: close\n            \r\nTransfer-Encoding: chunked";
     $rawResponse = '{
       "resultCode": ' . $errorCode . ',
       "message": "' . $message . '",
       "salt": "nMp9eFTqrURBqquBb3P9hRX8g7RDzE8DCvu3nKwYJLvwha8F"
     }';
     try {
         $request = new CreateTransaction($this->config);
         $request->setOrderID(1)->setUserID(1)->setIntegrationType(CreateTransaction::INTEGRATION_TYPE_HOSTED_AFTER)->setAutoCapture(true)->setContext(CreateTransaction::CONTEXT_ONLINE)->setMerchantReference("TEST")->setUserType(CreateTransaction::USER_TYPE_PRIVATE)->setUserRiskClass(\Upg\Library\Risk\RiskClass::RISK_CLASS_DEFAULT)->setUserIpAddress("192.168.1.2")->setUserData($this->getUser())->setBillingAddress($this->getAddress())->setAmount($this->getAmount())->addBasketItem($this->getBasketItem())->setLocale(\Upg\Library\Locale\Codes::LOCALE_EN);
         $api = new CreateTransactionApi($this->config, $request);
         $api->setResponseRaw($rawResponse, 400, $header);
         $api->sendRequest();
     } catch (\Upg\Library\Api\Exception\ApiError $e) {
         $exceptionRaised = true;
         $response = $e->getParsedResponse();
         $this->assertEquals($errorCode, $response->getData('resultCode'));
         $this->assertEquals($message, $response->getData('message'));
         $this->assertEquals($errorCode, $e->getCode());
     }
     if (!$exceptionRaised) {
         $this->fail("Excpected exception was not raised");
     }
 }
 /**
  * Make an successful call
  * Create a transaction then do the reserve call
  */
 public function testSuccessfulApiCall()
 {
     if (is_null($this->config)) {
         $this->markTestSkipped('Config is not set, please set up the required environment variables');
         return false;
     }
     $request = new CreateTransactionRequest($this->config);
     //unique ID for the tests
     $orderId = hash('crc32b', microtime());
     $captureId = hash('crc32b', microtime());
     $userId = "GUEST:" . hash('md5', microtime());
     $request->setOrderID($orderId)->setUserID($userId)->setIntegrationType(CreateTransactionRequest::INTEGRATION_TYPE_API)->setAutoCapture(false)->setContext(CreateTransactionRequest::CONTEXT_ONLINE)->setMerchantReference("TEST")->setUserType(CreateTransactionRequest::USER_TYPE_PRIVATE)->setUserRiskClass(RiskClass::RISK_CLASS_DEFAULT)->setUserData($this->getUser())->setBillingAddress($this->getAddress())->setAmount($this->getAmount())->addBasketItem($this->getBasketItem())->setLocale(Codes::LOCALE_EN);
     $apiEndPoint = new CreateTransaction($this->config, $request);
     $createTransactionResult = $apiEndPoint->sendRequest();
     $this->assertEquals(0, $createTransactionResult->getData('resultCode'));
     $allowedPayments = $createTransactionResult->getData('allowedPaymentMethods');
     $this->assertTrue(in_array('CC', $createTransactionResult->getData('allowedPaymentMethods')));
     if (!in_array('CC', $allowedPayments)) {
         $this->fail("Allowed payment from CreateTransaction lacks CC can not continue");
     }
     /**
      * Register payment instrument
      */
     $registerUserPaymentInstrumentRequest = new RegisterUserPaymentInstrumentRequest($this->config);
     $registerUserPaymentInstrumentRequest->setUserID($userId)->setPaymentInstrument($this->getPaymentInstrument());
     $registerUserPaymentInstrument = new RegisterUserPaymentInstrumentApi($this->config, $registerUserPaymentInstrumentRequest);
     $registerUserPaymentInstrumentResult = $registerUserPaymentInstrument->sendRequest();
     $paymentInstrumentId = $registerUserPaymentInstrumentResult->getData('paymentInstrumentID');
     $reserveRequest = new ReserveRequest($this->config);
     $reserveRequest->setOrderID($orderId)->setPaymentMethod(Methods::PAYMENT_METHOD_TYPE_CC)->setPaymentInstrumentID($paymentInstrumentId)->setCcv(123);
     $reserveApi = new ReserveApi($this->config, $reserveRequest);
     $reserveApi->sendRequest();
     /**
      * Do the capture
      */
     $captureRequest = new CaptureRequest($this->config);
     $captureRequest->setOrderID($orderId)->setCaptureID($captureId)->setAmount($this->getAmount());
     $captureApi = new CaptureApi($this->config, $captureRequest);
     $captureApi->sendRequest();
     /**
      * Do the updateTransaction test
      */
     $updateTransactionRequest = new UpdateTransactionRequest($this->config);
     $updateTransactionRequest->setOrderID($orderId)->setCaptureID($captureId)->setInvoiceNumber($orderId . ':1')->setInvoiceDate(new \DateTime())->setOriginalInvoiceAmount($this->getAmount())->setShippingDate(new \DateTime())->setInvoicePDF($this->getPDF())->setTrackingID("111222");
     $updateTransactionApi = new UpdateTransactionApi($this->config, $updateTransactionRequest);
     $result = $updateTransactionApi->sendRequest();
     $this->assertEquals(0, $result->getData('resultCode'));
     $this->assertEmpty($result->getData('message'));
     $this->assertNotEmpty($result->getData('salt'));
     return true;
 }