Skip to content

Instantly share code, notes, and snippets.

@jsdmultisys
Last active February 27, 2021 23:47
Show Gist options
  • Save jsdmultisys/2c941cd99ad4d013d434668f110d06d2 to your computer and use it in GitHub Desktop.
Save jsdmultisys/2c941cd99ad4d013d434668f110d06d2 to your computer and use it in GitHub Desktop.
refactor-evc-service
<?php
// ============================== BEFORE ===========================================================
/**
* @param string $mobile
* @param string|null $refId
* @param string|null $type
* @return array
* @throws BadRequestException
* @throws EvcGeneralException
* @throws GuzzleException
*/
public function inquireBalance(string $mobile, string $refId = null, string $type = null): array
{
try {
$shortcode = $this->toShortcode($mobile);
if (!config('evc.sync_endpoint')) {
throw new Exception("You need sync endpoint");
}
$refId = (!$refId) ? $this->uuid : $refId;
$url = (($type && $type == EvcConstants::MM) ? $this->mmUrl : $this->url);
$endpoint = config('evc.sync_endpoint');
$commandId = (($type && $type == EvcConstants::MM) ? "QueryOrganizationBalance" : "QueryOrgBalanceWithoutNC");
$accountType = (($type && $type == EvcConstants::MM) ? "MyTradeMoney" : "Organization Airtime Account");
$xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:api=\"http://cps.huawei.com/synccpsinterface/api_requestmgr\" xmlns:req=\"http://cps.huawei.com/synccpsinterface/request\" xmlns:com=\"http://cps.huawei.com/synccpsinterface/common\" xmlns:cus=\"http://cps.huawei.com/cpsinterface/customizedrequest\"><soapenv:Header/><soapenv:Body><api:Request><req:Header><req:Version>1.0</req:Version><req:CommandID>" . $commandId . "</req:CommandID><req:OriginatorConversationID>" . $refId . "</req:OriginatorConversationID><req:Caller><req:CallerType>2</req:CallerType><req:ThirdPartyID>" . config('evc.thirdparty_id') . "</req:ThirdPartyID><req:Password>" . config('evc.api_caller') . "</req:Password></req:Caller><req:KeyOwner>1</req:KeyOwner><req:Timestamp>" . date('YmdHis') . "</req:Timestamp></req:Header><req:Body><req:Identity><req:Initiator><req:IdentifierType>14</req:IdentifierType><req:Identifier>" . config('evc.thirdparty_id') . "</req:Identifier><req:SecurityCredential>" . config('evc.sp_operator') . "</req:SecurityCredential></req:Initiator><req:ReceiverParty><req:IdentifierType>4</req:IdentifierType><req:Identifier>" . $shortcode . "</req:Identifier></req:ReceiverParty></req:Identity><req:QueryOrganizationBalanceRequest><req:AccountType>" . $accountType . "</req:AccountType></req:QueryOrganizationBalanceRequest></req:Body></api:Request></soapenv:Body></soapenv:Envelope>";
$body = ["headers" => $this->headers, "body" => $xml];
Log::info('balance inquiry soap request :' . $xml);
$response = $this->http->post($url . $endpoint, $body);
$contents = $response->getBody()->getContents();
$result = $this->removeColon($contents);
Log::info('log result Balance Inquiry :' . $result);
$this->xmlDoc->loadXML($result);
if ($this->xmlDoc->getElementsByTagName('resResultCode')->item(0)->nodeValue == EvcConstants::SUCCESS_CODE) {
return [
"referenceId" => @$this->xmlDoc->getElementsByTagName('resOriginatorConversationID')->item(0)->nodeValue,
"transactionId" => @$this->xmlDoc->getElementsByTagName('resConversationID')->item(0)->nodeValue,
"responseCode" => @$this->xmlDoc->getElementsByTagName('resResultCode')->item(0)->nodeValue,
"responseDesc" => @$this->xmlDoc->getElementsByTagName('resResultDesc')->item(0)->nodeValue,
"currentBalance" => @$this->xmlDoc->getElementsByTagName('resCurrentBalance')->item(0)->nodeValue,
"unClearedBalance" => @$this->xmlDoc->getElementsByTagName('resUnclearedBalance')->item(0)->nodeValue,
"reservedBalance" => @$this->xmlDoc->getElementsByTagName('resReservedBalance')->item(0)->nodeValue,
"availableBalance" => @$this->xmlDoc->getElementsByTagName('resAvailableBalance')->item(0)->nodeValue,
];
} else {
return $this->jsonError(
$this->xmlDoc->getElementsByTagName('resResultDesc')->item(0)->nodeValue,
$this->xmlDoc->getElementsByTagName('resResultCode')->item(0)->nodeValue
);
}
} catch (ConnectException $e) {
throw new BadRequestException($e);
} catch (RequestException | Exception $e) {
throw new EvcGeneralException($e);
}
}
// ============================== AFTER ===========================================================
/**
* @param string $mobile
* @param string|null $referenceId
* @param string|null $type
* @return array
* @throws BadRequestException
* @throws EvcGeneralException
* @throws EvcUnknownException
* @throws GuzzleException
*/
public function inquireBalance(string $mobile, string $referenceId = null, string $type = null): array
{
try {
$balanceInquiryRequest = new InquireBalanceRequest($referenceId, $mobile, $type);
$xml = $balanceInquiryRequest->generateXml();
Log::info("Balance Inquiry Soap Request : $xml");
$url = $this->toEvcOrMobileMoneyUrl($type);
$response = $this->http->sendRequestToEvc($url, $xml);
Log::info("Balance Inquiry Response: $response");
$document = $this->http->parseXmlFromResponse($response);
$xmlDocument = new XmlResponse($document);
// Return
$balanceInquiryResponse = new InquireBalance;
return $balanceInquiryResponse->generate($xmlDocument);
} catch (ConnectException $e) {
throw new BadRequestException($e);
} catch (RequestException $e) {
throw new EvcGeneralException($e);
} catch (Exception $e) {
throw new EvcUnknownException($e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment