meraproject/module/integration/dolyami/model.php

137 lines
4.1 KiB
PHP
Raw Permalink Normal View History

<?
namespace integration;
use common\payment\structure\paymentLink;
use shop\Cart;
use shop\cart\Item as CartItem;
use shop\Item;
use shop\Order;
class Dolyami {
var $login = '';
var $pass = '';
var $sslcert;
var $sslkey;
public function __construct($login,$pass,$sslcert,$sslkey) {
$this->login = $login;
$this->pass = $pass;
$this->sslcert = $sslcert;
$this->sslkey = $sslkey;
}
public function commit($order_id){
$order = Order::getByID($order_id);
$uuid = paymentLink::getUUID($order_id);
echo '<pre>';
echo var_dump($uuid);
echo '</pre>';
$headers = [
'X-Correlation-ID: '.$uuid,
'Authorization: Basic '.base64_encode($this->login.':'.$this->pass),
'Content-Type: application/json',
];
$CART = Cart::getData($order[Order::$CART]);
foreach ($CART['citems'] as $v) {
$item = $CART['items'][$v[CartItem::$ITEM]];
$count = $v[CartItem::$COUNT];
$sum = $count * $v[CartItem::$PRICE];
$items[] = [
"name" => Item::getName($item),
"quantity" => $count,
"price" => floatval($v[CartItem::$PRICE]),
];
}
if($order[Order::$DELIVERY_PRICE]){
$items[] = [
"name" => "Доставка",
"quantity" => 1,
"price" => floatval($order[Order::$DELIVERY_PRICE]),
];
}
$ORDER_TOTAL = $CART['total']+$order[Order::$DELIVERY_PRICE]-$order[Order::$TOTAL_PAID];
$data = [
'orderId'=>$order_id,
'amount'=>cfloatval($ORDER_TOTAL),
'items'=>$items,
'prepaid_amount'=>cfloatval($order[Order::$TOTAL_PAID] + $CART['cart'][Cart::$COUPON_VALUE]),
];
$url = 'https://partner.dolyame.ru/v1/orders/'.$order_id.'/commit';
if ($curl = curl_init()){
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_POST,1);
curl_setopt($curl, CURLOPT_SSLCERT, $this->sslcert);
curl_setopt($curl, CURLOPT_SSLKEY, $this->sslkey);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_POSTFIELDS,je($data));
$out = curl_exec($curl);
curl_close($curl);
$res = json_decode($out,true);
echo $out;
echo '<pre>';
echo var_dump($res);
echo '</pre>';
}
return $res['link'];
}
public function refund($order_id){
$uuid = paymentLink::getUUID($order_id);
$headers = [
'X-Correlation-ID: '.$uuid,
'Authorization: Basic '.base64_encode($this->login.':'.$this->pass),
'Content-Type: application/json',
];
$items = [
[
'name'=>'1 сентября',
'quantity'=>1,
'price'=>100,
]
];
$data = [
'orderId'=>$order_id,
'amount'=>100,
'returned_items'=>$items,
];
$url = 'https://partner.dolyame.ru/v1/orders/'.$order_id.'/refund';
if ($curl = curl_init()){
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_POST,1);
curl_setopt($curl, CURLOPT_SSLCERT, $this->sslcert);
curl_setopt($curl, CURLOPT_SSLKEY, $this->sslkey);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_POSTFIELDS,je($data));
$out = curl_exec($curl);
curl_close($curl);
$res = json_decode($out,true);
echo $out;
echo '<pre>';
echo var_dump($res);
echo '</pre>';
}
return $res['link'];
}
}