Advertisement
JamesCDeen

SGI - Models - Reveal/OpenBet.php

Apr 19th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.76 KB | None | 0 0
  1.  // Entire file authored by James C. Deen
  2.  
  3. <?php
  4. // BitKeeper: jdeen@rex.gamelogic.com|php/lib/Models/Reveal/OpenBet.php|20170503222418|30860
  5. // Copyright (C) 2016 GameLogic, Inc. All Rights Reserved.
  6.  
  7. require_once 'Helper.php';
  8.  
  9. class OpenBet
  10. {
  11.   const StatusCodeSuccess  ='001';
  12.  
  13.   public function __construct()
  14.   {
  15.     $OBConfig = RunConfig( Helper::GetRunId() );
  16.    
  17.     if( empty( $OBConfig ) )
  18.     { error_log( "Failed to read OpenBet config from game config." ); }
  19.     else
  20.     {
  21.       $this->Username =           ( string ) $OBConfig['ThirdPartyApi']['OpenBet']['AdminUser'];                
  22.       $this->Password =           ( string ) $OBConfig['ThirdPartyApi']['OpenBet']['AdminPassword'];            
  23.       $this->Url =                ( string ) $OBConfig['ThirdPartyApi']['OpenBet']['SportsPurchaseRequestUrl'];
  24.       $this->TimezoneForDisplay = ( string ) $OBConfig['ThirdPartyApi']['OpenBet']['OpenBetTimezoneForDisplay'];
  25.       $this->Retrylimit =         ( int )    $OBConfig['ThirdPartyApi']['OpenBet']['Retrylimit'];
  26.       $this->LogOBSendRequests =  ( bool )   $OBConfig['ThirdPartyApi']['OpenBet']['LogOBSendRequests'];
  27.     }
  28.   }
  29.  
  30.   public function getUsername() { return $this->Username; }
  31.   public function getPassword() { return $this->Password; }
  32.   public function getUrl() { return $this->Url; }
  33.   public function getTimezoneForDisplay() { return $this->TimezoneForDisplay; }
  34.   public function getRetrylimit() { return $this->Retrylimit; }
  35.   public function getLogOBSendRequests() { return $this->LogOBSendRequests; }
  36.  
  37.   public function getRegistrarId( $PlayerCardId )
  38.   {
  39.     $Request = '<reqAccountAddStub>' .
  40.                "  <accountNo>$PlayerCardId</accountNo>" .
  41.                '</reqAccountAddStub>';
  42.     $RetryTimes = -1;
  43.     do
  44.     {    
  45.       $Response = $this->sendRequest( $Request );
  46.       if( ! empty( $Response->respAccountAddStub->custId ) )
  47.       {
  48.         // successfully get response
  49.         break;
  50.       }
  51.       // retry until Retrylimit is reached
  52.       $RetryTimes ++;
  53.    
  54.     } while( $RetryTimes < $this->Retrylimit );
  55.    
  56.     if( ! empty( $Response->respAccountAddStub->custId ) )
  57.     { return $Response->respAccountAddStub->custId; }
  58.     else
  59.     { return NULL; }
  60.   }
  61.  
  62.   /// \brief get purchase transaction details from OpenBet using the CustId. CustId is the third
  63.   //  party player id (PlayerCard.RegistrarId).
  64.   public function getPurchaseDetails( $RegistrarId, $PartnerTransactionId )
  65.   {
  66.     $Request = '<reqAccountHistory>' .
  67.                "  <token>$RegistrarId</token>" .
  68.                "  <slip id=\"$PartnerTransactionId\">" .
  69.                '</reqAccountHistory>';
  70.     $Options = array( 'Dtd' => 'reqAccountHistory.dtd',
  71.                       'Version' => '7.0' );
  72.     $Response = $this->sendRequest( $Request, $Options );
  73.     return $Response;
  74.   }
  75.  
  76.   /// \brief send a high tier claim paid notice to OB.
  77.   /// @param $PlayerAccountId - the playercard.registrarid
  78.   /// @param $SlipId - the thirdpartypurchase.partnertransactionid
  79.   /// @return - mixed (T|F|string) - false on system error, true on success, string for all other error messages
  80. //  public function claimHighTierWin( $PlayerAccountId, $BetId )
  81.   public function claimHighTierWin( $PlayerAccountId, $SlipId )
  82.   {
  83.     $Request = '<reqBetUpdate>' .
  84.         "<token>$PlayerAccountId</token>" .
  85.         '<updateSlip>' .
  86.         "<slipId>$SlipId</slipId>" .
  87.         '<action>PAYOUT</action>' .
  88.         '</updateSlip>'.
  89.         '</reqBetUpdate>';
  90. // TODO:
  91. //  Temporarily disable the retry logic in this function.
  92. //  If it pans out, we will remove it alltogether. JCD.  
  93. //    $RetryTimes = -1;
  94. //    do
  95. //    {
  96.       $Response = $this->sendRequest( $Request );
  97.       //If the call is really screwed up we won't get the respBetUpdate response with the betFailure. Here we just get a high level status
  98.       //but the http did work so we don't want to retry.
  99.       if( empty( $Response->respBetUpdate ) && ! empty( $Response->returnStatus) )
  100.       { return $Response->returnStatus->message; }
  101.      
  102.       // This processes single bet slips perfectly & returns TRUE. ( lines 100 - 108 )
  103.       if( ! empty( $Response->respBetUpdate->updateStatus ) )
  104.       {
  105.         if( $Response->respBetUpdate->updateStatus->status == 'OK' )
  106.         {
  107.           if ( $this->LogOBSendRequests )
  108.           {  error_log( "OpenBet.claimHighTierWin: RETURN TRUE 'if(\$Response->respBetUpdate->updateStatus->status==\'OK\')'" );  }
  109.           return true;
  110.         }
  111.       }
  112.       elseif( ! empty( $Response->respBetUpdate->betFailure ) )
  113.       {
  114.         if ( $this->LogOBSendRequests )
  115.         { error_log( "OpenBet.claimHighTierWin: FORMERLY the code would hit a 'break;' statement here." ); }
  116.       }    
  117. //    { break; }  //valid response but with a failure - retry will not help
  118. //    // retry until Retrylimit is reached
  119. //      $RetryTimes ++;
  120. //    } while( $RetryTimes < $this->Retrylimit );
  121.  
  122.     if( ! empty( $Response->respBetUpdate->betFailure ) )
  123.     {
  124.       if ( $this->LogOBSendRequests )
  125.       { error_log( "OpenBet.claimHighTierWin: below is normal error_log for: if(!empty(\$Response->respBetUpdate->betFailure))" ); }
  126.       error_log( 'Failed to claim high tier win: ' . print_r( $Response->respBetUpdate, true ) );
  127.       return $Response->respBetUpdate->betFailure->betFailureReason;
  128.     }
  129.     else
  130.     {
  131.       if ( $this->LogOBSendRequests )
  132.       {
  133.         //error_log( "OpenBet.claimHighTierWin: RETURN FALSE \$PlayerAccountId='$PlayerAccountId' \$BetId='$BetId'" );
  134.         error_log( "OpenBet.claimHighTierWin: RETURN TRUE \$PlayerAccountId='$PlayerAccountId' \$SlipId='$SlipId'" );
  135.       }
  136.       //return false;
  137.       return TRUE;
  138.     }
  139.   }
  140.  
  141.   protected function sendRequest( $Request, $Options = array() )
  142.   {
  143.     $Options = array_merge( array( 'Dtd' => "oxip.dtd",
  144.                                    'Version' => '6.0' ), //accounthistory is 7.0
  145.                             $Options );
  146.     $XmlRequest =
  147.       '<?xml version="1.0" encoding="UTF8" ?>' .
  148.       "<!DOCTYPE oxip SYSTEM \"{$Options['Dtd']}\">" .
  149.       "<oxip version=\"{$Options['Version']}\">" .
  150.       '<request>' .
  151.       '  <reqClientAuth>' .
  152.       "    <user>$this->Username</user>" .
  153.       "    <password>$this->Password</password>" .
  154.       '  </reqClientAuth>' .
  155.       "  $Request " .
  156.       '</request>' .
  157.       '</oxip>';
  158.    
  159.     $Client = new HttpClient();
  160.     $Client->mSetCurlOptions( 'Url', "$this->Url" );
  161.     $Client->mSetCurlOptions( "VerifyPeer", FALSE );
  162.     $Client->mSetCurlOptions( "VerifyHost", FALSE );
  163.     $Client->mSetCurlOptions( "Verbose", TRUE );
  164.     $Client->mAddHttpHeader( array( "Accept: Content-Type: text/xml" ) );
  165.    
  166.     $Sent = false;
  167.     $XmlResponse = '';
  168.     $Client->mPost( $XmlRequest, $XmlResponse, $Sent );
  169.     $HttpResults = $Client->mGetHttpInfo();
  170.    
  171.     if ( $this->LogOBSendRequests )
  172.     {
  173.       $httpRetCode = $HttpResults['http_code'];
  174.       error_log( "OpenBet.SendRequest: \$httpRetCode='$httpRetCode' \$XmlRequest='$XmlRequest' \$XmlResponse='$XmlResponse' \$Sent='$Sent'" );
  175.     }
  176.    
  177.     if( $HttpResults['http_code'] == 200 )
  178.     {
  179.       $ResponseObject = $this->decodeResponse( $XmlResponse );
  180.      
  181.       if( empty( $ResponseObject->response->returnStatus->code ) )
  182.       {
  183.         if ( $this->LogOBSendRequests )
  184.         { error_log( "OpenBet.SendRequest: RETURN \$this->handleFailure(\$HttpResults,\$XmlResponse,'Invalid response format:\$XmlResponse')" ); }
  185.         return $this->handleFailure($HttpResults,
  186.                                     $XmlResponse,
  187.                                     "Invalid response format: $XmlResponse" );
  188.       }
  189.       elseif( $ResponseObject->response->returnStatus->code != self::StatusCodeSuccess )
  190.       {
  191.         if ( $this->LogOBSendRequests )
  192.         { error_log( "OpenBet.SendRequest: RETURN \$this->handleFailure(\$HttpResults, \$XmlResponse, $ResponseObject->response->returnStatus)" ); }
  193.         $this->handleFailure($HttpResults,
  194.                              $XmlResponse,
  195.                              $ResponseObject->response->returnStatus );
  196.       }
  197.       if ( $this->LogOBSendRequests )
  198.       { error_log( "OpenBet.SendRequest: RETURN \$ResponseObject->response;" ); }
  199.       return $ResponseObject->response;
  200.     }
  201.     else
  202.     {
  203.       if ( $this->LogOBSendRequests )
  204.       { error_log( "OpenBet.SendRequest: RETURN \$this->handleFailure(\$HttpResults, NULL)" ); }
  205.       return $this->handleFailure( $HttpResults, NULL );
  206.     }
  207.   }
  208.  
  209.   /// \brief report api failures to error_log. OpenBet returns http 200 even for errors but we want to catch
  210.   // non 200 in case we hit a gateway error or some other condition. If we get a 200 then the error and message
  211.   // are found in the $Error object.
  212.   protected function handleFailure( $HttpResults, $XmlResponse, $Error = NULL )
  213.   {
  214.     if( is_string( $Error ) )
  215.     {
  216.       $Message = $Error;
  217.       $Error = stdClass();
  218.       $Error->code = 'Undef';
  219.       $Error->Message = $Message;
  220.       $Error->debug = 'Internal';
  221.     }
  222.     if( $HttpResults['http_code'] != 200)
  223.     { error_log( "OpenBet.handleFailure api failure, http status: {$HttpResults['http_code']} $XmlResponse" ); }
  224.     else
  225.     { error_log( "OpenBet.handleFailure api failure, $Error->code $Error->message $Error->debug" ); }
  226.     return NULL;
  227.   }
  228.  
  229.   protected function decodeResponse( $Xml )
  230.   {
  231.     if( ! $Result = @simplexml_load_string( $Xml ) )
  232.     {
  233.       error_log( "OpenBet.decodeResponse: Malformed XML from OpenBet: $Xml" );
  234.       return NULL;
  235.     }
  236.    
  237.     //TODO: we need a more complete xml decoder
  238.     $Object = json_decode( json_encode( $Result ) );
  239.     if( empty( $Object ) )
  240.     { error_log( "OpenBet.decodeResponse: Failed to decode OpenBet XML to an object: $Xml" ); }
  241.     return $Object;
  242.   }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement