function build_response($app, $account_info, $operation_history, $request_payment, $process_payment, $home) { if (count($operation_history->operations) < 3) { $operation_history_info = sprintf("You have less then 3 records in your payment history"); } else { $operation_history_info = sprintf("The last 3 payment titles are: %s, %s, %s", $operation_history->operations[0]->title, $operation_history->operations[1]->title, $operation_history->operations[2]->title); } if ($request_payment->status == "success") { $request_payment_info = "Response of request-payment is successive."; $is_process_error = false; } else { $request_payment_info = "Response of request-payment is errorneous." . sprintf(" The error label is %s", $request_payment->error); $is_process_error = true; } if ($is_process_error) { $process_payment_info = "The request-payment returns error. No operation."; } else { $process_payment_info = sprintf("You send %g to %s wallet", $process_payment->credit_amount, $process_payment->payee); } $template_meta = function ($method, $index) { $method['includes'] = array(array("is_collapsed" => false, "title" => "Source code", "id" => $index, "body" => $method['code']), array("is_collapsed" => true, "title" => "Response", "id" => $index + 100, "body" => json_encode($method['response'], JSON_OPTIONS))); return $method; }; $methods = array(array("info" => sprintf("You wallet balance is %s RUB", $account_info->balance), "code" => read_sample("account_info.txt"), "name" => "Account-info", "response" => $account_info), array("info" => $operation_history_info, "code" => read_sample("operation_history.txt"), "name" => "Operation-history", "response" => $operation_history), array("info" => $request_payment_info, "code" => read_sample("request_payment.txt"), "name" => "Request-payment", "response" => $request_payment), array("info" => $process_payment_info, "code" => read_sample("process_payment.txt"), "name" => "Process-payment", "response" => $process_payment, "is_error" => $is_process_error, "message" => "Call process_payment method isn't possible." . " See request_payment JSON for information")); return $app->render("auth.html", array("methods" => array_map($template_meta, $methods, array_keys($methods)), "home" => substr_replace($home, "", -1), "lang" => "PHP")); }
$url = sprintf("%s?%s", $process_result->acs_uri, http_build_query($process_result->acs_params)); $app->redirect($url); }); $app->get("/wallet/external-success/", function () use($app) { $request_id = $app->getCookie("request_id"); $instance_id = $app->getCookie("instance_id"); if (is_null($request_id) || is_null($instance_id)) { $params = array("text" => "cookie is expired or incorrect", "home" => "../"); return show_error($params, $app); } $api = new ExternalPayment($instance_id); $base_path = "http://" . $app->request->getHostWithPort() . $app->request->getPath() . ".."; do { $result = $api->process(array("request_id" => $request_id, "ext_auth_success_uri" => $base_path . "/external-success/", "ext_auth_fail_uri" => $base_path . "/../external-fail/")); if ($result->status == "in_progress") { sleep(1); } } while ($result->status == "in_progress"); $get_cookie_json = function ($cookie_name) use($app) { return json_decode($app->getCookie($cookie_name)); }; $codesamples_base = "external_payment/wallet/"; return $app->render("cards.html", array("payment_result" => $result, "panels" => array("instance_id" => template_meta(array("code" => read_sample($codesamples_base . "obtain_instance_id.txt"), "response" => $get_cookie_json("result/instance_id")), 1), "request_payment" => template_meta(array("code" => read_sample($codesamples_base . "request_payment.txt"), "response" => $get_cookie_json("result/request")), 2), "process_payment1" => template_meta(array("code" => read_sample($codesamples_base . "process_payment1.txt"), "response" => $get_cookie_json("result/process")), 3), "process_payment2" => template_meta(array("code" => read_sample($codesamples_base . "process_payment2.txt"), "response" => $result), 4)), "home" => "../../", "lang" => "PHP")); }); $app->get("/wallet/external-fail/", function () use($app) { $params = array("text" => "Check out GET params for additional information", "home" => "../../"); return show_error($params, $app); }); $app->get("/wallet/", function () use($app) { $app->redirect("../"); });