/** * Processes the associated command * * @param CommandInterface|TakePaymentCommand $command * * @throws PaymentFailedException if unable to create new payment record, or the gateway authorisation failed * form any reason * @throws SavePaymentFailedException if the payment succeeds, but something goes wrong when updating the payment * record */ protected function _handle(CommandInterface $command) { $this->paymentId = null; $payment = $this->convertCommandToPayment($command); try { // We clone the payment object here, so PHPSpec can test it, until the following issue is resolved… // https://github.com/phpspec/phpspec/issues/789 $payment = $this->repository->savePaymentBeforeProcessing($payment); $this->paymentId = $payment->id(); $purchaseRequest = $this->gateway->purchase($payment->getGatewayPurchaseArray()); $purchaseResponse = $purchaseRequest->send(); if (!$purchaseResponse->isSuccessful()) { // @todo Add saving of failed details to payment record throw PaymentNotAuthorisedException::createWithPayment($payment, $purchaseResponse->getMessage()); } } catch (PaymentFailedException $exception) { // These exceptions will be publicly safe, so just throw themhttps:/https://github.com/phpspec/phpspec/issues/789https://github.com/phpspec/phpspec/issues/789https://github.com/phpspec/phpspec/issues/789https://github.com/phpspec/phpspec/issues/789/github.com/phpspec/phpspec/issues/789 throw $exception; } catch (\Exception $exception) { throw PaymentFailedException::createWithPayment($payment, sprintf('An unrecognised exception (%s) has been thrown', get_class($exception)), 0, $exception); } try { $this->updatePaymentWithStripeCharge($payment, $purchaseResponse); } catch (\Exception $exception) { // Wrap in SavePaymentFailedException if need be to provide public exception if ($exception instanceof SavePaymentFailedException) { throw $exception; } throw SavePaymentFailedException::createWithPayment($payment, sprintf('An unrecognised exception (%s) has been thrown', get_class($exception)), 0, $exception); } }
/** * Sets the expected calls & responses on PaymentRepositoryInterface stub * * @param PaymentRepositoryInterface $repository */ protected function setRepositoryMethodExpectations(PaymentRepositoryInterface $repository) { /** @noinspection PhpVoidFunctionResultUsedInspection */ /** @noinspection PhpUndefinedMethodInspection */ // We expect the processed payment due to this issue… // https://github.com/phpspec/phpspec/issues/789 $repository->savePaymentBeforeProcessing($this->expectedUnpaidPayment)->willReturn($this->expectedUnpaidSavedPayment); /** @noinspection PhpVoidFunctionResultUsedInspection */ /** @noinspection PhpUndefinedMethodInspection */ $repository->markAsPaid($this->expectedProcessedPayment)->shouldBeCalled(); }