Last active
August 29, 2015 14:05
-
-
Save sdfsdhgjkbmnmxc/4a5f5396b0ac6832e5c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Работаем в корневой директории | |
chdir ('../../'); | |
require_once('payment/Futubank/futubank_core.php'); | |
require_once('api/Simpla.php'); | |
class SimplaCallbackHandler extends AbstractFutubankCallbackHandler { | |
private $simpla; | |
private $method; | |
private $futubank_form; | |
function __construct() { | |
$this->simpla = new Simpla(); | |
// вот и потекла абстракция: никто не ожидал, что для того, чтобы достать | |
// настройки плагина, нужен id заказа. МОжет как-то их можно ещё хранить? | |
$order = $this->load_order(intval($_POST['order_id'])); | |
if (!$order) die('Оплачиваемый заказ не найден'); | |
$this->method = $this->simpla->payment->get_payment_method(intval($order->payment_method_id)); | |
$settings = unserialize($this->method->settings); | |
// FIXME: нехорошо хардкодить, хорошо тоже из настроек брать | |
$is_test = 1; | |
$this->futubank_form = new FutubankForm( | |
$settings['merchant_id'], | |
$settings['secret_key'], | |
$is_test | |
); | |
} | |
protected function get_futubank_form() { | |
return $this->futubank_form; | |
} | |
protected function load_order($order_id) { | |
return $this->simpla->orders->get_order($order_id); | |
} | |
protected function get_order_currency($order) { | |
// FIXME | |
return 'RUB'; | |
} | |
protected function get_order_amount($order) { | |
return $this->simpla->money->convert($order->total_price, $this->method->currency_id, false); | |
} | |
protected function is_order_completed($order) { | |
return $order->payd; | |
} | |
protected function mark_order_as_completed($order, array $data) { | |
//////////////////////////////////// | |
// Проверка наличия товара | |
//////////////////////////////////// | |
$purchases = $this->simpla->orders->get_purchases(array('order_id'=>intval($order->id))); | |
foreach($purchases as $purchase) { | |
$variant = $this->simpla->variants->get_variant(intval($purchase->variant_id)); | |
if(empty($variant) || (!$variant->infinity && $variant->stock < $purchase->amount)) { | |
die("Нехватка товара $purchase->product_name $purchase->variant_name"); | |
} | |
} | |
// Установим статус оплачен | |
$this->simpla->orders->update_order(intval($order->id), array('paid' => 1)); | |
// Спишем товары | |
$this->simpla->orders->close(intval($order->id)); | |
$this->simpla->notify->email_order_user(intval($order->id)); | |
$this->simpla->notify->email_order_admin(intval($order->id)); | |
} | |
protected function mark_order_as_error($order, array $data) { | |
// FIXME | |
return false; | |
} | |
} | |
$cb = new SimplaCallbackHandler(); | |
$cb->show($_POST); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment