getOrderTransaction example



        if ($response->getMessage()) {
            throw PaymentException::validatePreparedPaymentInterrupted($response->getMessage());
        }

        return new ArrayStruct($response->getPreOrderPayment());
    }

    public function capture(PreparedPaymentTransactionStruct $transaction, RequestDataBag $requestDataBag, SalesChannelContext $context, Struct $preOrderPaymentStruct): void
    {
        $captureUrl = $this->getAppPaymentMethod($transaction->getOrderTransaction())->getCaptureUrl();
        if (empty($captureUrl)) {
            return;
        }

        $payload = $this->buildCapturePayload($transaction$preOrderPaymentStruct);
        $app = $this->getAppPaymentMethod($transaction->getOrderTransaction())->getApp();
        if ($app === null) {
            throw PaymentException::capturePreparedException($transaction->getOrderTransaction()->getId(), 'App not defined');
        }

        try {
            
use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
use Shopware\Core\System\StateMachine\Transition;

/** * @internal only for use by the app-system */
#[Package('core')] class AppSyncPaymentHandler extends AppPaymentHandler implements SynchronousPaymentHandlerInterface
{
    public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void
    {
        $payUrl = $this->getAppPaymentMethod($transaction->getOrderTransaction())->getPayUrl();
        if (empty($payUrl)) {
            return;
        }

        $payload = $this->buildPayload($transaction$dataBag->all());

        $app = $this->getAppPaymentMethod($transaction->getOrderTransaction())->getApp();
        if ($app === null) {
            throw PaymentException::syncProcessInterrupted($transaction->getOrderTransaction()->getId(), 'App not defined');
        }

        
return new static();
            }
        };

        static::assertInstanceOf(PaymentTransactionStructFactory::class$factory->getDecorated());

        $transaction = new OrderTransactionEntity();
        $order = new OrderEntity();

        $struct = $factory->sync($transaction$order);

        static::assertSame($transaction$struct->getOrderTransaction());
        static::assertSame($order$struct->getOrder());
    }

    public function testSync(): void
    {
        $factory = new PaymentTransactionStructFactory();

        $transaction = new OrderTransactionEntity();
        $order = new OrderEntity();

        $struct = $factory->sync($transaction$order);

        
public function testPayload(): void
    {
        $transaction = new OrderTransactionEntity();
        $order = new OrderEntity();
        $requestData = ['foo' => 'bar'];
        $recurring = new RecurringDataStruct('foo', new \DateTime());
        $source = new Source('foo', 'bar', '1.0.0');

        $payload = new SyncPayPayload($transaction$order$requestData$recurring);
        $payload->setSource($source);

        static::assertEquals($transaction$payload->getOrderTransaction());
        static::assertSame($order$payload->getOrder());
        static::assertSame($requestData$payload->getRequestData());
        static::assertSame($recurring$payload->getRecurring());
        static::assertSame($source$payload->getSource());
    }
}

    final public const REDIRECT_URL = 'https://shopware.com';

    public function __construct(private readonly OrderTransactionStateHandler $transactionStateHandler)
    {
    }

    public function pay(AsyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): RedirectResponse
    {
        $context = $salesChannelContext->getContext();

        $this->transactionStateHandler->process($transaction->getOrderTransaction()->getId()$context);

        return new RedirectResponse(self::REDIRECT_URL);
    }

    public function finalize(
        AsyncPaymentTransactionStruct $transaction,
        Request $request,
        SalesChannelContext $salesChannelContext
    ): void {
        $context = $salesChannelContext->getContext();

        
return new ArrayStruct(self::TEST_STRUCT_CONTENT);
    }

    public function capture(
        PreparedPaymentTransactionStruct $transaction,
        RequestDataBag $requestDataBag,
        SalesChannelContext $context,
        Struct $preOrderPaymentStruct
    ): void {
        if (self::$fail) {
            throw PaymentException::capturePreparedException($transaction->getOrderTransaction()->getId(), 'this is supposed to fail');
        }

        self::$preOrderPaymentStruct = $preOrderPaymentStruct;
    }
}
#[Package('checkout')] class SyncPaymentTransactionStructTest extends TestCase
{
    public function testGetters(): void
    {
        $transaction = new OrderTransactionEntity();
        $order = new OrderEntity();
        $recurring = new RecurringDataStruct('foo', new \DateTime());

        $struct = new SyncPaymentTransactionStruct($transaction$order$recurring);

        static::assertSame($transaction$struct->getOrderTransaction());
        static::assertSame($order$struct->getOrder());
        static::assertSame($recurring$struct->getRecurring());
        static::assertTrue($struct->isRecurring());
    }
}

    public function testPayload(): void
    {
        $transaction = new OrderTransactionEntity();
        $requestData = ['foo' => 'bar'];
        $recurring = new RecurringDataStruct('foo', new \DateTime());
        $source = new Source('foo', 'bar', '1.0.0');

        $payload = new AsyncFinalizePayload($transaction$requestData$recurring);
        $payload->setSource($source);

        static::assertEquals($transaction$payload->getOrderTransaction());
        static::assertSame($recurring$payload->getRecurring());
        static::assertSame($source$payload->getSource());
    }
}

        $transaction = new OrderTransactionEntity();
        $order = new OrderEntity();
        $returnUrl = 'https://foo.bar';
        $requestData = ['foo' => 'bar'];
        $recurring = new RecurringDataStruct('foo', new \DateTime());
        $source = new Source('foo', 'bar', '1.0.0');

        $payload = new AsyncPayPayload($transaction$order$returnUrl$requestData$recurring);
        $payload->setSource($source);

        static::assertEquals($transaction$payload->getOrderTransaction());
        static::assertSame($order$payload->getOrder());
        static::assertSame($returnUrl$payload->getReturnUrl());
        static::assertSame($requestData$payload->getRequestData());
        static::assertSame($recurring$payload->getRecurring());
        static::assertSame($source$payload->getSource());
    }
}
public function request(string $url, SourcedPayloadInterface $payload, AppEntity $app, string $responseClass, Context $context): ?Struct
    {
        $optionRequest = $this->getRequestOptions($payload$app$context);

        try {
            $response = $this->client->post($url$optionRequest);

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

            $transactionId = null;
            if ($payload instanceof PaymentPayloadInterface) {
                $transactionId = $payload->getOrderTransaction()->getId();
            }

            return $responseClass::create($transactionIdjson_decode($content, true, 512, \JSON_THROW_ON_ERROR));
        } catch (GuzzleException) {
            return null;
        }
    }

    /** * @return array<string, mixed> */
    
Context $context
    ): ?Struct {
        $optionRequest = $this->getRequestOptions($payload$app$context);

        try {
            $response = $this->client->post($url$optionRequest);

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

            $transactionId = null;
            if ($payload instanceof PaymentPayloadInterface) {
                $transactionId = $payload->getOrderTransaction()->getId();
            }

            return $responseClass::create($transactionId, \json_decode($content, true, 512, \JSON_THROW_ON_ERROR));
        } catch (GuzzleException) {
            return null;
        }
    }

    /** * @return array<string, mixed> */
    
use Shopware\Core\Checkout\Payment\Cart\SyncPaymentTransactionStruct;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

#[Package('checkout')] class DebitPayment extends DefaultPayment
{
    public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void
    {
        $this->transactionStateHandler->process($transaction->getOrderTransaction()->getId()$salesChannelContext->getContext());
    }
}

#[Package('checkout')] class SyncTestPaymentHandler implements SynchronousPaymentHandlerInterface
{
    public function __construct(private readonly OrderTransactionStateHandler $transactionStateHandler)
    {
    }

    public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void
    {
        $transactionId = $transaction->getOrderTransaction()->getId();
        $order = $transaction->getOrder();

        $lineItems = $order->getLineItems();
        if ($lineItems === null) {
            throw new SyncPaymentProcessException($transactionId, 'lineItems is null');
        }

        $customer = $order->getOrderCustomer()?->getCustomer();
        if ($customer === null) {
            throw new SyncPaymentProcessException($transactionId, 'customer is null');
        }

        
public function testPayload(): void
    {
        $transaction = new OrderTransactionEntity();
        $order = new OrderEntity();
        $requestData = ['foo' => 'bar'];
        $recurring = new RecurringDataStruct('foo', new \DateTime());
        $source = new Source('foo', 'bar', '1.0.0');

        $payload = new RecurringPayPayload($transaction$order$requestData$recurring);
        $payload->setSource($source);

        static::assertEquals($transaction$payload->getOrderTransaction());
        static::assertSame($order$payload->getOrder());
        static::assertSame($requestData$payload->getRequestData());
        static::assertSame($recurring$payload->getRecurring());
        static::assertSame($source$payload->getSource());
    }
}
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

/** * @internal */
#[Package('checkout')] class SyncTestFailedPaymentHandler implements SynchronousPaymentHandlerInterface
{
    public function pay(SyncPaymentTransactionStruct $transaction, RequestDataBag $dataBag, SalesChannelContext $salesChannelContext): void
    {
        throw PaymentException::syncProcessInterrupted($transaction->getOrderTransaction()->getId(), 'This is a TestPaymentHandler which will always fail');
    }
}
Home | Imprint | This part of the site doesn't use cookies.