Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,18 @@ public function createSchemaManager(\Doctrine\DBAL\Connection $connection): Abst
]);
},

Client\RyftClient::class => static function (ContainerInterface $c): Client\RyftClient {
$settings = $c->get(Settings::class);

return new Client\RyftClient(
publicKey: $settings->ryft['publicKey'],
secretKey: $settings->ryft['secretKey'],
client: $c->get(\GuzzleHttp\Client::class),
environment: $c->get(Environment::class),
log: $c->get(LoggerInterface::class)
);
},

Transports::TRANSPORT_HIGH_PRIORITY => static function (): TransportInterface {
return Transports::buildTransport(Transports::TRANSPORT_HIGH_PRIORITY);
},
Expand Down
27 changes: 23 additions & 4 deletions src/Application/Actions/Donations/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,26 @@
use MatchBot\Application\Actions\Action;
use MatchBot\Application\Actions\ActionError;
use MatchBot\Application\Actions\ActionPayload;
use MatchBot\Application\Assert;
use MatchBot\Application\Assertion;
use MatchBot\Application\AssertionFailedException;
use MatchBot\Application\Auth\DonationToken;
use MatchBot\Application\Auth\PersonManagementAuthMiddleware;
use MatchBot\Application\HttpModels\DonationCreate;
use MatchBot\Application\HttpModels\DonationCreatedResponse;
use MatchBot\Application\Matching\DbErrorPreventedMatch;
use MatchBot\Application\Settings;
use MatchBot\Client\RyftClient;
use MatchBot\Client\Stripe;
use MatchBot\Domain\DomainException\CampaignNotOpen;
use MatchBot\Domain\DomainException\CharityAccountLacksNeededCapaiblities;
use MatchBot\Domain\DomainException\CouldNotMakeStripePaymentIntent;
use MatchBot\Domain\DomainException\DonationCreateModelLoadFailure;
use MatchBot\Domain\DomainException\StripeAccountIdNotSetForAccount;
use MatchBot\Domain\DomainException\WrongCampaignType;
use MatchBot\Domain\Donation;
use MatchBot\Domain\DonationService;
use MatchBot\Domain\Money;
use MatchBot\Domain\PersonId;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
Expand All @@ -41,6 +46,7 @@ public function __construct(
private SerializerInterface $serializer,
LoggerInterface $logger,
private Stripe $stripe,
private RyftClient $ryftClient,
Settings $settings,
) {
parent::__construct($logger);
Expand Down Expand Up @@ -197,16 +203,29 @@ protected function action(Request $request, Response $response, array $args): Re
if ($donation->getPsp() === 'stripe') {
$stripeCustomerId = $donation->getPspCustomerId();
\assert($stripeCustomerId !== null);
$customerSession = $this->stripe->createCustomerSession($stripeCustomerId);
$this->logger->info('Stripe customer session expiry: ' . $customerSession->expires_at);
$stripeCustomerSession = $this->stripe->createCustomerSession($stripeCustomerId);
$this->logger->info('Stripe customer session expiry: ' . $stripeCustomerSession->expires_at);
} else {
$customerSession = null;
$stripeCustomerSession = null;
}

if ($donation->getPsp() === 'ryft') {
$ryftAccountId = $donation->getCampaign()->getCharity()->getRyftAccountId();
Assertion::notNull($ryftAccountId, 'Ryft account ID cannot be null for ryft payment method');
$ryftClientSecret = $this->ryftClient->createPaymentSession(
$ryftAccountId,
Money::fromPence($donation->getAmountForCharityFractional(), $donation->currency()),
);
} else {
$ryftClientSecret = null;
}


$data = new DonationCreatedResponse(
donation: $donation->toFrontEndApiModel($this->enableNoReservationsMode),
jwt: DonationToken::create($donation->getUuid()->toString()),
stripeSessionSecret: $customerSession?->client_secret,
stripeSessionSecret: $stripeCustomerSession?->client_secret,
ryftClientSecret: $ryftClientSecret,
);

return $this->respondWithData($response, $data, 201);
Expand Down
3 changes: 0 additions & 3 deletions src/Application/Commands/CreateFictionalData.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public function __construct(
parent::__construct(null);
}

/**
* @return string
*/
public function createRyftAccount(SymfonyStyle $io): string
{
$ryftURIPrefix = "https://sandbox-api.ryftpay.com/v1";
Expand Down
1 change: 1 addition & 0 deletions src/Application/HttpModels/DonationCreatedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(
* @see https://docs.stripe.com/api/customer_sessions/object#customer_session_object-client_secret
*/
public ?string $stripeSessionSecret = null,
public ?string $ryftClientSecret = null,
) {
}
}
82 changes: 82 additions & 0 deletions src/Client/RyftClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace MatchBot\Client;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use MatchBot\Application\Assertion;
use MatchBot\Application\Environment;
use MatchBot\Domain\Money;
use MatchBot\Domain\RyftAccountId;
use Psr\Log\LoggerInterface;

/**
* Using custom client just to call the functions of the Ryft HTTP API that are useful to us
* rather than the Ryft PHP SDK as that doesn't seem to add any real value - it isn't usable
* without referring in detail to documentation provided separately.
Comment on lines +15 to +16
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we checked if it has features like auto pagination, retries etc. like Stripe's?

*/
class RyftClient
{
private string $apiPrefix;

public function __construct(
private string $publicKey, // @phpstan-ignore property.onlyWritten
private string $secretKey,
private Client $client,
Environment $environment,
private LoggerInterface $log,
) {
$this->apiPrefix = match ($environment) {
Environment::Production => 'https://api.ryftpay.com/v1/',
Environment::Staging, Environment::Local, Environment::Regression => 'https://sandbox-api.ryftpay.com/v1/',
Environment::Test => 'https://placeholder-for-ryft-api-in-test.biggive.org/v1/'
};
}

/**
* @param RyftAccountId $ryftAccountId Ryft sub-account ID of the charity that will receive the payment
* @return string client secret
*
* See https://api-reference.ryftpay.com/#tag/Payments/operation/paymentSessionCreate
*/
public function createPaymentSession(RyftAccountId $ryftAccountId, Money $amount): string
{
$headers = [
'Authorization' => $this->secretKey,
'Account' => $ryftAccountId->ryftAccountId,
];

$request = new Request(
method: 'POST',
uri: $this->apiPrefix . 'payment-sessions',
headers: $headers,
body: json_encode(
[
'amount' => $amount->amountInPence(),
'currency' => $amount->currency->isoCode(),

],
\JSON_THROW_ON_ERROR
)
);

$response = $this->client->send($request);

$responseContents = $response->getBody()->getContents();

$this->log->info($responseContents);

/*
* 2026-04-01T15:40:22.243849016Z [2026-04-01T15:40:22.243429+00:00] matchbot.INFO: {"id":"ps_01KN4V7VRAGFBGHGRXDZBVTPFM","amount":270,"currency":"GBP","paymentType":"Standard","entryMode":"Online","enabledPaymentMethods":["Card"],"returnUrl":"https://donate.biggive.org","status":"PendingPayment","refundedAmount":0,"clientSecret":"ps_01KN4V7VRAGFBGHGRXDZBVTPFM_secret_0a31276f-1e46-40b0-8bdb-75742249d0b4","statementDescriptor":{"descriptor":"Big Give","city":"Manchester"},"authorizationType":"FinalAuth","captureFlow":"Automatic","createdTimestamp":1775058022,"lastUpdatedTimestamp":1775058022} [] {"commit":"fde1a7d","uid":"9425ebd","memory_peak_usage":"4 MB"}

*/

$response = json_decode($responseContents, true, \JSON_THROW_ON_ERROR);
Assertion::isArray($response);
$clientSecret = $response['clientSecret'];

\assert(\is_string($clientSecret));

return $clientSecret;
}
}