📜 ⬆️ ⬇️

QIWI and the new REST protocol in the examples

Greetings.
I am a longtime reader of Habr and often find answers to my questions here. But it so happened that the answer could not be found. And nowhere.
And the task was the following. Implement a new (REST) ​​protocol for the QIWI system. I had to spend a few days to find the right solution through trial, error, communication with the manager and the support team, because There are no examples anywhere, although the task is not so difficult.
In this post I want to share those examples that I missed so much. Hope this helps someone.
I must say that I will not paint, chew and explain in detail. There will be only basic, which I consider necessary. Yet everyone has their own preferences and goals.
First you need to register your store http://ishopnew.qiwi.ru/
After registering in the Connection Methods section, you will find the New Protocol item. There you can download a description of the API of the new protocol.

For sending requests I used cURL.
Here’s how it was:
$requestType = 'POST'; //  REST    $url = 'some url'; //     $parameters = array(); //      $loginPass = $id . ':' . $password; // ID   (    ) $headers = array( "Accept: text/json", "Content-Type: application/x-www-form-urlencoded; charset=utf-8" ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $loginPass); if ($requestType != 'GET') { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requestType); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters)); } $httpResponse = curl_exec($ch); if (!$httpResponse) { //  ,   echo curl_error($ch).'('.curl_errno($ch).')'; return false; } $httpResponseAr = json_decode($httpResponse); return $httpResponseAr->response; 

I think everything is simple and clear.

Now what operations can be carried out.

The documentation describes everything in some detail. I just focus on some things that have hampered my development.
')
I will describe separately for each operation.

1) User invoicing
 $requestType = 'PUT'; $url = 'https://w.qiwi.com/api/v2/prv/{prv_id}/bills/{bill_id}'; // {prv_id} –   ,    ID . // {bill_id} –   ,          . $parameters = array( 'user' => 'tel:+79999999999', 'amount' => 0.1, 'ccy' => 'RUB', 'comment' => ' QIWI', 'pay_source' => 'qw', 'lifetime' => date('c', $timePlusHour), 'prv_name' => 'QIWI', ); 

With the parameters everything is clear.

2) Request account status
If the invoice was not paid after the invoice, we need to check its status. Perhaps the system delayed the response or the user decided to pay for it a little later, in this case, you can use cron.
 $requestType = 'GET'; $url = 'https://w.qiwi.com/api/v2/prv/{prv_id}/bills/{bill_id}'; //   $parameters = array(); 


3) Forwarding to pay the bill
After successful invoicing, you need to redirect the user to the payment page. Here cURL is not used, just redirect.
 $url = 'https://w.qiwi.com/order/external/main.action?shop={prv_id }&transaction={bill_id}'; $url .= '&successUrl=' . $successUrl; $url .= '&failUrl=' . $failUrl; // {prv_id}  {bill_id}     . // $successUrl  $failUrl         . 


4) Cancellation of unpaid invoice
Used to cancel the invoice. If you invoice, but it is not paid, the system will send to the address specified in the store settings, POST requests for confirmation of the account status. If you do not answer, then you will end up with a letter to the email specified in the store settings.
 $requestType = 'PATCH'; $url = 'https://w.qiwi.com/api/v2/prv/{prv_id}/bills/{bill_id}'; //   $parameters = array( 'status' => 'rejected' ); 


5) Return of funds on the paid invoice
You can request a refund.
 $requestType = 'PUT'; $url = 'https://w.qiwi.com/api/v2/prv/{prv_id}/bills/{bill_id}/refund/{refund_id}'; //  ,  {refund_id}    $parameters = array( 'amount' => $amount ); 


6) Check return status
You also need to check the status of the return on the request described above.
 $requestType = 'GET'; $url = 'https://w.qiwi.com/api/v2/prv/{prv_id}/bills/{bill_id}/refund/{refund_id}'; //  ,  {refund_id}         $parameters = array(); 


The server response is described in detail in the documentation.

You will also need to create two POST request handlers, about which I wrote in paragraph 4. The request will be sent several parameters that will need to be processed.
  1. bill_id = BILL-1
  2. status = paid
  3. error = 0
  4. amount = 1.00
  5. user = tel% 3A% 2B79031811737
  6. prv_name = TEST
  7. ccy = RUB
  8. comment = test
  9. command = bill

Depending on what address the request came from, you should check and decide what to do with the payment. Either transfer the money if the payment is passed and the request is successful. To reduce the balance, if the money was credited, but the payment was unsuccessful.

And of course I want to express a separate indignation about testing. The QIWI system does not have a test mode or a test platform, so you have to use your wallet and make payments, for example, 10 kopecks.

I hope that with this message I have helped at least a little those who are going to use the new protocol in their project. After all, he promises to be able to receive payment from all countries where there is QIWI.

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


All Articles