Skip to content

Payment API

To get payments and manage them, use the Ibexa\Contracts\Payment\PaymentServiceInterface interface.

By default, UUID is used to generate payment identifiers. You can change that by providing a custom payment identifier in Ibexa\Contracts\Payment\Payment\PaymentCreateStruct or Ibexa\Contracts\Payment\Payment\PaymentUpdateStruct.

Get single payment

Get single payment by ID

To access a single payment by using its numerical ID, use the PaymentServiceInterface::getPayment method:

1
2
3
4
        $paymentId = 1;
        $payment = $this->paymentService->getPayment($paymentId);

        $output->writeln(sprintf('Payment %d has status %s', $paymentId, $payment->getStatus()));

Get single payment by identifier

To access a single payment by using its string identifier, use the PaymentServiceInterface::getPaymentByIdentifier method:

1
2
3
4
        $paymentIdentifier = '4ac4b8a0-eed8-496d-87d9-32a960a10629';
        $payment = $this->paymentService->getPaymentByIdentifier($paymentIdentifier);

        $output->writeln(sprintf('Your payment for transaction has status %s', $payment->getStatus()));

Get multiple payments

To fetch multiple payments, use the PaymentServiceInterface::findPayments method. It follows the same search query pattern as other APIs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
        $paymentCriterions = [
            new Currency('USD'),
            new Currency('CZK'),
        ];

        $paymentQuery = new PaymentQuery(new LogicalOr(...$paymentCriterions));
        $paymentQuery->setLimit(10);

        $paymentsList = $this->paymentService->findPayments($paymentQuery);

        $paymentsList->getPayments();
        $paymentsList->getTotalCount();

        foreach ($paymentsList as $payment) {
            $output->writeln($payment->getIdentifier() . ': ' . $payment->getOrder()->getIdentifier() . ': ' . $payment->getOrder()->getValue()->getTotalGross()->getAmount());
        }

Create payment

To create a payment, use the PaymentServiceInterface::createPayment method and provide it with the Ibexa\Contracts\Payment\Payment\PaymentCreateStruct object that takes the following arguments: method, order and amount.

1
2
3
        $payment = $this->paymentService->createPayment($paymentCreateStruct);

        $output->writeln(sprintf('Created payment %s for order %s', $payment->getIdentifier(), $payment->getOrder()->getIdentifier()));

Update payment

You can update payment information after the payment is created. You could do it to support a scenario when, for example, an online payment failed, has been processed by using other means, and its status has to be updated in the system. The Ibexa\Contracts\Payment\Payment\PaymentUpdateStruct object takes the following arguments: transition, identifier, and context. To update payment information, use the PaymentServiceInterface::updatePayment method:

1
2
3
4
5
6
        $paymentUpdateStruct = new PaymentUpdateStruct();
        $paymentUpdateStruct->setTransition('pay');

        $this->paymentService->updatePayment($payment, $paymentUpdateStruct);

        $output->writeln(sprintf('Changed payment status to %s', $payment->getStatus()));

Delete payment

To delete a payment from the system, use the PaymentServiceInterface::deletePayment method:

1
        $this->paymentService->deletePayment($payment);