Init
This commit is contained in:
84
payment/Best2Pay/Best2Pay.php
Normal file
84
payment/Best2Pay/Best2Pay.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
require_once('api/Simpla.php');
|
||||
|
||||
class Best2Pay extends Simpla
|
||||
{
|
||||
public function checkout_form($order_id, $button_text = null)
|
||||
{
|
||||
// Код валюты в Best2Pay
|
||||
$currency = '643';
|
||||
|
||||
if(empty($button_text))
|
||||
$button_text = 'Перейти к оплате';
|
||||
|
||||
$order = $this->orders->get_order((int)$order_id);
|
||||
$payment_method = $this->payment->get_payment_method($order->payment_method_id);
|
||||
$payment_settings = $this->payment->get_payment_settings($payment_method->id);
|
||||
|
||||
$price = $this->money->convert($order->total_price, $payment_method->currency_id, false);
|
||||
|
||||
$success_url = $this->config->root_url.'/order/'.$order->url;
|
||||
|
||||
// регистрационная информация (логин, пароль #1)
|
||||
// registration info (login, password #1)
|
||||
$sector = $payment_settings['sector'];
|
||||
|
||||
// номер заказа
|
||||
// number of order
|
||||
$id = $order->id;
|
||||
$password = $payment_settings['password'];
|
||||
|
||||
// адрес api
|
||||
if($payment_settings['mode'] == 'test')
|
||||
$best2pay_url = "https://test.best2pay.net";
|
||||
else
|
||||
$best2pay_url = "https://pay.best2pay.net";
|
||||
|
||||
|
||||
// описание заказа
|
||||
// order description
|
||||
$desc = 'Оплата заказа №'.$id;
|
||||
|
||||
// формирование подписи
|
||||
// generate signature
|
||||
$signature = base64_encode(md5($sector.($price*100).$currency.$password));
|
||||
|
||||
// Регистрируем заказ
|
||||
$url = $best2pay_url.'/webapi/Register';
|
||||
|
||||
|
||||
$data = array(
|
||||
'sector' => $sector,
|
||||
'reference' => $id,
|
||||
'amount' => $price*100,
|
||||
'description' => $desc,
|
||||
'email' => htmlspecialchars($order->email,ENT_QUOTES),
|
||||
'currency' => $currency,
|
||||
'mode' => 1,
|
||||
'url' => $success_url,
|
||||
'signature' => $signature
|
||||
);
|
||||
$options = array(
|
||||
'http' => array(
|
||||
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
|
||||
'method' => 'POST',
|
||||
'content' => http_build_query($data),
|
||||
),
|
||||
);
|
||||
$context = stream_context_create($options);
|
||||
$best2pay_id = file_get_contents($url, false, $context);
|
||||
if(intval($best2pay_id)==0)
|
||||
$button_text = $best2pay_id;
|
||||
|
||||
$signature = base64_encode(md5($sector.$best2pay_id.$password));
|
||||
$button = "<form accept-charset='utf8' action='".$best2pay_url."/webapi/Purchase' method=POST>".
|
||||
"<input type=hidden name=sector value='$sector'>".
|
||||
"<input type=hidden name=id value='$best2pay_id'>".
|
||||
"<input type=hidden name=signature value='$signature'>".
|
||||
"<input type=submit class=checkout_button value='$button_text'>".
|
||||
"</form>";
|
||||
return $button;
|
||||
}
|
||||
|
||||
}
|
||||
89
payment/Best2Pay/callback.php
Normal file
89
payment/Best2Pay/callback.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Simpla CMS
|
||||
*
|
||||
* @copyright 2013 Denis Pikusov
|
||||
* @link http://simplacms.ru
|
||||
* @author Denis Pikusov
|
||||
*
|
||||
* К этому скрипту обращается best2pay в процессе оплаты
|
||||
*
|
||||
*/
|
||||
|
||||
// Работаем в корневой директории
|
||||
chdir ('../../');
|
||||
require_once('api/Simpla.php');
|
||||
$simpla = new Simpla();
|
||||
|
||||
$xml = file_get_contents('php://input');
|
||||
$xml = simplexml_load_string($xml);
|
||||
$response = json_decode(json_encode($xml));
|
||||
|
||||
|
||||
if($response->type != 'PURCHASE' || $response->state != 'APPROVED')
|
||||
exit();
|
||||
|
||||
// Сумма, которую заплатил покупатель
|
||||
$amount = $response->amount/100;
|
||||
|
||||
// Внутренний номер покупки продавца
|
||||
// В этом поле передается id заказа в нашем магазине.
|
||||
$order_id = $response->reference;
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// Выберем заказ из базы
|
||||
////////////////////////////////////////////////
|
||||
$order = $simpla->orders->get_order(intval($order_id));
|
||||
if(empty($order))
|
||||
die('Оплачиваемый заказ не найден');
|
||||
|
||||
// Нельзя оплатить уже оплаченный заказ
|
||||
if($order->paid)
|
||||
die('Этот заказ уже оплачен');
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// Выбираем из базы соответствующий метод оплаты
|
||||
////////////////////////////////////////////////
|
||||
$method = $simpla->payment->get_payment_method(intval($order->payment_method_id));
|
||||
if(empty($method))
|
||||
die("Неизвестный метод оплаты");
|
||||
|
||||
// Проверяем контрольную подпись
|
||||
$settings = unserialize($method->settings);
|
||||
$signature = $response->signature;
|
||||
unset($response->signature);
|
||||
$str = implode('', (array)$response).$settings['password'];
|
||||
print $my_signature = base64_encode(md5($str));
|
||||
if($my_signature !== $signature)
|
||||
die("bad sign\n");
|
||||
|
||||
|
||||
$mrh_pass2 = $settings['password2'];
|
||||
|
||||
if($amount != $simpla->money->convert($order->total_price, $method->currency_id, false) || $amount<=0)
|
||||
die("incorrect price\n");
|
||||
|
||||
////////////////////////////////////
|
||||
// Проверка наличия товара
|
||||
////////////////////////////////////
|
||||
$purchases = $simpla->orders->get_purchases(array('order_id'=>intval($order->id)));
|
||||
foreach($purchases as $purchase)
|
||||
{
|
||||
$variant = $simpla->variants->get_variant(intval($purchase->variant_id));
|
||||
if(empty($variant) || (!$variant->infinity && $variant->stock < $purchase->amount))
|
||||
{
|
||||
die("Нехватка товара $purchase->product_name $purchase->variant_name");
|
||||
}
|
||||
}
|
||||
|
||||
// Установим статус оплачен
|
||||
$simpla->orders->update_order(intval($order->id), array('paid'=>1));
|
||||
|
||||
// Спишем товары
|
||||
$simpla->orders->close(intval($order->id));
|
||||
$simpla->notify->email_order_user(intval($order->id));
|
||||
$simpla->notify->email_order_admin(intval($order->id));
|
||||
|
||||
|
||||
die("ok");
|
||||
27
payment/Best2Pay/settings.xml
Normal file
27
payment/Best2Pay/settings.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module>
|
||||
<name>
|
||||
Best2Pay
|
||||
</name>
|
||||
<settings>
|
||||
<variable>sector</variable>
|
||||
<name>Sector ID</name>
|
||||
</settings>
|
||||
<settings>
|
||||
<variable>password</variable>
|
||||
<name>Пароль</name>
|
||||
</settings>
|
||||
<settings>
|
||||
<variable>mode</variable>
|
||||
<name>Режим оплаты</name>
|
||||
<options>
|
||||
<name>Тестовый режим</name>
|
||||
<value>test</value>
|
||||
</options>
|
||||
<options>
|
||||
<name>Рабочий режим</name>
|
||||
<value>0</value>
|
||||
</options>
|
||||
</settings>
|
||||
|
||||
</module>
|
||||
Reference in New Issue
Block a user