tigr/yandex-kassa

yandex kassa 的辅助工具,帮助处理回调和请求。

0.1.3 2017-09-04 08:25 UTC

This package is auto-updated.

Last update: 2024-09-14 04:24:28 UTC


README

Build Status Minimum PHP Version Coverage Status Total Downloads Latest Stable Version License

yandex kassa 的辅助工具,帮助处理回调和请求。

安装

composer require tigr/yandex-kassa

用法

  1. 创建处理 yandex kassa 请求的控制台,并在其中执行类似以下操作:
<?php

   use TiGR\YandexKassa\YandexKassaHelper;
   use TiGR\YandexKassa\Exception\AuthorizationErrorException;
   use TiGR\YandexKassa\Exception\BadRequestException;

   $helper = new YandexKassaHelper(KASSA_SHOP_ID, KASSA_SHOP_PASSWORD);

   $errorStatus = YandexKassaHelper::STATUS_BAD_REQUEST;

   if ($helper->isCheckOrderAction()) {
       $errorStatus = YandexKassaHelper::STATUS_PAYMENT_REJECTED;
   }

   try {
       $helper->parseRequest($_POST);
   } catch (AuthorizationErrorException $e) {
       // ... handle this exception ...

       return $this->xmlResponse($helper->buildResponse(null, $e->getMessage()));
   } catch (BadRequestException $e) {
       // ... handle this exception ...

       return $this->xmlResponse($helper->buildResponse(null, $e->getMessage()));
   } catch (\Exception $e) {
       // ... handle this exception ...

       return $this->xmlResponse($helper->buildResponse($errorStatus));
   }

   try {
       $payment = $helper->getPayment();

       // ... do some validation using $payment data ...

       if ($helper->isPaymentAvisoAction()) {
           // ... Mark this payment as settled in your system ...
           // ... log successful transaction, if needed ...
           // ... notify user of successful transaction. if needed ...
       }

       return $this->xmlResponse($helper->buildResponse(/* successful by default */));
   } catch (\Exception $e) {
       // ... handle this exception ...

       return $this->xmlResponse(
           $helper->buildResponse(YandexKassaHelper::STATUS_PAYMENT_REJECTED, $e->getMessage())
       );
   }