📜 ⬆️ ⬇️

Library for working with QIWI through SOAP

It so happened that we decided to connect the acceptance of payments through QIWI. No sooner said than done! Here is only in the development process had to deal with the wretchedness of code examples from developers of kiwi:
Code of the server receiving the request from Qiwi
<?php /** *       QIWI . * SoapServer   SOAP-,    login, password, txn, status, *      Param    updateBill   TestServer. * *        updateBill. */ $s = new SoapServer('IShopClientWS.wsdl', array('classmap' => array('tns:updateBill' => 'Param', 'tns:updateBillResponse' => 'Response'))); // $s = new SoapServer('IShopClientWS.wsdl'); $s->setClass('TestServer'); $s->handle(); class Response { public $updateBillResult; } class Param { public $login; public $password; public $txn; public $status; } class TestServer { function updateBill($param) { //           $f = fopen('c:\\phpdump.txt', 'w'); fwrite($f, $param->login); fwrite($f, ', '); fwrite($f, $param->password); fwrite($f, ', '); fwrite($f, $param->txn); fwrite($f, ', '); fwrite($f, $param->status); fclose($f); //  password, login //      $param->status      if ($param->status == 60) { //   //      ($param->txn),    } else if ($param->status > 100) { //    ( ,      ..) //      ($param->txn),    } else if ($param->status >= 50 && $param->status < 60) { //     } else { //    } //     //           ,   0 // $temp->updateBillResult = 0 //     (,  ),    //    QIWI           0 //    24  $temp = new Response(); $temp->updateBillResult = 0; return $temp; } } ?> 

Of course, I understand, the example is exhaustive, but after all, was it possible to put something out of the way? Since the system is popular, as is the language of PHP - I decided to immediately bring the library to a public repository in order to simplify the lives of those who only have to connect this system. Since in my recent question, no one against the post did not object - I post it here.

The library has both a client and a server part. It is connected elementary - we unload it via git or add a dependency to the composer ( werkint / qiwi ). The main work goes through an instance of the class Qiwi \ Client. For example, you can inherit from it, and in the constructor specify the name / password of the store, and arrange the class as a service Symfony2 (as we have done):
Connection
 <?php namespace MyOwnMegaPrefix\Qiwi; use MyOwnMegaPrefix\Settings, Werkint\Qiwi; class MyQiwi extends Qiwi\Client { protected $settings; public function __construct( Settings $settings // ,     ) { $this->settings = $settings; parent::__construct( $this->settings->get('qiwi.login'), $this->settings->get('qiwi.pass') ); } } 

The work of the client (this is the one who sends requests to the SOAP-server) is elementary - we take an instance of Qiwi \ Client and call the appropriate methods. They are just simple wrappers on a class for working with php-soap, convert parameters, and return code is changed to a more detailed status. The date fields are in \ DateTime, everything else corresponds to api qiwi (only you don’t need to duplicate your login / password).
Working with the server is a bit more complicated. Since server methods are called rather cunningly (SOAP), it was decided to use closures to process the request. Yes, do not forget to disable the x509 certificate in the settings . I never found a way to tie WSSE to php-soap (as far as I understood, no one found it). It's a shame that in the sample code for Java there is a certificate verification.
Code of the server receiving the request from Qiwi
 use Werkint\Qiwi\ServerMethods\CheckBillResponse as QiwiBill; $callback = function ($bill) use (&$myMegaService) { /** @var QiwiBill $bill */ $row = $myMegaService->findByKey( //      $bill->id ); if (!$row) { throw new \Exception('  '); } $myMegaService->process($row); // -    return $myMegaService->status(); //     QIWI. 0 -   }; //     $theQiwiObject->processRequest($callback); //    text/html, qiwi    (  ,   ) header('Content-Type: text/xml; charset=utf-8'); 

Conveniently? I think so. You can simply compare with the alternative way . So, the advantages of the library:

By itself, there are downsides:

References: protocol description , sample code , repository .
Already after the system was bolted, I found out about the ready library for working with Kiwi - from the user JekaRu . So there will be more choice. :)
Thanks for attention. If there are comments on the purity of the code - I will hear it with pleasure.

UPD: Added description to Readme.md and translated lib to PSR-2
UPD2: Moved libu, nick4fake / ishop -> werkint / qiwi. For compatibility, I left the old code at the old address.

')

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


All Articles