liqpay

Для приема платежей через liqpay нужно зарегистрироваться и получить merchant_id и merchant_signature, которые далее будут использованы для подписи запросов и проверки ответов.

Собственно пример отправки на liqpay:

<?php
require_once 'config.php';

$xml = '<request>
	<version>1.2</version>
	<merchant_id>' . MERCHANT_ID . '</merchant_id>
	<result_url>http://example.com/process.php</result_url>
	<server_url>http://example.com/process.php</server_url>
	<order_id>' . time() . '</order_id>
	<amount>0.03</amount>
	<currency>UAH</currency>
	<description>test</description>
	<default_phone></default_phone>
	<pay_way></pay_way>
	<goods_id>1234</goods_id>
</request>';

$sign = base64_encode(sha1(MERCHANT_SIGNATURE . $xml . MERCHANT_SIGNATURE, true));
$xml_encoded = base64_encode($xml);
?><!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>liqpay</title>
</head>
<body>
<form action="https://www.liqpay.com/?do=clickNbuy" method="POST">
	<input type="hidden" name="operation_xml" value="<?= $xml_encoded ?>">
	<input type="hidden" name="signature" value="<?= $sign ?>">
	<input type="submit" value="Submit"/>
</form>
</body>
</html>

Информацию по полям xml можно подсмотреть в их доке https://liqpay.com/?do=pages&p=cnb12

result_url - страница куда будет перенаправлен пользователь после проплаты в liqpay.

server_url - url который будет дергать liqpay после проплаты.

Их можно указать одинаковыми - соотв. там же и проверяем и пишем в базу и показываем пользователю сообщение о том что все прошло успешно.

Обработка ответа liqpay:

<?php
require_once 'config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
	$xml_decoded = base64_decode($_POST['operation_xml']);
	$xml = new SimpleXMLElement($xml_decoded);
	$signature = base64_encode(sha1(MERCHANT_SIGNATURE . $xml_decoded . MERCHANT_SIGNATURE, true));

	if ($_POST['signature'] == $signature) {

		// success, failure, wait_secure
		if ($xml->status == 'success') {
			echo '<h1>Success</h1>';
		}

	} else {
		echo '<h1>Wrong Signature</h1>';
	}

} else {
	echo '<h1>Wrong Method</h1>';
}

Вот дамп того что происходит на этой страничке:

Success

$_POST
------

Array
(
	[operation_xml] => PHJlc3BvbnNlPgogIDxhY3Rpb24+cmVzdWx0X3VybDwvYWN0aW9uPgogIDxhbW91bnQ+MC4wMzwv
YW1vdW50PgogIDxjdXJyZW5jeT5VQUg8L2N1cnJlbmN5PgogIDxkZXNjcmlwdGlvbj50ZXN0PC9k
ZXNjcmlwdGlvbj4KICA8Z29vZHNfaWQ+MTIzNDwvZ29vZHNfaWQ+CiAgPG1lcmNoYW50X2lkPmk1
MTg1NTQ5NDUxPC9tZXJjaGFudF9pZD4KICA8b3kZXJfaWQ+MTM4ODI3MDM2NTwvb3JkZXJfaWQ+
CiAgPHBheV93YXk+bGlxcGF5PC9wYXlfd2F5PgogIDxwYXlzX2NvdW50Pjg8L3BheXNfY291bnQ+
CiAgPHNlbmRlcl9waG9uZT4rMzgwOTQ5MjU0ODg1PC9zZW5kZXJfcGhvbmU+CiAgPHN0YXR1cz5z
dWNjZXNzPC9zdGF0dXM+CiAgPHRyYW5zYWN0aW9uX2lkPjMzMDk3NzQyPC90cmFuc2FjdGlvbl9p
ZD4KICA8dmVyc2lvbj4xLjI8L3ZlcnNpb24+CjwvcmVzcG9uc2U+Cg==

	[signature] => 3PT3CmSld9MLD8qlmZtUZg8V28=
)


$xml_decoded
------------

<response>
	<action>result_url</action>
	<amount>0.03</amount>
	<currency>UAH</currency>
	<description>test</description>
	<goods_id>1234</goods_id>
	<merchant_id>i3000545000</merchant_id>
	<order_id>1388270365</order_id>
	<pay_way>liqpay</pay_way>
	<pays_count>8</pays_count>
	<sender_phone>+380949250000</sender_phone>
	<status>success</status>
	<transaction_id>33097000</transaction_id>
	<version>1.2</version>
</response>


$xml
----

SimpleXMLElement Object
(
	[action] => result_url
	[amount] => 0.03
	[currency] => UAH
	[description] => test
	[goods_id] => 1234
	[merchant_id] => i3000545000
	[order_id] => 1388270365
	[pay_way] => liqpay
	[pays_count] => 8
	[sender_phone] => +380949250000
	[status] => success
	[transaction_id] => 33097000
	[version] => 1.2
)


$signature
----------

3PT3CmSld9MLD8qlmZtUZg8V28=

И еще один пример, проверки проплаты по id транзакции или заказа:

<?php
require_once 'config.php';

$request_xml = new SimpleXMLElement('<request></request>');
$request_xml->addChild('version', 1.2);
$request_xml->addChild('action', 'view_transaction');
$request_xml->addChild('merchant_id', MERCHANT_ID);
$request_xml->addChild('transaction_id' /*, 33096903*/);
$request_xml->addChild('transaction_order_id', 1388267638);

$request_xml = $request_xml->asXML();

$operation_xml = base64_encode($request_xml);
$signature = base64_encode(sha1(MERCHANT_SIGNATURE . $request_xml . MERCHANT_SIGNATURE, true));

$request_xml = new SimpleXMLElement('<request></request>');
$operation_envelope = $request_xml->addChild('liqpay')->addChild('operation_envelope');
$operation_envelope->addChild('operation_xml', $operation_xml);
$operation_envelope->addChild('signature', $signature);

$ch = curl_init('https://www.liqpay.com/?do=api_xml');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_xml->asXML());
$result = curl_exec($ch);
curl_close($ch);

$response_xml = new SimpleXMLElement($result);

$xml_decoded = base64_decode($response_xml->liqpay->operation_envelope->operation_xml);
$xml = new SimpleXMLElement($xml_decoded);
$signature = base64_encode(sha1(MERCHANT_SIGNATURE . $xml_decoded . MERCHANT_SIGNATURE, true));

if ($response_xml->liqpay->operation_envelope->signature == $signature) {
	echo '<pre>' . print_r($xml, true) . '</pre>';
}

Ответ будет приблизительно таким:

SimpleXMLElement Object
(
	[action] => view_transaction
	[merchant_id] => i3000545000
	[status] => success
	[transaction] => SimpleXMLElement Object
		(
			[id] => 33096000
			[amount] => 0.03
			[currency] => UAH
			[description] => test
			[from] => +380949000000
			[order_id] => 1388000638
			[pay_way] => liqpay
			[referer_url] => http://example.com/
			[status] => success
			[to] => +8200027866718
		)

	[transaction_id] => SimpleXMLElement Object
		(
		)

	[transaction_order_id] => 1388267000
	[version] => 1.2
)