public function test_createTransactionFail() { // create guzzle mock $mockHandler = new MockHandler([new RequestException('Error', new Request('method', 'url'), new Response(TransactionException::ERROR))]); $errorMessages = TransactionException::getErrorMessages(); self::setExpectedException('\\KKomarov\\MLePay\\Exceptions\\TransactionException', $errorMessages[TransactionException::ERROR]); $this->createTransaction($mockHandler); }
/** * Creates a transaction * * @param GuzzleHttpClient $guzzleClient Guzzle http client * @param string $recieverEmail The receiver email is your merchant email, this is unique to every account and is provided in the Merchant Profile page. * @param string $senderEmail The email address of the customer or buyer. * @param string $senderName The name of the customer or buyer. * @param string $senderPhone The mobile or landline phone number of the customer or buyer. * @param string $senderAddress The address of the customer or buyer. * @param integer $amount The amount in centavos to be paid by the customer or buyer. * @param string $currency The currency of the transaction. Currently, only "PHP" is supported. * @param string $nonce A random unique 16-character string to identify this request. This is used as a security measure. * @param integer $timestamp The UNIX Timestamp of the current date and time. This is used as a security measure. * @param int $expiry The UNIX Timestamp of the date and time the transaction is set to expire. This is useful for transactions with a set "deadline" or "cutoff" for payments to be completed. * @param string $payload Custom transaction details. When the status of a transaction is changed, the payload will also be passed along with the other transaction details as a notification to the webhook. This will NOT be shown to the customer or buyer. * @param string $description The description of the transaction. This will be shown to the buyer or customer. * * @return Transaction * @throws TransactionException * @throws \RuntimeException */ public function createTransaction(GuzzleHttpClient $guzzleClient, $recieverEmail, $senderEmail, $senderName, $senderPhone, $senderAddress, $amount, $currency = self::CURRENCY_PHP, $nonce, $timestamp, $expiry = 0, $payload = '', $description = '') { $requestBody = json_encode(['receiver_email' => $recieverEmail, 'sender_email' => $senderEmail, 'sender_name' => $senderName, 'sender_phone' => $senderPhone, 'sender_address' => $senderAddress, 'amount' => $amount, 'currency' => self::CURRENCY_PHP, 'nonce' => $nonce, 'timestamp' => $timestamp, 'expiry' => $expiry, 'payload' => $payload, 'description' => $description]); try { $response = $guzzleClient->post(self::CREATE_TRANSACTION_URL, ['body' => $requestBody, 'headers' => ['Content-Type' => 'application/json', 'X-Signature' => $this->getSignature(self::CREATE_TRANSACTION_URL, $requestBody)]]); $responseHeaders = $response->getHeaders(); $responseBody = $response->getBody()->getContents(); $responseJson = json_decode($responseBody, true); $this->logger->debug('Create transaction response:', ['headers' => $responseHeaders, 'body' => $responseJson]); return new Transaction($responseJson['transaction']['amount'], $responseJson['transaction']['payload'], $responseJson['transaction']['code'], $responseJson['transaction']['currency']); } catch (RequestException $e) { $this->logger->error('Create transaction response:', ['message' => $e->getMessage()]); $errorMessages = TransactionException::getErrorMessages(); $errorMessage = $errorMessages[TransactionException::ERROR]; $errorCode = TransactionException::ERROR; if (array_key_exists($e->getCode(), $errorMessages)) { $errorMessage = $errorMessages[$errorCode]; $errorCode = $e->getCode(); } throw new TransactionException($errorMessage, $errorCode, $e); } }