Work with WebMoney API

    WebMoney Transfer has long been for many the usual way of making payments on the Internet. One of the advantages of the system is its rich API for interacting with third-party software. Through it, you can receive the status of your accounts, make money transfers, issue invoices to customers for payment, control their payment, return payments without commission, work with payments with patronage, receive information from passports of other users, send messages via WM internal mail. The WebMoney website has good API documentation .

    Your humble servant wrote 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 following line to the script that will use the API:

    use Business :: WebMoney;
    

    For the module to work, you need a secret key and certificate, which are exported from the browser as a PKCS12 file (.p12) after registration in 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 in the form of 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 what wallets we have and how much money lies in them:

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

    The reqn parameter is in all interface requests. 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 to transfer money to 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 a list of wallets, for each of which its name (pursename field), currency (desc field) and remaining money (amount field) are obtained.

    For an online store - we will issue an invoice, verify the payment, return the money


    The easiest way to create an online store is to use the WebMoney Merchant service: register, get a code for the form, install it on the site. In case of successful payment, you will receive an http-request from the Merchant server with the payment number, amount, currency and a protective md5-convolution. In the event of an emergency, unprocessed situations will occur. 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 online store server. Or a situation where it is impossible to complete a customer order, and it is necessary to return the money. All this can be automated using the WebMoney API.

    First, we’ll issue an invoice (when using Merchant, this is done automatically, and this step must 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 invoiced customer. storepurse is the wallet we are waiting for money for. amount - amount, desc - description.

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

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

    The reference to the hash array is returned, we check the state field of each. 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 done so already. 1 - the bill was paid with patronage, 3 - refused to pay. The wmtranid field is the transaction ID that the bill was paid for. He is still useful to us.

    If we want to return money for an unproven service, it is enough to call the money_back method:

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

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

    And finally, we will give thanks for the purchase (of course, this is just a demonstration of the possibilities. Customers do not need to spam):

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

    Connect API Access


    To get access to most of the listed functions, you need to have a personal certificate and write a letter via internal WebMoney to WM Technical Support with a request for connection and a list of IPs from which access will be made.

    Key Security


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

    Also popular now: