1. 程式人生 > >【HAVENT原創】Australian Business Number (ABN) 驗證

【HAVENT原創】Australian Business Number (ABN) 驗證

exception lis mat ber java pri bnl javascrip style

PHP 代碼如下:

//   ValidateABN
//     Checks ABN for validity using the published 
//     ABN checksum algorithm.
//
//     Returns: true if the ABN is valid, false otherwise.
//      Source: http://www.clearwater.com.au/code
//      Author: Guy Carpenter
//     License: The author claims no rights to this code.  
//              Use it as you wish.
function ValidateABN($abn) { $weights = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19); // strip anything other than digits $abn = preg_replace("/[^\d]/","",$abn); // check length is 11 digits if (strlen($abn)==11) { // apply ato check method $sum = 0; foreach
($weights as $position=>$weight) { $digit = $abn[$position] - ($position ? 0 : 1); $sum += $weight * $digit; } return ($sum % 89)==0; } return false; }

JavaScript 代碼如下:

function checkABN(str) {
    if (!str || str.length !== 11) {
        return
false; } var weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19], checksum = str.split(‘‘).map(Number).reduce( function(total, digit, index) { if (!index) { digit--; } return total + (digit * weights[index]); }, 0 ); if (!checksum || checksum % 89 !== 0) { return false; } return true; }

C# 代碼如下:

public static bool ValidateABN(string abn)
{
    bool isValid = false;
    int[] weight = { 10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 };
    int weightedSum = 0;

    //ABN must not contain spaces, comma‘s or hypens
    abn = StripNonDigitData(abn);

    //ABN must be 11 digits long  
    if (!string.IsNullOrEmpty(abn) & Regex.IsMatch(abn, "^\\d{11}$"))
    {
        //Rules: 1,2,3  
        for (int i = 0; i <= weight.Length - 1; i++)
        {
            weightedSum += (int.Parse(abn[i].ToString()) - ((i == 0 ? 1 : 0))) * weight[i];
        }
        //Rules: 4,5  
        return ((weightedSum % 89) == 0);
    }
    return isValid;
}

public static string StripNonDigitData(string input)
{
    StringBuilder output = new StringBuilder("");
    foreach (char c in input)
    {
        if (char.IsDigit(c))
        {
            output.Append(c);
        }
    }
    return output.ToString();
}

ABNLookup 查詢(需要去 https://abr.business.gov.au/Documentation/WebServiceRegistration 註冊一個賬號,如果成功,會在5天內收到一封郵件,裏面有個GUID),PHP 代碼:

<?php

namespace PPost\Library;

class ABNLookup extends \SoapClient{

    private $guid = "xxxxxxxxxxxxxxxxxx";    // guid
    
    public function __construct()
    {
        $params = array(
            ‘soap_version‘ => SOAP_1_1,
            ‘exceptions‘ => true,
            ‘trace‘ => 1,
            ‘cache_wsdl‘ => WSDL_CACHE_NONE
        ); 

        parent::__construct(‘http://abr.business.gov.au/abrxmlsearch/ABRXMLSearch.asmx?WSDL‘, $params);
    }
    
    public function searchByAbn($abn, $historical = ‘N‘){
        $params = new \stdClass();
        $params->searchString                = $abn;
        $params->includeHistoricalDetails    = $historical;
        $params->authenticationGuid            = $this->guid;
        return $this->ABRSearchByABN($params);
    }
}
<?php

namespace PPost\Infrastructure\Helper;


class ABNHelper
{

    public static function ValidateABN($abn){
        $weights = array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);

        // strip anything other than digits
        $abn = preg_replace("/[^\d]/","",$abn);

        // check length is 11 digits
        if (strlen($abn)==11) {
            // apply ato check method
            $sum = 0;
            foreach ($weights as $position=>$weight) {
                $digit = $abn[$position] - ($position ? 0 : 1);
                $sum += $weight * $digit;
            }
            return ($sum % 89)==0;
        }

        return false;
    }

    public static function ABNLookup($abn){
        try{
            $abnLookup = new \PPost\Library\ABNLookup();
            try{
                $result = $abnLookup->searchByAbn($abn);

                var_dump($result);
                //return $result->ABRPayloadSearchResults->response;
                return $result;
            } catch    (Exception $e){
                throw $e;
            }
        } catch(Exception $e){
            throw $e;
        }
    }
}

【HAVENT原創】Australian Business Number (ABN) 驗證