📜 ⬆️ ⬇️

Work with WebMoney API

WebMoney Transfer has long been for many the usual way of payment on the Internet. One of the advantages of the system is a rich API for interacting with third-party services software. Through it, you can get the status of your bills, make money transfers, bill clients for payment, control their payment, return payments without commission, work with payments with protection, receive information from other users' certificates, send messages via internal WM mail. The WebMoney site has good API documentation .

Your submissive servant has written the module Business :: WebMoney, which is distributed under a free license and received the status of the official Perl-interface of the system.

Connecting Business :: WebMoney - everything, as usual, without surprises


First install the module from CPAN:

 # perl -MCPAN -e 'install Business :: WebMoney'

After installing the module, add the line to the script that will use the API:
')
 use Business :: WebMoney;

The module requires a private key and certificate, which are exported from the browser as a PKCS12 file (.p12) after registering with WebMoney Keeper Light. Keys from WebMoney Keeper Classic are not suitable - the module does not support working with them (although there is a workaround - see the last paragraph). The path to the p12 file and its password are specified as constructor parameters:

 my $ wm = Business :: WebMoney-> new (
         p12_file => '/path/to/your/certificate.p12',
         p12_pass => 'certificate_password',
 );

The first check is how much money we have left.


First, let's see which wallets we have, and how much money they have in them:

 my $ res = $ wm-> get_balance (
         reqn => ++ $ reqn,
         wmid => '123456789012',
 ) or die $ wm-> errstr;

The reqn parameter is in all requests to interfaces. This is an integer that must be greater than the last money transfer request. In your application, you must provide some way to fulfill this condition. Either save the last used reqn for transferring money in the database, or implement some other mechanism to ensure this condition, for example, attach to the current time.

The wmid parameter is your Webmoney ID. If your main WMID has access to other WMIDs, then you can watch the wallets of any of them. The get_balance method returns a link to the list of wallets, for each of which its name is obtained (pursename field), currency (desc field) and the rest of money (amount field).

For an online store - we will issue an invoice, check the payment, refund the money


The easiest way to make an online store is to use the WebMoney Merchant service: register, get the code for the form, install it on the site. In case of successful payment, you will receive an http request from the Merchant system server with the payment number, amount, currency, and md5 security convolution. In case of emergency situations, there will be unprocessed situations. For example, if for some reason the http-request for a successful payment fails, then you will receive a complaint from the client that he paid the money and the service was not provided to him. This means that you need to periodically check the payment of bills on the initiative of the server of the online store. Or a situation when it is impossible to fulfill a customer’s order and it’s necessary to return the money All this can be automated using the WebMoney API.

To begin with, we will issue an invoice (when using Merchant, this is done automatically, and this step should be skipped):

 my $ res = $ wm-> invoice (
         reqn => ++ $ reqn,
         orderid => $ orderid,
         customerwmid => $ contragent_wmid,
         storepurse => $ my_purse,
         amount => 1,000,000
         desc => 'Pay a million and you will be happy'
 ) or die $ wm-> errstr;

Orderid is the account number in our own database. Customerwmid is the WMID of the buyer who is billed. storepurse - a wallet for which we are waiting for money. amount - amount, desc - description.

Now we will periodically request a list of invoices issued by us for the time interval $ datestart - $ datefinish, and monitor the payment on them:

 my $ res = $ wm-> get_out_invoices (
         reqn => ++ $ reqn,
         purse => $ my_purse,
         datestart => $ datestart,
         datefinish => $ datefinish,
 ) or die $ res-> errstr;

The link to the array of hashes is returned, we check the state field for everyone. If there is 0, then the bill has not yet been paid. If 2, then the bill is paid - you can provide a service, if we have not already done so. 1 - the bill was paid with protection, 3 - refused to pay. The wmtranid field is the transaction ID for which the invoice was paid. He is still useful to us.

If we want to return money for undelivered service, it’s enough to call the money_back method:

 my $ res = $ wm-> money_back (
         reqn => ++ $ reqn,
         inwmtranid => $ wmtranid,
         amount => $ amount,
 ) or die $ wm-> errstr;

We transfer it to the inwmtranid ID field of the transaction with which we were paid money, and in the amount - the amount of payment. Money will be returned to the buyer, and (attention!) 0.8% without commission.

And finally, we’ll give thanks for the purchase (of course, this is just a demonstration of opportunities. No need to spam customers):

 my $ res = $ wm-> message (
         reqn => ++ $ reqn,
         receiverwmid => $ contragent_wmid,
         msgsubj => 'Message Subject',
         msgtext => 'Message text',
 ) or die $ wm-> errstr;

API Access Connection


To gain access to most of the listed functions, you must have a personal passport and write a letter via WebMoney internal mail to WM Technical Support with a request to connect and a list of IPs from which access will be made.

Key Security


In most cases, you can do without putting the main keys to the wallet on the combat server. This can be achieved through the mechanism of powers of attorney WebMoney. You can create a separate WMID, delegate to it the authority to invoice and control payment on behalf of the main WMID, and even if the key is compromised, your money will be safe.

Source: https://habr.com/ru/post/126251/


All Articles