/** * Creates JSON data for payment/init including signature * @param merchantId ID of merchant assigned by bank * @param orderNo reference order number * @param dttm date and time ot request sending, format YYYYMMDDHHMMSS * @param totalAmount total order amount * @param returnUrl return URL address for back redirecting from payment gateway to e-shop * @param cart cart structured data * @param description brief payment description * @param customerId customer ID * @param privateKey merchant private key for data signing * @param privateKeyPassword merchant private key password * @param closePayment close payment flag (0/1) * @param merchantData merchant base64 encoded data * @param returnMethodPOST indicator if return method POST should be used */ function createPaymentInitData($merchantId, $orderNo, $dttm, $totalAmount, $returnUrl, $cart, $description, $customerId, $privateKey, $privateKeyPassword, $closePayment, $merchantData, $returnMethodPOST) { $payOperation = Constants::$PAYOPERATION; $payMethod = Constants::$PAYMETHOD; $currency = Constants::$CURRENCY; $returnMethod = $returnMethodPOST == 'yes' ? Constants::$POST_RETURNMETHOD : Constants::$GET_RETURNMETHOD; $closePayment = $closePayment == '1' ? "true" : "false"; $totalAmount = $totalAmount * 100; $data = array("merchantId" => $merchantId, "orderNo" => $orderNo, "dttm" => $dttm, "payOperation" => $payOperation, "payMethod" => $payMethod, "totalAmount" => $totalAmount, "currency" => $currency, "closePayment" => $closePayment, "returnUrl" => $returnUrl, "returnMethod" => $returnMethod, "cart" => $cart, "description" => $description, "merchantData" => $merchantData); if (!is_null($customerId) && $customerId != '0') { $data["customerId"] = $customerId; } $data["language"] = Constants::$LANGUAGE; $data["signature"] = signPaymentInitData($data, $privateKey, $privateKeyPassword); return $data; }
/** * Creates JSON data for payment/init including signature * @param merchantId ID of merchant assigned by bank * @param orderNo reference order number * @param dttm date and time ot request sending, format YYYYMMDDHHMMSS * @param totalAmount total order amount * @param returnUrl return URL address for back redirecting from payment gateway to e-shop * @param cart cart structured data * @param description brief payment description * @param customerId customer ID * @param privateKey merchant private key for data signing * @param privateKeyPassword merchant private key password * @param closePayment close payment flag (0/1) * @param merchantData merchant base64 encoded data * @param returnMethodPOST indicator if return method POST should be used * @param currency currency in which the order is being made * @param language language of the payment interface */ function createPaymentInitData($merchantId, $orderNo, $dttm, $totalAmount, $returnUrl, $cart, $description, $customerId, $privateKey, $privateKeyPassword, $closePayment, $merchantData, $returnMethodPOST, $currency = "CZK", $language = "CS") { $payOperation = Constants::$PAYOPERATION; $payMethod = Constants::$PAYMETHOD; $returnMethod = $returnMethodPOST == 'yes' ? Constants::$POST_RETURNMETHOD : Constants::$GET_RETURNMETHOD; $closePayment = $closePayment == '1' ? "true" : "false"; $totalAmount = $totalAmount * 100; $titles = $cart[0]['description']; $shippingTotal = $cart[1]['amount']; if ($language == 'CS') { // ČSOB chybně používá kódy zemí místo kódu jazyka (ISO 639-1) $language = 'CZ'; } if (!in_array($language, Constants::$SUPPORTED_LANGUAGES)) { $language = 'EN'; } $data = array("merchantId" => $merchantId, "orderNo" => $orderNo, "dttm" => $dttm, "payOperation" => $payOperation, "payMethod" => $payMethod, "totalAmount" => $totalAmount, "currency" => $currency, "closePayment" => $closePayment, "returnUrl" => $returnUrl, "returnMethod" => $returnMethod, "cart" => $cart, "description" => $description, "merchantData" => $merchantData, "language" => $language); if (!is_null($customerId) && $customerId != '0') { $data["customerId"] = $customerId; } $data["signature"] = signPaymentInitData($data, $privateKey, $privateKeyPassword); return $data; }
require_once 'logger.php'; require_once 'crypto.php'; require_once 'setup.php'; echo "preparing payment init data ...\n\n"; $initdata = $_POST['initdata']; $data = json_decode($initdata, true); if (is_null($data)) { echo 'parsing JSON failed, please go back, update and try again'; return; } echo "parsed payment/init request:\n"; echo htmlspecialchars(json_encode($data, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE)) . "\n\n"; $dttm = (new DateTime())->format("YmdHis"); $data["dttm"] = $dttm; $data["closePayment"] = $data["closePayment"] == '1' ? "true" : "false"; $data["signature"] = signPaymentInitData($data, $privateKey, $privateKeyPassword); echo "prepared payment/init request:\n"; echo htmlspecialchars(json_encode($data, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE)) . "\n\n"; echo "processing payment/init request ...\n\n"; $ch = curl_init($url . NativeApiMethod::$init); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json;charset=UTF-8')); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'payment/init failed, reason: ' . htmlspecialchars(curl_error($ch)); return; } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);