/** * Exception handler function * * @param Exception $ex */ function sagepayExceptionHandler(Exception $ex) { SagepayUtil::log("Exception:" . $ex->getMessage() . PHP_EOL . $ex->getTraceAsString()); if ($ex instanceof SetupException) { include_once DEMO_PATH . '/setup-error.php'; } else { include_once DEMO_PATH . '/error.php'; } }
/** * Get all tokens by customer ID * * @param array $customerId * * @return array */ public function getAllTokensByCustomerId($customerId) { $query = 'SELECT * FROM `' . $this->table . '` WHERE customer_id = ?'; try { return $this->dbHelper->execute($query, array($customerId))->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $ex) { SagepayUtil::log($ex->getMessage()); return array(); } }
/** * Throw exception if accessed wrong path * * @param string $name * @param mixed $arguments * @throws Exception */ public function __call($name, $arguments) { SagepayUtil::log('Called invalid action "' . $name . '" with params: ' . json_encode($arguments)); throw new Exception('Invalid page was accessed'); }
/** * Send a POST request to SagePay and return the response as an array. * * @param string $url The url to POST to. * @param array $data The data to post. * @param int $ttl cURL time of execution * @param string $caCertPath path to SSL certificate * * @return array The response from Sage Pay. */ public static function requestPost($url, $data, $ttl = 30, $caCertPath = '') { set_time_limit(60); $output = array(); $curlSession = curl_init(); curl_setopt($curlSession, CURLOPT_URL, $url); curl_setopt($curlSession, CURLOPT_HEADER, 0); curl_setopt($curlSession, CURLOPT_POST, 1); curl_setopt($curlSession, CURLOPT_POSTFIELDS, SagepayUtil::arrayToQueryString($data)); curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlSession, CURLOPT_TIMEOUT, $ttl); curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 2); if (!empty($caCertPath)) { curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, 1); curl_setopt($curlSession, CURLOPT_CAINFO, $caCertPath); } else { curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, 0); } $rawresponse = curl_exec($curlSession); if (curl_getinfo($curlSession, CURLINFO_HTTP_CODE) !== 200) { $output['Status'] = "FAIL"; $output['StatusDetails'] = "Server Response: " . curl_getinfo($curlSession, CURLINFO_HTTP_CODE); $output['Response'] = $rawresponse; return $output; } if (curl_error($curlSession)) { $output['Status'] = "FAIL"; $output['StatusDetail'] = curl_error($curlSession); $output['Response'] = $rawresponse; return $output; } curl_close($curlSession); $requestForLog = SagepayUtil::arrayToQueryStringRemovingSensitiveData($data, self::$nonSensitiveRequestDataArray); $response = SagepayUtil::queryStringToArray($rawresponse, "\r\n"); $responseForLog = SagepayUtil::queryStringToArrayRemovingSensitiveData($rawresponse, "\r\n", self::$nonSensitiveResponseDataArray); SagepayUtil::log("Request:" . PHP_EOL . $requestForLog); SagepayUtil::log("Response:" . PHP_EOL . json_encode($responseForLog)); return array_merge($output, $response); }
/** * Notify page, used for server ONLY */ public function actionNotify() { $payment = new ModelPayment(); $result = $payment->getByVendorTxCode(filter_input(INPUT_POST, 'VendorTxCode')); $siteFqdn = $this->sagepayConfig->getSiteFqdn(); SagepayUtil::log('NOTIFY:' . PHP_EOL . json_encode(filter_input_array(INPUT_POST))); $vtxData = filter_input_array(INPUT_POST); if (in_array(filter_input(INPUT_POST, 'Status'), array(SAGEPAY_REMOTE_STATUS_OK, SAGEPAY_REMOTE_STATUS_AUTHENTICATED, SAGEPAY_REMOTE_STATUS_REGISTERED))) { $surcharge = floatval(filter_input(INPUT_POST, 'Surcharge', FILTER_VALIDATE_FLOAT)); $vtxData['Amount'] = $result['amount'] + $surcharge; if (filter_input(INPUT_POST, 'TxType') == SAGEPAY_REMOTE_STATUS_PAYMENT) { $vtxData['CapturedAmount'] = $vtxData['Amount']; } $data = array("Status" => SAGEPAY_REMOTE_STATUS_OK, "RedirectURL" => $siteFqdn . 'server/success?vtx=' . filter_input(INPUT_POST, 'VendorTxCode'), "StatusDetail" => 'The transaction was successfully processed.'); } else { $data = array("Status" => SAGEPAY_REMOTE_STATUS_OK, "RedirectURL" => $siteFqdn . 'server/failure?vtx=' . filter_input(INPUT_POST, 'VendorTxCode'), "StatusDetail" => filter_input(INPUT_POST, 'StatusDetail')); } $vtxData['AllowGiftAid'] = filter_input(INPUT_POST, 'GiftAid'); $payment->update(filter_input(INPUT_POST, 'VendorTxCode'), $vtxData); echo SagepayUtil::arrayToQueryString($data, "\n"); }