baseUrl=rtrim( trim($this->baseUrl), '/' ); if( !str_starts_with( $this->baseUrl, 'https://' ) ){ throw new InvalidArgumentException( 'GSM Pay base URL must use HTTPS.' ); } if(trim($this->apiKey)===''){ throw new InvalidArgumentException( 'A server-side API key is required.' ); } } public function createInvoice( string $amount, string $currency, string $customerName='', string $customerEmail='', string $merchantReference='', string $callbackUrl='', ?string $idempotencyKey=null ):array{ $idempotencyKey= $idempotencyKey ?? bin2hex( random_bytes(16) ); return $this->request( 'POST', '/api/payment.php', [ 'amount'=>$amount, 'currency'=>$currency, 'customer_name'=>$customerName, 'cus_email'=>$customerEmail, 'additional_id'=>$merchantReference, 'callback_url'=>$callbackUrl, ], [ 'X-Idempotency-Key: '.$idempotencyKey, ] ); } public function invoiceStatus( string $invoiceId ):array{ $invoiceId=trim($invoiceId); if($invoiceId===''){ throw new InvalidArgumentException( 'Invoice ID is required.' ); } return $this->request( 'GET', '/api/ascertain.php?invoice_id=' .rawurlencode($invoiceId) ); } public static function verifyCallback( string $rawBody, string $timestamp, string $signature, string $callbackKey ):bool{ if( $timestamp==='' || $signature==='' || $callbackKey==='' ){ return false; } $expected=hash_hmac( 'sha256', $timestamp.'.'.$rawBody, $callbackKey ); return hash_equals( $expected, strtolower($signature) ); } private function request( string $method, string $path, ?array $json=null, array $extraHeaders=[] ):array{ if(!function_exists('curl_init')){ throw new RuntimeException( 'PHP cURL extension is required.' ); } $headers=[ 'Accept: application/json', 'X-API-KEY: '.$this->apiKey, ]; $body=null; if($json!==null){ $body=json_encode( $json, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR ); $headers[]='Content-Type: application/json'; } foreach($extraHeaders as $header){ $headers[]=$header; } $handle=curl_init( $this->baseUrl.$path ); if($handle===false){ throw new RuntimeException( 'Unable to initialize HTTP client.' ); } curl_setopt_array( $handle, [ CURLOPT_CUSTOMREQUEST=>$method, CURLOPT_HTTPHEADER=>$headers, CURLOPT_RETURNTRANSFER=>true, CURLOPT_FOLLOWLOCATION=>false, CURLOPT_CONNECTTIMEOUT=>10, CURLOPT_TIMEOUT=>30, CURLOPT_SSL_VERIFYPEER=>true, CURLOPT_SSL_VERIFYHOST=>2, ] ); if($body!==null){ curl_setopt( $handle, CURLOPT_POSTFIELDS, $body ); } $response=curl_exec($handle); if($response===false){ $message=curl_error($handle); curl_close($handle); throw new RuntimeException( 'GSM Pay request failed: ' .$message ); } $status=(int)curl_getinfo( $handle, CURLINFO_RESPONSE_CODE ); curl_close($handle); $decoded=json_decode( $response, true ); if( $status<200 || $status>=300 ){ $message= is_array($decoded) && is_string( $decoded['error'] ?? null ) ? $decoded['error'] : 'HTTP '.$status; throw new RuntimeException( 'GSM Pay rejected the request: ' .$message ); } if(!is_array($decoded)){ throw new RuntimeException( 'GSM Pay returned invalid JSON.' ); } return $decoded; } }