Interceptor after payment - Magento 2 - magento2

I need to crete an Interceptor after payment aprovation.
I currently use the class
Magento\Sales\Api\OrderRepositoryInterface
With function
afterSave
But not resolve my problem, because I need an event that fires after payment approval and not every time I change the order status.

Have a look at creating an observer instead:
Calling a Magento event Observer AFTER payment capture
How to create observer in magento:
https://devdocs.magento.com/guides/v2.4/extension-dev-guide/events-and-observers.html

Related

Paypal Webhook Event for card expiracy in a subscription

I would be pleased to know what is the webhook event fired by Paypal when, during a subscription, a payment fails (not at the subscription creation but at its renewal).
Thank you.
BILLING.SUBSCRIPTION.PAYMENT.FAILED
The most straightforward solution is to simply base all logic on PAYMENT.SALE.COMPLETED. If you don't receive one on time for a subscription, then it has lapsed.
Adding a custom_id when a subscription is first created can be useful for reconciliation.

Paypal Webhook is created sufficient to imply activated

Paypal with active webhooks. We use a subscription plan.
When a customer
subscribes to the subscription plan PayPal's webhook sends us a BILLING.SUBSCRIPTION.CREATED. 

We do not get a BILLING.SUBSCRIPTION.ACTIVATED. Can I expect it being activated when it is
created or do I need to double check that?
(Side note, when trying to send an api request to activate it, an error message informs me that the subscription plan is not “suspended”. So that looks like it is already activated. Logging into PayPal also confirms it is activated.)

The documentation on it is a little vague.
Created means the user clicked the button to subscribe. It does not mean they entered any payment details, or finished subscribing.
If you are not receiving a BILLING.SUBSCRIPTION.ACTIVATED event but the subscription is active, perhaps you did not subscribe to that event?
In any case, the most useful event to listen for is PAYMENT.SALE.COMPLETED. This will occur for the first payment as well as every future payment on the schedule. You can build all your business logic with only PAYMENT.SALE.COMPLETED , using every receipt of this event to mark how long the subscription is valid for in your system (e.g. 1 month from receipt)

Paypal Implement refunds

I'm trying to integrate "Paypal smart buttons" in my webpage which was with a deprecated paypal implementation.
On this new integration I'm using server-side SDK and I can create and capture an order and save data in my DB.
The problem I have now is to implement the refund.
On my last implementation I had an IPN Listener and whenever I get a refund I could register it in my BD.
Since now IPN listener is deprecated, how can I implemented the "listener function"? Is the webhooks the new approach to implement the listener? I know that with Webhooks Management API I can list all webhooks events, but then how can I related them with the payment made and registered before in my BD?
I've already read lots of official paypal documentation but this is no clear to me, yet.
Register a webhook listener URL for the events you want, including refunds.
When you receive a refund webhook event at that URL, the body of the webhook will contain information about the capture/payment object (transaction id) that was refunded.
If in addition to the capture/transaction id you need some additional id for reconciliation purposes, there are two pieces of information you can include in the original order creation.
an invoice_id, which is essentially your system's unique order ID. It must be unique, never used before for a successful completed transaction since it is used to block any future duplicate (accidental) payment attempts of the same ID.
a custom_id, which can have any arbitrary value and is not indexed and not visible to the payer.

PayPal subscription button with custom parameter

can someone please advise me on how to create a Paypal subscription button that is associated with my subscription plan and can contain a custom parameter?
I would like to specify a custom parameter in the button for each buyer such as CustomerId=1234 so, I can register the buyer as my member immediately.
I currently found the way to create a subscription button but cannot add any parameter.
Thank you in advance
When creating the subscription, add the parameter custom_id.
This is documented in the Create subscription request body.

PayPal Subscription API, Smart Payment Button and custom variables

I am trying to set up a recurring payment with PayPal Subscription REST API. I'm following the Basic Integration in PayPal Developer Portal, using as suggested the Smart Payment Button. Besides, I'm using a WebHook to "capture" events related to subscription.
As the tutoria suggest, I created a Product and a Plan.
Now, I rendered the Smart Payment Button in a page (using the example), and when clicked it triggers the PayPal authorization flow. When the user complete the subscription process, the BILLING.SUBSCRIPTION.CREATED event is triggered. Later, also the PAYMENT.SALE.COMPLETED and BILLING.SUBSCRIPTION.ACTIVATED are triggered, and I receive data in my WebHook. Now, I have troubles in identifying which user activated the subscription. My idea is to pass a custom variable (with something allows me to identify the user) and retrive it later when the WebHook post me data, but I can't undertand how to do it. Anyway, is this the best way to do it? Or there is another solution?
I solved this using "custom_id" in the smart button:
return actions.subscription.create({
'plan_id': 'PLAN_ID',
'custom_id': 'CUSTOM_ID'
});
Then, I receive custom_id from webhooks or querying the subscription.