Skip to content

Instantly share code, notes, and snippets.

@ishukshin
Last active May 8, 2016 05:16
Show Gist options
  • Save ishukshin/41d2ecab4e3c8468b1dc to your computer and use it in GitHub Desktop.
Save ishukshin/41d2ecab4e3c8468b1dc to your computer and use it in GitHub Desktop.
$configs = [
'shopId' => '12345',
'ShopPassword' => 'abcdefgh',
'scId' => '3456',
];
  1. Форма отправляет пользователя на money.yandex, где он вводит оплату
  2. Яндекс.касса обращается по заданному в её настройках урлу, допустим, https://site.ru/y/check/, и передаёт разные параметры, ожидая, что мы проверим заказ и возвратим ответ в нужном формате (см. метод check)
  3. Если ответ хороший, касса обращается к авизо-урлу https://site.ru/y/aviso/, передавая параметры оплаты.
  4. Если от авизо ответ, какой яндекс хочет (см. метод aviso), оплата завершена и юзеру выводится уведомление об этом и ссылка "Вернуться в магазин", задаваемая в shopSuccessURL в форме.
  5. Если мы выдаём код ошибки, у юзера оплата не проходит.

https://money.yandex.ru/doc.xml?id=526537 - тут описаны форматы вышеуказанных ответов

https://tech.yandex.ru/money/apps/ - доки в том числе на оплату с мобильных. Что проще (SDK или через веб) - не знаю. Мне проще веб.

<form action="https://demomoney.yandex.ru/eshop.xml" method="post">
<input name="shopId" value="<?=$this->yandexConfig['shopId']?>" type="hidden"/>
<input name="scid" value="<?=$this->yandexConfig['scId']?>" type="hidden"/>
<input name="sum" value="<?=$price?>" type="hidden">
<input type="hidden" name="CustomerNumber" value="<?=$user->getId();?>">
<input name="paymentType" value="AC" type="hidden"/> <!-- а тут можно задать типы оплаты через select, если нужно что-то, кроме карт -->
<input name="orderNumber" value="<?=$order->getId();?>" type="hidden"/>
<input name="cps_email" value="<?=$this->escape($user->getEmail());?>" type="hidden"/>
<input name="shopSuccessURL" type="hidden" value="http://site.ru/payd/"/>
<input name="shopFailURL" type="hidden" value="http://site.ru/fail/"/>
<input type="submit" value="Заплатить" />
</form>
<?php
class YK{
/* ... */
/**
* проверка заказа
* @param array $params параметры от яндекса
*/
public function check($params){
$code = 0;
$message = '';
if(!isset($params['orderNumber']) or !$params['orderNumber']){
$code = 100;
$message = 'no number';
} else {
$order = $this->entityManager->find($params['orderNumber']);
if(!$order){
$message = 'no such order';
$code = 100;
}
}
if($order and $order->getProviderPrice() != $params['orderSumAmount']){
$message = 'Wrong sum. Price should be the same as was placed via placeOrder()';
$order->setStatus('error');
$order->setPaydDetails($params);
$this->entityManager->persist($order);
$this->entityManager->flush();
$code = 100;
}
if(!$code) {
$hash = md5($params['action'] . ';' . $params['orderSumAmount'] . ';' . $params['orderSumCurrencyPaycash'] . ';' . $params['orderSumBankPaycash'] . ';' . $this->configs['shopId'] . ';' . $params['invoiceId'] . ';' . $params['customerNumber'] . ';' . $this->configs['ShopPassword']);
if (strtolower($hash) != strtolower($params['md5'])) {
$code = 1;
}
// $code = 0; // REMOVE IT
}
$ret = '<?xml version="1.0" encoding="UTF-8"?>';
$ret.= '<checkOrderResponse performedDatetime="' . $params['requestDatetime'] . '" message="'.$message.'" code="' . $code . '"' . ' invoiceId="' . $params['invoiceId'] . '" shopId="' . $this->configs['shopId'] . '"/>';
echo $ret;
}
/**
* проведение платежа успешно, отмечаем отмеченность
* @param array $params
*/
public function aviso($params) {
$code = 0;
$message = '';
if(!isset($params['orderNumber']) or !$params['orderNumber']){
$code = 200;
$message = 'no number';
} else {
$order = $this->entityManager->find($params['orderNumber']);
if(!$order){
$message = 'no such order';
$code = 200;
}
}
if(!$code){
$hash = md5($params['action'] . ';' . $params['orderSumAmount'] . ';' . $params['orderSumCurrencyPaycash'] . ';' . $params['orderSumBankPaycash'] . ';' . $this->configs['shopId'] . ';' . $params['invoiceId'] . ';' . $params['customerNumber'] . ';' . $this->configs['ShopPassword']);
if (strtolower($hash) != strtolower($params['md5'])) {
$code = 1;
} else {
$code = 0;
}
# отмечаем оплаченность и сохраняем детали
$order->setIspayd(true);
$order->setStatus('work'); // new не бывает у ispayd=true
$order->setPaydDetails($params);
$this->entityManager->persist($order);
$this->entityManager->flush();
}
$ret = '<?xml version="1.0" encoding="UTF-8"?>';
$ret.= '<paymentAvisoResponse performedDatetime="' . $params['requestDatetime'] . '" code="' . $code . '" invoiceId="' . $params['invoiceId'] . '" shopId="' . $this->configs['shopId'] . '"/>';
echo $ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment