Skip to content

Instantly share code, notes, and snippets.

@juanwilde
Last active April 11, 2016 01:09
Show Gist options
  • Save juanwilde/806af60944d58d1b61516277274b5096 to your computer and use it in GitHub Desktop.
Save juanwilde/806af60944d58d1b61516277274b5096 to your computer and use it in GitHub Desktop.
Part of suscriptions.php
/**
* @param $msisdn
* @param $amount
* @param $token
* @return array
*/
public function getBill($msisdn, $amount, $token)
{
if($this->_billError500 == true) {
error_log(print_r('ha dado error 500', true));
$this->_billError500 = false;
}
if($this->_billError400 == true) {
error_log(print_r('ha dado error 400', true));
$this->_billError400 = false;
}
$transaction = $this->generateTransactionCode();
try {
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<request>
<transaction>' . $transaction . '</transaction>
<msisdn>' . $msisdn . '</msisdn>
<amount>' . $amount . '</amount>
<token>' . $token . '</token>
</request>';
$request = new \GuzzleHttp\Psr7\Request('POST', $this->_billUrl, ['Content-Type' => 'text/xml; charset=UTF8'], $xml);
$response = $this->_client->send($request);
if ($response->getStatusCode() == 200) {
$response = (array)new SimpleXMLElement($response->getBody());
$data = $this->data($response['statusCode'], $response['statusMessage'], $response['txId']);
$data['200'] = 'todo OK!';
error_log(print_r('BILL_SUCCESS_____', true));
error_log(print_r($data, true));
return $data;
}
} catch (ServerException $e) { // if 500 Server Error
$this->_billError500 = true;
$response = (array)new SimpleXMLElement($e->getResponse()->getBody()->getContents());
$error = $this->errors($response['statusCode'], $response['statusMessage'], $response['txId'], $transaction);
$error['500?'] = 'error 500 cuando no hay fondos...';
error_log(print_r('BILL_ERROR_500_____', true));
error_log(print_r($error, true));
$this->getBill($msisdn, $amount, $this->getToken()['token']);
} catch (ClientException $e) { // if 400 Token Already used or Missing property
$this->billError400 = true;
$response = (array)new SimpleXMLElement($e->getResponse()->getBody()->getContents());
$error = $this->errors($response['statusCode'], $response['statusMessage'], $response['txId'], $transaction);
$error['GETSTATUSCODE'] = $response->getStatusCode();
error_log(print_r('BILL_ERROR_400_____', true));
error_log(print_r($error, true));
$this->getBill($msisdn, $amount, $this->getToken()['token']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment