203 lines
5.9 KiB
PHP
203 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace integration;
|
|
use shop\Cart;
|
|
use shop\cart\Item as CartItem;
|
|
use shop\coupon\structure\eShopCouponType;
|
|
use shop\Item;
|
|
use shop\Order;
|
|
use YooKassa\Model\PaymentMethod\PaymentMethodBankCard;
|
|
use YooKassa\Client as YooKassaClient;
|
|
use YooKassa\Model\Confirmation\ConfirmationRedirect;
|
|
use YooKassa\Model\Payment;
|
|
use YooKassa\Helpers\Random;
|
|
use YooKassa\Model\ConfirmationAttributes;
|
|
use YooKassa\Request\Payments\CreatePaymentRequest;
|
|
|
|
|
|
|
|
class YooKassa {
|
|
var $shopId;
|
|
var $token;
|
|
public function __construct($shopId,$token) {
|
|
$this->shopId = $shopId;
|
|
$this->token = $token;
|
|
}
|
|
|
|
public function getPaymentLink($order_id,$payment_id,$return_url){
|
|
$payment = self::getPaymentInfo($order_id,$payment_id,$return_url);
|
|
$res = $payment->getConfirmation()->getConfirmationUrl();
|
|
return $res;
|
|
}
|
|
|
|
public function getPaymentInfo($order_id,$payment_id,$return_url){
|
|
$C = Order::getByID($order_id);
|
|
$amount = $C[Order::$TOTAL] - $C[Order::$TOTAL_PAID] ;
|
|
|
|
$client = new YooKassaClient();
|
|
$client->setAuth($this->shopId,$this->token);
|
|
$data = array(
|
|
'amount' => array(
|
|
'value' => floatval($amount),
|
|
'currency' => 'RUB',
|
|
),
|
|
'confirmation' => array(
|
|
'type' => 'redirect',
|
|
'return_url' => $return_url,
|
|
),
|
|
'metadata'=>[
|
|
'order_id'=>$order_id,
|
|
'pay_id'=>$payment_id,
|
|
],
|
|
'capture' => true,
|
|
'description' => 'Заказ №'.$order_id,
|
|
'receipt'=>$this->getReceipt($C),
|
|
);
|
|
|
|
|
|
if($_GET['test']) {
|
|
echo '<pre>';
|
|
echo var_dump($amount);
|
|
echo '</pre>';
|
|
echo '<pre>';
|
|
echo var_dump($C[Order::$TOTAL], $C[Order::$TOTAL_PAID], $C[Order::$CASHBACK_TOTAL]);
|
|
echo '</pre>';
|
|
}
|
|
//$data['payment_method_data']['type'] = 'bank_card';
|
|
//$data['payment_method_data']['type'] = 'apple_pay';
|
|
//$data['payment_method_data']['type'] = 'google_pay';
|
|
//$data['payment_method_data']['payment_method_token'] = 'google_pay';
|
|
$payment = $client->createPayment( $data, uniqid('', true) );
|
|
|
|
return $payment;
|
|
}
|
|
|
|
|
|
public function getReceipt($order){
|
|
|
|
$CART = Cart::getData($order[Order::$CART]);
|
|
|
|
$cashback = -$order[Order::$CASHBACK_TOTAL];
|
|
|
|
|
|
|
|
$items = [];
|
|
foreach ($CART['citems'] as $v) {
|
|
$item = $CART['items'][$v[CartItem::$ITEM]];
|
|
$count = $v[CartItem::$COUNT];
|
|
$sum = $count * $v[CartItem::$PRICE];
|
|
|
|
switch ($CART['cart'][Cart::$COUPON_TYPE]) {
|
|
case eShopCouponType::VALUE:
|
|
$sum -= $CART['cart'][Cart::$COUPON_VALUE];
|
|
break;
|
|
case eShopCouponType::PERCENT:
|
|
default:
|
|
$sum *= (100-cfloatval($CART['cart'][Cart::$COUPON_VALUE]))/100;
|
|
break;
|
|
}
|
|
|
|
$items[] = [
|
|
"quantity"=>$count,
|
|
"amount"=>[
|
|
'value'=>floatval($sum),
|
|
'currency'=>'RUB',
|
|
],
|
|
"vat_code"=>1, // https://yookassa.ru/developers/payment-acceptance/receipts/54fz/other-services/parameters-values
|
|
"description"=>Item::getName($item),
|
|
];
|
|
|
|
}
|
|
if($order[Order::$DELIVERY_PRICE]){
|
|
$items[] = [
|
|
"quantity"=>1,
|
|
"amount"=>[
|
|
'value'=>floatval($order[Order::$DELIVERY_PRICE]),
|
|
'currency'=>'RUB',
|
|
],
|
|
"vat_code"=>1,
|
|
"description"=>"Доставка",
|
|
];
|
|
}
|
|
foreach ($items as &$v){
|
|
if($cashback<=0) break;
|
|
$c = $v['quantity'];
|
|
$p = $v['amount']['value'];
|
|
$t = $c*$p;
|
|
$newTotal = $t-$cashback;
|
|
|
|
if($newTotal<$c) {
|
|
$newTotal = $c;
|
|
}
|
|
|
|
$newPrice = round($newTotal/$c);
|
|
$total = $newPrice*$c;
|
|
|
|
$diff = $t-$total;
|
|
|
|
$cashback-=$diff;
|
|
|
|
$v['amount']['value'] = $newPrice;
|
|
|
|
}
|
|
unset($v);
|
|
|
|
if($_GET['test']){
|
|
echo '<pre>';
|
|
echo var_dump($items);
|
|
echo '</pre>';
|
|
}
|
|
|
|
return [
|
|
'customer'=>[
|
|
'phone'=>$order[Order::$PHONE],
|
|
'email'=>$order[Order::$EMAIL],
|
|
],
|
|
"taxSystem"=>1,
|
|
"items"=>$items
|
|
];
|
|
}
|
|
|
|
public function getPaymentInfoEmbed($order_id,$payment_id,$return_url){
|
|
$order = $C = Order::getByID($order_id);
|
|
$amount = $C[Order::$TOTAL] - $C[Order::$TOTAL_PAID] - $C[Order::$CASHBACK_TOTAL];
|
|
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
$client = new YooKassaClient();
|
|
$client->setAuth($this->shopId,$this->token);
|
|
$data = array(
|
|
'amount' => array(
|
|
'value' => floatval($amount),
|
|
'currency' => 'RUB',
|
|
),
|
|
'confirmation' => array(
|
|
'type' => 'embedded'
|
|
),
|
|
'metadata'=>[
|
|
'order_id'=>$order_id,
|
|
'pay_id'=>$payment_id,
|
|
],
|
|
'capture' => true,
|
|
'description' => 'Заказ №'.$order_id,
|
|
'receipt'=>$this->getReceipt($order),
|
|
);
|
|
|
|
if($_GET['test']) {
|
|
echo '<pre>';
|
|
echo var_dump($data);
|
|
echo '</pre>';
|
|
}
|
|
//$data['payment_method_data']['type'] = 'bank_card';
|
|
//$data['payment_method_data']['type'] = 'apple_pay';
|
|
//$data['payment_method_data']['type'] = 'google_pay';
|
|
//$data['payment_method_data']['payment_method_token'] = 'google_pay';
|
|
$payment = $client->createPayment( $data, uniqid('', true) );
|
|
|
|
return $payment;
|
|
}
|
|
}
|