demo){ $this->apiUrl = 'https://sbx-origination.heidipay.io'; if($this->isFinanziamento){ $this->merchantKey = '329fa1a44aae7e6271d444f1de3d6bc90c86caeb'; } else { $this->merchantKey = '391df5ff3b722aa6da1df954a19725658be5c3d1'; } } switch($_GET[$this->step_var]){ default: $form = $this->Procedura->iniziato($this->nome_metodo); if(!$this->merchantKey){ $this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>"error"])); } else { $token = $this->getToken(); if($token){ $this->createPayment($token); } else { $this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>"error"])); } } // return $this->Procedura->iniziato($this->nome_metodo); // $this->Procedura->confermato($this->identificativo); // return $this->Procedura->redirect($this->Procedura->genera_link_agg(array($this->step_var=>"return_ok"))); case 'return_ok': //La pagina return_ok non viene visitata dalle app iOS (scatta il controllo onbeforeload prima della visita a differenza di Android), quindi su return_ok non va fatta *nessuna* operazione. return $this->Procedura->concluso(); break; case 'return_ko': return $this->Procedura->annulla(); break; case 's2s': if($_GET["testStatus"]){ // pending: customer is in the checkout process // cancelled: status gets to cancelled if: // a customer has abandoned the checkout (received after 4 hours); // he was inactive on the checkout for 4 hours; // he was declined (received immediately) // awaiting_confirmation: if you have opted for a non-synchronized warehouse setup (manual order confirmation), your order will be set to this status when the customer completes the checkout // success: If you would like a personalised mapping of the status please let your relationship manager know. $output = '{"status": "' . $_GET["testStatus"] . '", "token": "ric-2202"}'; } else { $output = file_get_contents("php://input"); } if($output){ $output = json_decode($output); if($output){ $status = (string) $output->status; $token = (string) $output->token; if(in_array($status, ["success", "awaiting_confirmation"])){ return $this->Procedura->confermato((string)$output->token); $this->Procedura->redirect($this->Procedura->genera_link_agg(array($this->step_var=>"return_ok"))); } else if($status == "cancelled"){ return $this->Procedura->errore(); } else if($status == "pending"){ return $this->Procedura->incerto(); } } else { trigger_error("Ricevuta risposta HeyLight con contenuto non json"); return $this->Procedura->errore(); } } else { trigger_error("Ricevuta risposta HeyLight senza contenuto"); return $this->Procedura->errore(); } // mail("a.toce@tnx.it", "Ricezione webhook heylight", print_r($output, true)); break; case 'error': // $errorMessage = $this->$_GET[$this->step_var] ? $this->$_GET[$this->step_var] : null; return $this->Procedura->errore(); } } function setValuta($currencyCode){ $this->valuta = $currencyCode; } function createPayment($token){ global $lang; $parametriRichiesta = []; $parametriRichiesta["amount_format"] = "DECIMAL"; $parametriRichiesta["amount"]["amount"] = $this->importo; $parametriRichiesta["amount"]["currency"] = "EUR"; $parametriRichiesta["redirect_urls"]["success_url"] = $this->Procedura->genera_link_agg([$this->step_var=>"return_ok"]); $parametriRichiesta["redirect_urls"]["failure_url"] = $this->Procedura->genera_link_agg([$this->step_var=>"return_ko"]); $parametriRichiesta["webhooks"]["mapping_scheme"] = "DEFAULT"; $parametriRichiesta["webhooks"]["status_url"] = $this->Procedura->genera_link_agg([$this->step_var=>"s2s"]); $parametriRichiesta["webhooks"]["token"] = $this->identificativo; $parametriRichiesta["order_reference"] = $this->identificativo; $parametriRichiesta["store_id"] = "ecommerce"; $parametriRichiesta["language"] = $lang == "ita" ? "it" : "en"; if($this->isFinanziamento) $parametriRichiesta["additional_data"]["pricing_structure_code"] = "PC6"; if($this->allowed_terms) $parametriRichiesta["allowed_terms"] = is_array($this->allowed_terms) ? $this->allowed_terms : explode(",", $this->allowed_terms); $createPaymentRequest = curl_init(); curl_setopt($createPaymentRequest, CURLOPT_URL, $this->apiUrl . "/api/checkout/v1/init/"); curl_setopt($createPaymentRequest, CURLOPT_HTTPHEADER, ["accept: application/json", "content-type: application/json", "authorization: Bearer " . $token]); curl_setopt($createPaymentRequest, CURLOPT_RETURNTRANSFER, true); curl_setopt($createPaymentRequest, CURLOPT_VERBOSE, true); curl_setopt($createPaymentRequest, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($createPaymentRequest, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($createPaymentRequest, CURLOPT_POST, 1); curl_setopt($createPaymentRequest, CURLOPT_POSTFIELDS, json_encode($parametriRichiesta)); $createPaymentResponse = curl_exec($createPaymentRequest); $createPaymentInfo = curl_getinfo($createPaymentRequest); if($createPaymentInfo["http_code"] == 201 && $createPaymentInfo["content_type"] == "application/json"){ $createPaymentResponse = json_decode($createPaymentResponse); if((string) $createPaymentResponse->action == "REDIRECT" && (string) $createPaymentResponse->redirect_url){ $this->Procedura->redirect((string) $createPaymentResponse->redirect_url); } else { trigger_error("Procedura HeyLight senza redirect?"); $this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>"error"])); } } else { $this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>"error"])); } } function getToken(){ $getTokenRequest = curl_init(); curl_setopt($getTokenRequest, CURLOPT_URL, $this->apiUrl . "/auth/generate/"); curl_setopt($getTokenRequest, CURLOPT_HTTPHEADER, ["accept: application/json", "content-type: application/json"]); curl_setopt($getTokenRequest, CURLOPT_RETURNTRANSFER, true); curl_setopt($getTokenRequest, CURLOPT_VERBOSE, true); curl_setopt($getTokenRequest, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($getTokenRequest, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($getTokenRequest, CURLOPT_POST, 1); curl_setopt($getTokenRequest, CURLOPT_POSTFIELDS, json_encode(["merchant_key" => $this->merchantKey])); $getTokenResponse = curl_exec($getTokenRequest); $getTokenInfo = curl_getinfo($getTokenRequest); if($getTokenInfo["http_code"] == 200 && $getTokenInfo["content_type"] == "application/json"){ $getTokenResponse = json_decode($getTokenResponse); if((string) $getTokenResponse->status == "success" && (string) $getTokenResponse->data->token != ""){ return (string) $getTokenResponse->data->token; } else { $this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>"error"])); } } else { trigger_error("Errore sul recupero del token HeyLight"); $this->Procedura->redirect($this->Procedura->genera_link_agg([$this->step_var=>"error"])); } } } ?>