Blame | Letzte Änderung | Log anzeigen | RSS feed
<?php/*** @package php_share* @author Webagentur Niewerth <tiefland@weban.de>* @copyright 2011 Webagentur Niewerth* @license propietary http://www.weban.de* @version $Rev: 760 $* @filesource**//**** @package php_share* @author Webagentur Niewerth <tiefland@weban.de>* @copyright 2011 Webagentur Niewerth*/// SVN: $Id: billsafe.php 760 2011-10-12 14:04:01Z tiefland $require_once "creditcard.interface.php";require_once "Billsfafe_HttpResponse.class.php";require_once "Config.php";class billsafe implements creditcard{protected $ini;protected $version;protected $date;function __construct(){$this->date ='$Date: 2011-10-12 16:04:01 +0200 (Wed, 12 Oct 2011) $';$this->version = substr( $this->date, 7, 19 );if ( !is_array( $_SESSION["INI"] ) ){$this->ini = $GLOBALS["INI"];}else{$this->ini = $_SESSION["INI"];}}function getCreditcardStatus( $bestellId, $summe = null ){global $Userdata;if ( is_null( $summe ) ){$summe = getBasketSumme();}//$summe = ( double )$summe;$server = $this->ini["billsafe"]["server"];try{$url = $server;$params["merchant_id"] = $this->ini["billsafe"]["zugang"];$params["merchant_license"] = $this->ini["billsafe"]["secret"];$params["application_signature"] = $this->ini["billsafe"]["signature"];$params["application_version"] = $this->version;$params["method"] = $this->ini["billsafe"]["method"];$params["format"] = $this->ini["billsafe"]["format"];$params["order_number"] = $bestellId;$params["order_amount"] = $summe;$params["order_taxAmount"] = round( getBasketSummeMwSt(), 2 );$params["order_currencyCode"] = "EUR";//$params["customer_id"] = $Userdata["ID"];$params["customer_gender"] = ( $_SESSION["SHOP"]["buy"]["Persdata"]["Anrede"] =="Frau" ) ? "f" : "m";$params["customer_firstname"] = $_SESSION["SHOP"]["buy"]["Persdata"]["Vorname"];$params["customer_lastname"] = $_SESSION["SHOP"]["buy"]["Persdata"]["Nachname"];$params["customer_street"] = $_SESSION["SHOP"]["buy"]["Persdata"]["Strasse"];$params["customer_houseNumber"] = $_SESSION["SHOP"]["buy"]["Persdata"]["Hausnummer"];$params["customer_postcode"] = $_SESSION["SHOP"]["buy"]["Persdata"]["PLZ"];$params["customer_city"] = $_SESSION["SHOP"]["buy"]["Persdata"]["Ort"];$params["customer_country"] = "DE";$params["customer_email"] = $_SESSION["SHOP"]["buy"]["Persdata"]["email"];$params["customer_phone"] = $_SESSION["SHOP"]["buy"]["Persdata"]["Telefon"];$items = getBasketItems();foreach ( $items as $item_id => $item ){$params["articleList_" . $item_id . "_number"] = $item["ID"];$params["articleList_" . $item_id . "_name"] = $item["kurzbezeichnung"];//$params["articleList_" . $item_id . "_description"] = $item["beschreibung"];$params["articleList_" . $item_id . "_quantity"] = $item["Menge"];if ( $this->ini["netto_preise"] == 1 ){$params["articleList_" . $item_id . "_netPrice"] = $item["preis"];}else{$params["articleList_" . $item_id . "_grossPrice"] =$item["preis"];}$params["articleList_" . $item_id . "_tax"] = 19;$params["articleList_" . $item_id . "_type"] = ( $item["Father"] ==-3 ) ? "shipment" : "goods";if ( is_numeric( $item["preis2"] ) && $item["preis2"] >0 ){$item_id++;$name = ( getAufschlag() < 0 ) ?"Zahlungssystemaufschlagrabatt" :"Zahlungssystemaufschlag";$name .= " (" . getProzent() . "%)";$params["articleList_" . $item_id . "_number"] =999999;$params["articleList_" . $item_id . "_name"] = $name;//$params["articleList_" . $item_id . "_description"] = $item["beschreibung"];$params["articleList_" . $item_id . "_quantity"] = 1;if ( $this->ini["netto_preise"] == 1 ){$params["articleList_" . $item_id . "_netPrice"] =getAufschlag();}else{$params["articleList_" . $item_id ."_grossPrice"] = getAufschlag();}$params["articleList_" . $item_id . "_tax"] = 19;$params["articleList_" . $item_id . "_type"] ="handling";}}$b_url = $this->ini["absoluteURL"];$b_url = ( $this->ini["ssl_buy"] ) ? str_replace( "http://","https://", $b_url ) : $b_url;$params["url_return"] = $b_url . "/billsafe_accept_" . $bestellId .".html";$params["url_cancel"] = $b_url . "/billsafe_cancel_" . $bestellId .".html";$params["product"] = "invoice";foreach ( $params as $key => $val ){$param_arr[] = urlencode( utf8_encode( $key ) ) . "=" .urlencode( utf8_encode( $val ) );}$params = "" . implode( "&", $param_arr );//echo $params;$p_url = parse_url( $url );$header[] = 'POST ' . $p_url["path"] . ' HTTP/1.1';$header[] = 'Host: ' . $p_url["host"];$header[] = 'Content-Type: text/plain';$header[] = 'Content-Length: ' . strlen( $params );$header[] = 'Accept-Encoding: identity'; //no compression yet$auth = $this->ini["billsafe"]["user"] . ':' . $this->ini["billsafe"]["password"];$header[] = 'Authorization: Basic ' . base64_encode( $auth );$header[] = 'Connection: close';$header = utf8_encode( implode( "\r\n", $header ) );$fp = fsockopen( "ssl://" . $p_url["host"], 443, $error_nr,$error_msg );if ( $fp ){if ( fwrite( $fp, $header . "\r\n\r\n" . $params ) ){while ( !feof( $fp ) ){$rawResponse .= fgets( $fp, 4096 );}}}$response["full"] = $this->_parseResponse( $rawResponse );$cfg = new Config();$cfg_out = $cfg->parseConfig( $response["full"]->body, "XML",array( "isFile" => false ) );$xml = $cfg_out->toArray();$response["parsed"] = $xml["root"]["response"];if ( $response["parsed"]["ack"] == "OK" ){$ret = "<form name='rechnung' action='" . $this->ini["billsafe"]["gateway"] ."' id='zahlung'><input type='hidden' name='token' value='" . $response["parsed"]["token"] ."'><input type='submit' value='per Rechnung bezahlen'></form><script type=\"text/javascript\">zahlungsubmit();</script>";}else{admin_debug( $response["parsed"] );if ( $this->ini["change_order"] ){$_SESSION["zahlarten"]["billsafe"] = false;$ret["error"] = true;$ret = $this->_getMsg( $ret, "init_fehler", $bestellId );}}fclose( $fp );return $ret;}catch ( exception $e ){admin_debug( $e );$_SESSION["zahlarten"]["billsafe"] = false;$ret["error"] = true;$ret = $this->_getMsg( $ret, "init_fehler", $bestellId );}}function getCreditcardReturnStatus( $bestellId ){$server = $this->ini["billsafe"]["server"];try{$table = ( $this->ini["dbConnect"]["order_db"] ) ? $this->ini["dbConnect"]["order_db"] . "." : "";$table .= "Bestellung";$query = "SELECTBemerkungFROM$tableWHEREid=$bestellId";$res = mysql_query( $query, $GLOBALS["order_dbh"] );$row = mysql_fetch_assoc( $res );$bemerkung = $row["Bemerkung"];if ( $_GET["action"] == "accept" ){$params["merchant_id"] = $this->ini["billsafe"]["zugang"];$params["merchant_license"] = $this->ini["billsafe"]["secret"];$params["application_signature"] = $this->ini["billsafe"]["signature"];$params["application_version"] = $this->version;$params["method"] = "getTransactionResult";$params["token"] = $_GET["token"];$params["format"] = $this->ini["billsafe"]["format"];foreach ( $params as $key => $val ){$param_arr[] = urlencode( utf8_encode( $key ) ) ."=" . urlencode( utf8_encode( $val ) );}$params = "" . implode( "&", $param_arr );//var_dump($params);$p_url = parse_url( $server );$header[] = 'POST ' . $p_url["path"] . ' HTTP/1.1';$header[] = 'Host: ' . $p_url["host"];$header[] = 'Content-Type: text/plain';$header[] = 'Content-Length: ' . strlen( $params );$header[] = 'Accept-Encoding: identity'; //no compression yet$auth = $this->ini["billsafe"]["user"] . ':' . $this->ini["billsafe"]["password"];$header[] = 'Authorization: Basic ' . base64_encode( $auth );$header[] = 'Connection: close';$header = utf8_encode( implode( "\r\n", $header ) );$fp = fsockopen( "ssl://" . $p_url["host"], 443, $error_nr,$error_msg );if ( $fp ){if ( fwrite( $fp, $header . "\r\n\r\n" . $params ) ){while ( !feof( $fp ) ){$rawResponse .= fgets( $fp, 4096 );}}}$response["full"] = $this->_parseResponse( $rawResponse );$cfg = new Config();$cfg_out = $cfg->parseConfig( $response["full"]->body,"XML", array( "isFile" => false ) );$xml = $cfg_out->toArray();$response["parsed"] = $xml["root"]["response"];if ( $response["parsed"]["ack"] == "OK" ){switch ( $response["parsed"]["status"] ){case "ACCEPTED":$res = true;$kk_info["code"] = $response["parsed"]["transactionId"];$kk_info["anbieter"] = "billsafe";$kk_info["status"] = "offen";$bemerkung .="Die Zahlung wurde von BillSAFE am " .date( "d.m.Y" ) . " um " . date( "H:i" ) ." Uhr genehmigt!\nTransaktionsnummer: " .$kk_info["code"] . "\n";if ( USE_TRANSLATION2 === true ){$msg = $GLOBALS["langstrings"]["buy"]["billsafe_accepted"];} elseif ( $_SESSION["languageException"] ){$msg = "Thanks for your purchase at " .__SHOP__ . "!";}else{$msg ="Vielen Dank für Ihren Einkauf bei " .__SHOP__ . "!";}$sql_u_ab = "UPDATEartikel_to_Bestellung AS abSETrechnungStatus='schreiben'WHEREBestellung=$bestellIdANDartikel IN (SELECT ID FROM artikel WHERE ID=ab.artikel AND Father !=-3)";mysql_query( $sql_u_ab, $GLOBALS["order_dbh"] );break;case "DECLINED":$kk_info["code"] = $response["parsed"]["declineReason"]["code"];$kk_info["status"] = "abgelehnt";$_SESSION["zahlarten"]["billsafe"] = false;$kk_info["anbieter"] = "billsafe";$bemerkung .="Die Zahlung wurde von Billsafe am " .date( "d.m.Y" ) . " um " . date( "H:i" ) ." Uhr abgelehnt!\nGrund: " . $response["parsed"]["declineReason"]["message"] ."\n";$res = false;break;}fclose( $fp );}else{admin_debug( $response["parsed"] );$res = false;$msg = "Ein Fehler ist aufgetreten! ";}} elseif ( $_GET["action"] == "cancel" ){$bemerkung .="Die Zahlung per billsafe wurde vom Kunden am " .date( "d.m.Y" ) . " um " . date( "H:i" ) ." Uhr abgebrochen!\n";$kk_info["code"] = "";$kk_info["status"] = "abgebrochen";$res = false;}if ( is_array( $kk_info ) ){$kk_info_str = serialize( $kk_info );}$query = "UPDATE$tableSET";if ( $kk_info_str ){$query .= "kk_info='$kk_info_str',";}$query .= "Bemerkung='$bemerkung'WHEREid=$bestellId";mysql_query( $query, $GLOBALS["order_dbh"] );$ret = array( "error" => !$res );if ( $ret["error"] ){$msg .= $this->_getMsg( $ret, $kk_info["status"], $bestellId );}$ret["meld"] = $msg;return $ret;}catch ( exception $e ){admin_debug( $e );}}function genCheckSum( $src ){switch ( $this->ini["kreditkarte"]["sha_algo"] ){case "sha512":$data = hash( "sha512", $src );break;case "sha256":$data = hash( "sha256", $src );break;case "sha1":default:$data = sha1( $src );break;}return $data;}private function _parseResponse( $responceString ){if ( empty( $responceString ) ){throw new Exception( 'invalid response' );}$separator = "\r\n\r\n";$separatorLength = strlen( $separator );$headerEndPosition = strpos( $responceString, "\r\n\r\n" );if ( $headerEndPosition === false ){throw new Exception( 'invalid response' );}$header = substr( $responceString, 0, $headerEndPosition );//$this->_verbose( "[response header] \r\n" . $header );$body = substr( $responceString, $headerEndPosition + $separatorLength );$response = new Billsafe_HttpResponse();if ( preg_match( '#http/\d.\d (\d+) (.*)#i', $header, $match ) ){$response->statusCode = trim( $match[1] );$response->statusText = trim( $match[2] );}if ( preg_match( '#Content-Type: (.*)#i', $header, $match ) ){$response->contentType = trim( $match[1] );}if ( preg_match( '#Content-Length: (.*)#i', $header, $match ) ){$response->contentLength = ( int )trim( $match[1] );}if ( preg_match( '#Transfer-Encoding: chunked#i', $header, $match ) ){$response->body = $this->_joinChunks( $body );$response->contentLength = strlen( $response->body );}else{$response->body = trim( $body );}//$this->_verbose( "[response body] \r\n" . $response->body );return $response;}private function _getMsg( $ret, $status, $bestellId ){if ( USE_TRANSLATION2 === true ){if ( $this->ini["change_order"] ){$abschluss = $GLOBALS["langstrings"]["buy"]["click_button"];}else{$abschluss = $GLOBALS["langstrings"]["buy"]["cancel_order"];}} elseif ( $_SESSION["languageException"] ){if ( $this->ini["change_order"] ){$abschluss ="Click on the button to choose an other payment method.";}else{$abschluss = "Therefore we will now cancel your order.";}}else{if ( $this->ini["change_order"] ){$abschluss ="Klicken Sie auf den Button, um eine andere Zahlweise auszuwählen.";}else{$abschluss ="Deshalb werden wir nun Ihre Bestellung nun stornieren.";}}switch ( $status ){case "init_fehler":if ( USE_TRANSLATION2 === true ){$msg = $GLOBALS["langstrings"]["buy"]["billsafe_not_avail"];} elseif ( $_SESSION["languageException"] ){$msg ="Unfourtunately Billsafe is not available at the moment. ";}else{$msg ="Leider steht Billsafe zur Zeit nicht zur Verfügung. ";}break;case "abgebrochen":if ( USE_TRANSLATION2 === true ){$msg = $GLOBALS["langstrings"]["buy"]["order_canceled"];} elseif ( $_SESSION["languageException"] ){$msg = "You have canceled the payment. ";}else{$msg = "Sie haben die Zahlung abgebrochen. ";}break;case "abgelehnt":if ( USE_TRANSLATION2 === true ){$msg = $GLOBALS["langstrings"]["buy"]["billsafe_declined"];} elseif ( $_SESSION["languageException"] ){$msg ="Unfortunately the payment over BILLSAFE is not possible. ";}else{$msg ="Leider ist der Rechungskauf mit BillSAFE nicht möglich. ";}break;}$msg .= $abschluss;if ( $this->ini["change_order"] ){$msg .= '<form method="post" action="/"><input type="hidden" name="best_id" value="' . $bestellId .'"><input type="hidden" name="email" value="' . $_SESSION["SHOP"]["buy"]["Persdata"]["email"] .'"><input type="hidden" name="mode" value="change_order"><input type="submit" value="Zahlart / Bestellung ändern"></form>';}return $msg;}}?>