Skip to main content

Clear Example

A clear example of how to create a payment and display a widget in PHP

$lunu_processing_version = 'alpha'; // prod server
$lunu_widget_version = 'alpha'; // prod widget


$app_id = '149ec6da-f0dc-4cdf-9fb3-8ba2dc602f60';
$api_secret = '23d93cac-000f-5000-8000-126728f15140';

$auth_token = base64_encode($app_id . ':' . $api_secret);

$order_id = '23925343';
$order_amount = '100.00';
$amount_of_shipping = '15.00';
$payment_description = 'YOUR DESCRIPTION';

$customer_email = '[email protected]';

// Idempotence-Key must be unique for each query
$idempotence_key = 'example_' . time() . '_' . $order_id;

$lunu_pay_url_create = 'https://' . $lunu_processing_version . '.lunu.io/api/v1/payments/create';

$ch = curl_init($lunu_pay_url_create);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'shop_order_id' => $order_id, // Optional field

// callback url on payment status change
'callback_url' => 'https://your.site/lunu-pay/api.php?method=notify&order_id=' . $order_id,

// Optional field for an email so that refunds to the customer are available
'email' => $customer_email,

'amount' => $order_amount,
'amount_of_shipping' => $amount_of_shipping, // Optional field

'description' => $payment_description,
'expires' => date("c", time() + 3600) // expires through one minute

)));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic ' . $auth_token,
'Idempotence-Key: ' . $idempotence_key,
'Content-Type: application/json'
));

$responseBody = curl_exec($ch);
$responseHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


if ($responseHttpCode !== 200) {
echo "Response code is invalid<br/>";
var_dump(array(
'url' => $lunu_pay_url_create,
'code' => $responseHttpCode,
'body' => $responseBody
));
exit;
}

$data = json_decode($responseBody, true);

if (!is_array($data)) {
echo "Response body is invalid<br/>";
var_dump(array(
'url' => $lunu_pay_url_create,
'code' => $responseHttpCode,
'body' => $responseBody
));
exit;
}

if (is_array($data['error'])) {
echo "Processing error:<br/>";
var_dump(array(
'url' => $lunu_pay_url_create,
'code' => $responseHttpCode,
'body' => $responseBody,
'error' => $data['error'],
));
exit;
}

if (!is_array($data['response'])) {
echo "Response is empty<br/>";
var_dump(array(
'url' => $lunu_pay_url_create,
'code' => $responseHttpCode,
'body' => $responseBody
));
exit;
}

$response = $data['response'];

$confirmation_token = $response['confirmation_token'];

if (empty($confirmation_token)) {
echo "confirmation_token is empty<br/>";
var_dump(array(
'url' => $lunu_pay_url_create,
'code' => $responseHttpCode,
'body' => $responseBody
));
exit;
}


$success_url = 'https://your.site/thanks';
$cancel_url = 'https://your.site/order-cancel';

echo "
<!--HTML element that will display the payment form-->
<div id=\"payment-form\"></div><br><br>
<script>
(function(d, t) {
var n = d.getElementsByTagName(t)[0], s = d.createElement(t);
s.type = 'text/javascript';
s.charset = 'utf-8';
s.async = true;
s.src = 'https://plugins.lunu.io/packages/widget-ui/" . $lunu_widget_version . ".js?t=' + 1 * new Date();
s.onload = function() {
new window.Lunu.widgets.Payment(
d.getElementById('payment-form'),
{
overlay: true,
confirmation_token: '" . $confirmation_token . "',
// Token that must be received from the Processing Service before making a payment
// Required parameter

callbacks: {
init_error: function(error) {
// Handling initialization errors
},
init_success: function() {
// Handling a Successful Initialization
},
payment_paid: function(params) {
// Handling a successful payment event
var handleSuccess = window.LUNU_PAYMENT_SUCCESS_CALLBACK;
handleSuccess && handleSuccess(params);
" . (empty($success_url) ? "" : "window.location.href = '" . $success_url . "';") . "
},
payment_cancel: function() {
// Handling a payment cancellation event
var handleCancel = window.LUNU_PAYMENT_CANCEL_CALLBACK;
handleCancel && handleCancel();
" . (empty($cancel_url) ? "" : "window.location.href = '" . $cancel_url . "';") . "
},
payment_close: function() {
// Handling the event of closing the widget window
}
}
}
);
};
n.parentNode.insertBefore(s, n);
})(document, 'script');
</script>";

/*
header('Location: ' . 'https://widget.lunu.io/' . $lunu_widget_version . '/#/?' . http_build_query(array(
'action' => 'select',
'token' => $confirmation_token,
'success' => $success_url,
'cancel' => $cancel_url,
'enableLunuGift' => $lunu_widget_version === 'beta'
)));
*/