I am using stripe for payment and all working fine if stripe payment done on website itself but if stripe is used for redirect to website for payment then the observer is not working as expected.
Observer get call even before the stripe return to website. So I am not getting trasaction id or any data from stripe.
event.xml
<!-- Backup observer using sales_order_place_after -->
<event name="sales_order_place_after">
<observer name="market_subscription_activation_order_place"
instance="MarketSubscriptionActivationObserverOrderPlaceAfter"/>
</event>
and OrderPlaceAfter.php
public function execute(Observer $observer) {
/** @var OrderInterface $order */
$order = $observer->getEvent()->getOrder();
if (!$order || !$order->getId()) {
return;
}
$this->logger->info('Order Place After Observer - Triggered', [
'order_id' => $order->getId(),
'order_increment_id' => $order->getIncrementId()
]);
try {
// Check each order item for activation-eligible products
foreach ($order->getItems() as $item) {
$product = $item->getProduct();
// Skip if product doesn't have external_package_id
if (!$product || !$product->getData('external_package_id')) {
continue;
}
$this->processActivation($order, $item);
}
} catch (Exception $e) {
$this->logger->error('Order Place After Observer - Error', [
'order_id' => $order->getId(),
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
// Don't throw - we don't want to break order placement
// Log error and continue
}
}
/**
* Process activation for a single order item
*
* @param OrderInterface $order
* @param $item
* @return void
* @throws Exception
*/
private function processActivation(OrderInterface $order, $item): void {
$product = $item->getProduct();
$externalPackageId = $product->getData('external_package_id');
$this->logger->info('Processing Activation', [
'order_id' => $order->getId(),
'product_sku' => $product->getSku(),
'external_package_id' => $externalPackageId
]);
// Get subscriber data
try {
$subscriber = $this->subscriberRepository->getByCustomerId((int) $order->getCustomerId());
} catch (Exception $e) {
$this->logger->warning('Subscriber not found for customer', [
'customer_id' => $order->getCustomerId(),
'error' => $e->getMessage()
]);
// Create activation record with error
$this->createActivationRecord($order, $item, null, 'failed', 'Subscriber not found');
return;
}
// Validate subscriber has IMSI
if (!$subscriber->getImsi()) {
$this->logger->warning('Subscriber missing IMSI', [
'customer_id' => $order->getCustomerId()
]);
$this->createActivationRecord($order, $item, null, 'failed', 'Subscriber missing IMSI');
return;
}
// Call Java API
try {
$javaResponse = $this->javaApiClient->requestPlanActivation(
[
'plan_id' => $externalPackageId,
'order_id' => $order->getIncrementId(),
'amount' => $order->getGrandTotal(),
'currency' => $order->getOrderCurrencyCode(),
'transaction_id' => $order->getPayment()->getLastTransId(),
'payment_method' => $order->getPayment()->getMethod(),
'payment_gateway' => "Stripe", // or PayPal if needed
'request_id' => uniqid("ReqID")
]
);
// Create activation record with request_id
$this->createActivationRecord(
$order,
$item,
$javaResponse['request_id'],
'activated',
null,
$subscriber
);
// Update order status
$order->setStatus('activated');
$order->addCommentToStatusHistory(
sprintf('Plan activation requested. Request ID: %s', $javaResponse['request_id'])
);
$order->save();
$this->logger->info('Activation request successful', [
'order_id' => $order->getId(),
'request_id' => $javaResponse['request_id']
]);
} catch (Exception $e) {
$this->logger->error('Java API call failed', [
'order_id' => $order->getId(),
'error' => $e->getMessage()
]);
// Create activation record with error
$this->createActivationRecord(
$order,
$item,
null,
'failed',
$e->getMessage(),
$subscriber
);
}
}