JS Events
Last updated
Last updated
In CryptoPay, there are manageable processes hooks on the PHP side and events on the JS side. Events are accessible as windowCryptoPayApp.events and allow you to run extra code.
You can access all events and their examples below. Events do not need to have a return value.
Events always take parameters as context (ctx) objects.
If the option is on, all running events will always appear in the console. Parameter 3 is used to specify which file the event is in or where it is located. It is shown in the Init example.
In order to understand the structure of events in general, the init event will be illustrated in detail. The following events will be explained only in terms of the process in which they are executed and the parameters.
Also many events are awaited with await. So you can use promises for processes such as API requests etc. in events.
The Init event is the first process that runs after a network or currency is selected. In this process, the relevant MultipleChain provider is selected according to the user's choice, a request is sent to the API and the necessary data for the payment process is created.
The ctx parameter in the Init event is as follows:
network => The network selected by the user.
currency => The currency selected by the user.
init => Init value is the initial data in CryptoPay'js. In other words, the currency, payment amount, recipient address and similar values to be used in the payment process constitute the init object. The structure of the init object is given in the example.
And you can define the following two values:
You can define disableAutoQr to ctx in init event. It is true by default. This value controls the following. If there is only one network and one currency, and also if only QR payments are enabled as a payment option, CryptoPay will open the QR payment screen directly. However, in some specific cases you don't want this to happen. You can therefore control this with the disableAutoQr value.
What does this do? For example, when a network is selected in CryptoPay, it will always make a request to the init endpoint. But on the WooCommerce checkout page, we do a form check first. For this, we are already sending a request to an endpoint. It would be an unnecessary waste of time to send a request to the init endpoint right after that. For this reason, when doing form control, if there is no problem, we also return the init value. Then we add the value to ctx as specified below. If CryptoPay sees that the init value is correct, it will stop requesting the init endpoint.
Example:
To give an example of what we said in the above, the checkFormProcess value returns an object. If it contains the init value. The whole process works properly. But if you want to output an error message, this time you need to return a value with the error value. An example is given about this.
Event triggered when the user selects a network. It gives you the selected network and the previous network value in the context object.
It is the event that runs after a currency is selected or automatically updates the payment amount, i.e. immediately after the currency conversion request is sent. It gives you the current selected currency and payment amount as context.
It is the event that runs when the pay now button is clicked in the currency selection section after selecting the network in Network mode. It gives you the order object and the selected network as context.
Again like pay now, it is the event triggered when selecting currencies in network mode. It gives you the value of the previous and selected currency as context.
selectedNetwork, payNow and selectedCurrency are not triggered in currency list mode. When you select a currency in currency list mode, init is triggered directly and there is already the necessary data there. This is because in network mode they are all independent processes and the init event runs after payNow. When you select a currency in Currency mode, selectedNetwork, payNow, selectedCurrency processes are completed. For this reason, only the init event is sufficient.
This event is triggered when qr payment option is selected. It gives you order and network values as objects.
This event is the event that is triggered if the user makes the payment and a transaction is detected while waiting for the payment to be made on the qr payment page. In addition to the order and network values, it also gives you the hash id of the current transaction.
It is the event that runs when the wallet list is opened. You get order and network values as context.
It is the event that runs when the wallet list is closed. You get order and network values as context.
It is the event that runs when a connection request to a wallet is created and provides you with the order, network and the object of the wallet selected by the user in context.
It works when the wallet connection request is approved.
It works when the wallet connection request is declined.
The event that runs before the transfer (payment) request sent to the user after the wallet connection is confirmed.
It works when the wallet payment request is approved.
It works when the wallet payment request is declined.
It is triggered when a transaction is received after a QR or wallet payment.
It is triggered if the createTransaction setting is true in the config and there is a problem creating a transaction record in the API.
If the createTransaction setting is true in the config and the transaction record creation process completed successfully.
It is triggered when the user accepts the payment request or when the confirmation process starts after the transaction is captured in the qr payment process (if confirmation is true in the config).
Works when the verification step is complete.
It is an event that is triggered when the user clicks on the change button (option to go back to the beginning to change the network), if there is an error in the process, in short, when it is desired to go back to the beginning and the existing data is reset.
For example, we use it to disconnect the socket connection in QR payments.