СберБанк Экваринг подключение через API - Web-Global

СберБанк Экваринг подключение через API

17.06.2021 413
Поделиться:

Самый популярный вопрос, как оплатить через СберБанк на сваём сайте, или запрос API СберБанк. Я долго искал как это сделать вот и нашёл ответ.

Это фронт index.html

<!doctype html>
<html lang=»en»>
<head>
<meta charset=»UTF-8″>
<meta name=»viewport»
content=»width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0″>
<meta http-equiv=»X-UA-Compatible» content=»ie=edge»>
<script src = «https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js»></script>
<title>Document</title>
</head>

<body>

<input type=»text» name=»amount» class=»amount»>
<button class=»btnsber»>Оплатить</button>

<script>
$(document).ready(function() {
$(«.btnsber»).on(«click», function () {
console.log(‘click working’);
var amountValue = $(‘input.amount’).val();

$.ajax({
type: «POST»,
url: «https://domen/sperpay.php»,
data: {amount: amountValue},
success:function (msg){
location.href = (msg);
},
error: function(responce) {
console.log(responce);
}
});
console.log(‘end script’);

})
})

$(document).ready(function(){
$(‘.btnsber’).on(‘click’, function(){
var url = window.location.href;
location.href = (msg);
});
});

</script>
</body>
</html>

sperpay.php

<?php
$amount = $_POST[‘amount’] * 100;
$returnUrl = «https://domen»; куда возврашает после оплаты

$username = «логин»; от Сбербанка
$pass = «пароль»; от Сбербанка

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => ‘https://securepayments.sberbank.ru/payment/rest/register.do’,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => »,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => ‘POST’,
CURLOPT_POSTFIELDS => ‘userName=’.$username.’&password=’.$pass.’&amount=’.$amount.’&returnUrl=’.$returnUrl.»,
CURLOPT_HTTPHEADER => array(
‘Content-Type: application/x-www-form-urlencoded’
),
));

$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, JSON_OBJECT_AS_ARRAY);
echo » . $response[‘formUrl’] . »;
?>