/** * Handle the command. * * @param ConfigurationRepositoryInterface $configuration * @param Repository $config * @return Gateway */ public function handle(ConfigurationRepositoryInterface $configuration, Repository $config) { $testMode = $config->get('anomaly.module.payments::config.test_mode'); if ($testMode) { $key = $configuration->presenter('anomaly.extension.stripe_gateway::test_api_key', $this->account->getSlug())->__value(); } else { $key = $configuration->presenter('anomaly.extension.stripe_gateway::live_api_key', $this->account->getSlug())->__value(); } $gateway = new Gateway(); $gateway->setApiKey($key); $gateway->setTestMode($testMode); return $gateway; }
/** * Handle the command. * * @param ConfigurationRepositoryInterface $configuration */ public function handle(ConfigurationRepositoryInterface $configuration) { $mode = $configuration->get('anomaly.extension.stripe_gateway::test_mode', $this->gateway->getSlug()); /* @var EncryptedFieldTypePresenter $key */ if ($mode->getValue()) { $key = $configuration->presenter('anomaly.extension.stripe_gateway::test_api_key', $this->gateway->getSlug()); } else { $key = $configuration->presenter('anomaly.extension.stripe_gateway::live_api_key', $this->gateway->getSlug()); } $gateway = new Gateway(); $gateway->setApiKey($key->decrypted()); $gateway->setTestMode($mode->getValue()); return $gateway; }
/** * 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); } }
/** * @uses TakePaymentCommandHandler::_handle() */ function it_should_emit_a_failure_event_if_not_ok(PaymentRepositoryInterface $repository, Gateway $gateway, TakePaymentCommand $command, EmitterInterface $emitter) { $this->setRepositoryMethodExpectations($repository); $this->clearRepositoryMarkAsPaidExpectation($repository); $gatewayException = new PaymentFailedException('Failed to process payment with the Stripe payment gateway'); /** @noinspection PhpUndefinedMethodInspection */ $gateway->purchase(Argument::any())->willThrow($gatewayException); /** @noinspection PhpUndefinedMethodInspection */ $this->shouldThrow($gatewayException)->during('handle', [$command]); /** @noinspection PhpUndefinedMethodInspection */ $emitter->emit(Argument::type(TakePaymentFailureEvent::class))->shouldHaveBeenCalled(); }