-
Notifications
You must be signed in to change notification settings - Fork 2
feat: create MQ jobs to resynch payment profiles from external MS #453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+798
−44
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3716b8e
feat: create MQ jobs to resynch payment profiles from external MS
smarcet 2f08e3a
chore: update jobs mappings
smarcet 96438db
chore: add application_type check
smarcet 210e40c
chore: fix retries
smarcet 38782a6
chore: add log info
smarcet c9e95aa
chore: fix webhook creation type
smarcet 36ef8cc
chore: add command mq:setup_payment_service_message_broker {exchange_…
smarcet 358b5d7
chore: update readme.md
smarcet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
app/Console/Commands/SetupPaymentServiceMessageBrokerCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <?php namespace App\Console\Commands; | ||
| /* | ||
| * Copyright 2025 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
| use App\Jobs\Payments\EventTypes; | ||
| use Exception; | ||
| use Illuminate\Console\Command; | ||
| use Illuminate\Support\Facades\Config; | ||
| use Illuminate\Support\Facades\Log; | ||
| use PhpAmqpLib\Connection\AMQPStreamConnection; | ||
|
|
||
| /** | ||
| * Class SetupSponsorServiceMessageBrokerCommand | ||
| * @package App\Console\Commands | ||
| */ | ||
| final class SetupPaymentServiceMessageBrokerCommand extends Command | ||
| { | ||
| /** | ||
| * The console command name. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $name = "setup_payment_service_message_broker"; | ||
|
|
||
| /** | ||
| * The name and signature of the console command. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $signature = "mq:setup_payment_service_message_broker {exchange_name} {exchange_type}"; | ||
|
|
||
| /** | ||
| * The console command description. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $description = "Set up Payment Service rabbitmq exchange, queue and bindings"; | ||
|
|
||
| /** | ||
| * Execute the console command. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function handle(): void | ||
| { | ||
| $host_settings_path = "queue.connections.message_broker.hosts.0"; | ||
| $queue_settings_path = "queue.connections.payments_sync_consumer"; | ||
| $host_settings = Config::get($host_settings_path); | ||
|
|
||
| $exchange_name = $this->argument('exchange_name'); | ||
|
|
||
| if (empty($exchange_name)) | ||
| throw new \InvalidArgumentException("exchange_name is required"); | ||
|
|
||
| $exchange_type = $this->argument('exchange_type'); | ||
|
|
||
| if (empty($exchange_type)) | ||
| throw new \InvalidArgumentException("exchange_type is required"); | ||
|
|
||
| if (!$host_settings) { | ||
| throw new \InvalidArgumentException("Host setting not found at {$host_settings_path}"); | ||
| } | ||
| $queue_settings = Config::get($queue_settings_path); | ||
|
|
||
| $host = $host_settings['host']; | ||
| $port = $host_settings['port']; | ||
| $user = $host_settings['user']; | ||
| $password = $host_settings['password']; | ||
| $vhost = $host_settings['vhost']; | ||
|
|
||
| $queue = $queue_settings['queue']; | ||
|
|
||
| $routingKeys = [ | ||
| EventTypes::PAYMENT_PROFILE_UPDATED, | ||
| EventTypes::PAYMENT_PROFILE_CREATED, | ||
| EventTypes::PAYMENT_PROFILE_DELETED, | ||
| ]; | ||
|
|
||
| try { | ||
| $connection = new AMQPStreamConnection($host, $port, $user, $password, $vhost); | ||
| $channel = $connection->channel(); | ||
|
|
||
| // Exchange | ||
| $channel->exchange_declare($exchange_name, $exchange_type, false, true, false); | ||
|
|
||
| // Queue | ||
| $channel->queue_declare($queue, false, true, false, false); | ||
|
|
||
| // Bindings | ||
| foreach ($routingKeys as $key) { | ||
| $channel->queue_bind($queue, $exchange_name, $key); | ||
| echo "Binding created: $queue ← $exchange_name ($key)\n"; | ||
| } | ||
|
|
||
| $channel->close(); | ||
| $connection->close(); | ||
| echo "Done.\n"; | ||
|
|
||
| } catch (Exception $ex) { | ||
| echo "Error: " . $ex->getMessage() . "\n"; | ||
| Log::error($ex); | ||
| exit(1); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php namespace App\Jobs\Payments; | ||
| /* | ||
| * Copyright 2025 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use App\Services\Apis\IPaymentsApi; | ||
| use App\Services\Model\Imp\PaymentGatewayProfileService; | ||
| use Illuminate\Bus\Queueable; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
| use Illuminate\Foundation\Bus\Dispatchable; | ||
| use Illuminate\Queue\InteractsWithQueue; | ||
| use Illuminate\Queue\SerializesModels; | ||
| use Illuminate\Support\Facades\Log; | ||
| use models\summit\IPaymentConstants; | ||
| use models\summit\ISummitRepository; | ||
| use models\summit\Summit; | ||
|
|
||
| class CreatePaymentProfileMQJob implements ShouldQueue | ||
| { | ||
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
|
||
| public int $tries = 3; | ||
|
|
||
| private PaymentGatewayProfileService $service; | ||
|
|
||
| private IPaymentsApi $payments_api; | ||
|
|
||
| private ISummitRepository $summit_repository; | ||
|
|
||
|
|
||
| /** | ||
| * @param ISummitRepository $summit_repository | ||
| * @param PaymentGatewayProfileService $service | ||
| * @param IPaymentsApi $payments_api | ||
| */ | ||
| public function __construct | ||
| ( | ||
| ISummitRepository $summit_repository, | ||
| PaymentGatewayProfileService $service, | ||
| IPaymentsApi $payments_api | ||
| ){ | ||
| $this->summit_repository = $summit_repository; | ||
| $this->service = $service; | ||
| $this->payments_api = $payments_api; | ||
| } | ||
|
|
||
| public function handle(PaymentsMQJob $job): void{ | ||
| try { | ||
| $payload = $job->payload(); | ||
| $json = json_encode($payload); | ||
| Log::debug("CreatePaymentProfileMQJob::handle", ['payload' => $json ]); | ||
| $data = $payload['data']; | ||
| $id = intval($data['id']); | ||
| $summit_id = intval($data['summit_id']); | ||
| $application_type = $data['application_type']; | ||
| if(!in_array($application_type, IPaymentConstants::ValidApplicationTypes)){ | ||
| Log::warning("CreatePaymentProfileMQJob::handle Application Type $application_type is not valid."); | ||
| return; | ||
| } | ||
| $response = $this->payments_api->getPaymentProfile($summit_id, $id); | ||
| Log::debug("CreatePaymentProfileMQJob::handle", ['response' => $response]); | ||
| $summit = $this->summit_repository->getById($summit_id); | ||
| if($summit instanceof Summit) { | ||
| // mappings | ||
| $response['external_id'] = $id; | ||
| $response['active'] = $response['is_active'] ?? false; | ||
| Log::debug("CreatePaymentProfileMQJob::handle creating payment profile", ['response' => $response ]); | ||
| $this->service->addPaymentProfile($summit, $response); | ||
| } | ||
| $job->delete(); | ||
| } catch (\Exception $ex) { | ||
| Log::error($ex); | ||
| throw $ex; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php namespace App\Jobs\Payments; | ||
| /* | ||
| * Copyright 2025 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use App\Models\Foundation\Summit\Repositories\IPaymentGatewayProfileRepository; | ||
| use App\Services\Model\Imp\PaymentGatewayProfileService; | ||
| use Illuminate\Bus\Queueable; | ||
| use Illuminate\Contracts\Queue\ShouldQueue; | ||
| use Illuminate\Foundation\Bus\Dispatchable; | ||
| use Illuminate\Queue\InteractsWithQueue; | ||
| use Illuminate\Queue\SerializesModels; | ||
| use Illuminate\Support\Facades\Log; | ||
| use models\summit\IPaymentConstants; | ||
| use models\summit\ISummitRepository; | ||
| use models\summit\PaymentGatewayProfile; | ||
| use models\summit\Summit; | ||
|
|
||
| class DeletePaymentProfileMQJob implements ShouldQueue | ||
| { | ||
| use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
|
||
| public int $tries = 3; | ||
|
|
||
| private PaymentGatewayProfileService $service; | ||
|
|
||
| private ISummitRepository $summit_repository; | ||
|
|
||
| private IPaymentGatewayProfileRepository $payment_gateway_profile_repository; | ||
|
|
||
| public function __construct | ||
| ( | ||
| ISummitRepository $summit_repository, | ||
| IPaymentGatewayProfileRepository $payment_gateway_profile_repository, | ||
| PaymentGatewayProfileService $service | ||
| ){ | ||
| $this->summit_repository = $summit_repository; | ||
| $this->payment_gateway_profile_repository = $payment_gateway_profile_repository; | ||
| $this->service = $service; | ||
| } | ||
|
|
||
| public function handle(PaymentsMQJob $job): void{ | ||
| try { | ||
| $payload = $job->payload(); | ||
| $json = json_encode($payload); | ||
| Log::debug("DeletePaymentProfileMQJob::handle", ['payload' => $json ]); | ||
|
|
||
| $data = $payload['data']; | ||
|
|
||
| $id = intval($data['id']); | ||
| $summit_id = intval($data['summit_id']); | ||
| $application_type = $data['application_type']; | ||
| if(!in_array($application_type, IPaymentConstants::ValidApplicationTypes)){ | ||
| Log::warning("DeletePaymentProfileMQJob::handle Application Type $application_type is not valid."); | ||
| return; | ||
| } | ||
|
|
||
| $summit = $this->summit_repository->getById($summit_id); | ||
| $local_payment_profile = $this->payment_gateway_profile_repository->getByExternalId($id); | ||
|
|
||
| if($summit instanceof Summit && $local_payment_profile instanceof PaymentGatewayProfile){ | ||
| $local_payment_profile_id = $local_payment_profile->getId(); | ||
| Log::warning("DeletePaymentProfileMQJob::handle deleting payment profile", ['local_payment_profile_id' => $local_payment_profile_id ]); | ||
| $this->service->deletePaymentProfile($summit, $local_payment_profile_id); | ||
| } | ||
| $job->delete(); | ||
| } catch (\Exception $ex) { | ||
| Log::error($ex); | ||
| throw $ex; | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php namespace App\Jobs\Payments; | ||
| /* | ||
| * Copyright 2025 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| /** | ||
| * Class EventTypes | ||
| * @package App\Jobs\SponsorServices | ||
| */ | ||
| final class EventTypes | ||
| { | ||
| const string PAYMENT_PROFILE_CREATED = 'payment_profile_created'; | ||
| const string PAYMENT_PROFILE_UPDATED = 'payment_profile_updated'; | ||
| const string PAYMENT_PROFILE_DELETED = 'payment_profile_deleted'; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php namespace App\Jobs\Payments; | ||
| /** | ||
| * Copyright 2025 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use Illuminate\Support\Facades\Log; | ||
| use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob as BaseJob; | ||
|
|
||
| class PaymentsMQJob extends BaseJob | ||
| { | ||
| public int $tries = 1; | ||
|
|
||
| /** | ||
| * Get the decoded body of the job. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function payload(): array | ||
| { | ||
| $routing_key = $this->getRabbitMQMessage()->getRoutingKey(); | ||
| Log::debug("PaymentsMQJob::payload processing job", ['routing_key' => $routing_key]); | ||
| switch ($routing_key) { | ||
| case EventTypes::PAYMENT_PROFILE_CREATED: | ||
| $job = 'App\Jobs\Payments\CreatePaymentProfileMQJob@handle'; | ||
| break; | ||
| case EventTypes::PAYMENT_PROFILE_UPDATED: | ||
| $job = 'App\Jobs\Payments\UpdatePaymentProfileMQJob@handle'; | ||
| break; | ||
| case EventTypes::PAYMENT_PROFILE_DELETED: | ||
| $job = 'App\Jobs\Payments\DeletePaymentProfileMQJob@handle'; | ||
| break; | ||
| default: | ||
| Log::warning('Received an unknown routing key', ['routing_key' => $routing_key, 'message' => $this->getRawBody()]); | ||
| return []; | ||
| } | ||
| return [ | ||
| 'job' => $job, | ||
| 'data' => json_decode($this->getRawBody(), true) | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.