#!/usr/bin/perl # # example.pl - Example of diamondcard.us api usage # # # Copyright (c) 2007, DreamTime.net Inc. # All rights reserved. # Email: sales@diamondcard.us # Phone: 1-303-997-0900 # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the DreamTime.net Inc. nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY DreamTime.net Inc. ``AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL DreamTime.net Inc. BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # # Values to adjust my $AUTH_CODE = 'ABCD1234567890EFGH'; # -- Local constants -- my $SERVER = 'https://www.diamondcard.us'; my $URI = 'api'; my $PROXY = $SERVER.'/exec/api-dispatcher'; my $WSDL = $SERVER.'/download/api/dc-api.wsdl'; # -- Local data -- my($errMsg); my($action); my(%opts); # -- External routines -- use Getopt::Long; # Getopt stuff use SOAP::Lite; use SOAP::WSDL; use Data::Dumper; # -- START HERE -- &Usage if scalar(!@ARGV); # Load input params GetOptions(\%opts, "help|?","action=s", "type=s", "cnt=s", "code=s", "st=s", "dur=i","quant=i", "orderId=i", "accId=s", "user=s", "pass=s", "bal=s", "amount=s", "cur=s", "first=s", "last=s", "email=s", "phone=s", "comm=s", "status=s", "speedd=s","pinl=s","ani=s","number=s","exp=s","redir=s","arenew=s","nomail=s" ) or &Usage; &CheckParams or &Usage; # Check input params if ($action eq 'did_listcountries') { # Send request to get countries list my %params = ('AuthKey' => $AUTH_CODE, # Aauth code 'DidType' => $opts{type}); # Do sending my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print "List of counteries:\n"; print join("\n",@{$res->{Countries}})."\n"; } else { # Fail sending print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'did_liststates') { # Send request to get countries list my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'DidType' => '', 'Country' => $opts{cnt}); # Do sending my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print "List of states:\n"; print join("\n",@{$res->{States}})."\n"; } else { # Fail sending print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'did_list') { # Send request to get countries list my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'DidType' => $opts{type}, 'Country' => $opts{cnt}, 'State' => $opts{st}); # Do sending my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print "List:\n"; print join("\n",@{$res->{List}})."\n"; } else { # Fail sending print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'order_did') { # Send request to order DID my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'DidCode' => $opts{code}, 'Quantity' => $opts{quant} || 1, 'UniqKey' => 'aa', 'Duration' => $opts{dur}); # Do sending my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print "Status: ".$res->{Status}."\n"; print "OrderId: ".$res->{OrderId}."\n"; print "Assigned numbers: ".$res->{Numbers}."\n" if ($res->{Numbers}); } else { # Fail sending print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'order_info') { # Send request to get order info my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'OrderId' => $opts{orderId}); # Do sending my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print "Status: $res->{Status}\n"; print "Assigned numbers: $res->{Numbers}\n" if ($res->{Numbers}); # print Dumper($res); } else { # Fail sending print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'acc_info') { # Add account my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'AccId' => $opts{accId}); # Create account my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print Dumper($res); } else { # Fail print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'acc_add') { # Add account my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'UserName' => $opts{user}, 'Password' => $opts{pass}, 'BalanceType' => $opts{bal}, 'Amount' => $opts{amount}, 'Currency' => $opts{cur}, 'FirstName' => $opts{first}, 'LastName' => $opts{last}, 'Email' => $opts{email}, 'NoMail' => $opts{nomail}, 'DayPhone' => $opts{phone}); # Create account my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print "Account created:\n"; print "AccId: $res->{AccId}\n"; print "PinCode: $res->{PinCode}\n"; # print Dumper($res); } else { # Fail print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'acc_recharge') { # Recharge account my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'AccId' => $opts{accId}, 'Amount' => $opts{amount}, 'Currency' => $opts{cur}); # Create account my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print "Account recharged successfully:\n"; print "New Balance: $res->{Balance} $res->{Currency}\n"; # print Dumper($res); } else { # Fail print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'acc_update') { # Update account details my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'AccId' => $opts{accId}, 'Status' => $opts{status}, 'SpeedDial' => $opts{speedd}, 'Pinless' => $opts{pinl}, 'CallerId' => $opts{ani}); # Create account my $res = &sendRequest($action, \%params); if ($res->{ErrMsg} eq 'OK') { # Success print "Account details updated successfully.\n"; } else { # Fail print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'did_info') { # DID info my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'Number' => $opts{number}); # Create account my $res = &sendRequest($action, \%params); if (!$res->{ErrMsg}) { # Success print "Number: $opts{number}\n"; print "Expiration date: ".($res->{ExpDate} ? $res->{ExpDate} : 'no expiration date')."\n"; print "AutoRenew: $res->{AutoRenew}\n"; if (!$res->{SubAccId}) { print "Redirect to: ".($res->{Redirect} ? $res->{Redirect} : 'not configured')."\n"; } else { print "Assigned to sub account $res->{SubAccId}\n"; print "Sub account Expiration date: ".($res->{SubExpDate} ? $res->{SubExpDate} : 'no expiration date')."\n"; print "Sub account AutoRenew: $res->{SubAutoRenew}\n"; print "Sub account redirect to: ".($res->{SubRedirect} ? $res->{SubRedirect} : 'not configured')."\n"; } # print Dumper($res); } else { # Fail print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'did_assign') { # Assign DID to sub account my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'Number' => $opts{number}, 'SubAccId' => $opts{accId}, 'ExpDate' => $opts{exp}); # Create account my $res = &sendRequest($action, \%params); if ($res->{ErrMsg} eq 'OK') { # Success print "Number successfully assigned.\n"; } else { # Fail print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'did_update') { # Update DID redirect settings my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'Number' => $opts{number}, 'SubAccId' => $opts{accId}, 'Redirect' => $opts{redir}, 'AutoRenew' => $opts{arenew}); # Create account my $res = &sendRequest($action, \%params); if ($res->{ErrMsg} eq 'OK') { # Success print "Number successfully updated.\n"; } else { # Fail print "Error: $res->{ErrMsg}\n"; # Error message } } elsif ($action eq 'did_renew') { # Renew DID my %params = ('AuthKey' => $AUTH_CODE, # Auth code 'Number' => $opts{number}, 'RenewPeriod' => $opts{dur}, 'UniqKey' => 'qqq'); my $res = &sendRequest($action, \%params); if ($res->{ErrMsg} eq 'OK') { # Success print "Number successfully renewed.\n"; } else { # Fail print "Error: $res->{ErrMsg}\n"; # Error message } } exit; sub Usage { # Print help page and exit (my $name = $0) =~ s/^.*\///; # Strip off the path from my name print STDERR "\n$name synopsis: \n", " $name -action=did_listcountries -type=TYPE - list of countries\n", " $name -action=did_liststates -cnt=COUNTRY_CODE - list of states for USA/Canada\n", " $name -action=did_list -type=TYPE -cnt=COUNTRY_CODE -st=STATE - list of available DIDs\n", " $name -action=order_did -code=DIDCODE -dur=DURATION -am=AMOUNT - order DID\n", " TYPE - DID type. TFN for toll free DIDs, PPN or empty for regular DIDs\n", " COUNTRY_CODE - 2 char country code.\n", " STATE - 2 char state code.\n", " DIDCODE - DID code received in -did_list action.\n", " DURATION - duration in months, available values: 1,3,6,12.\n", " AMOUNT - amount of DID to order, by default 1.\n", " $name -action=order_info -orderId=ORDER_ID - load order info by ID\n", " ORDER_ID - order ID received in -order_did action.\n", " $name -action=acc_add -user=USERNAME -pass=PASSWORD -nomail=NOMAIL -bal=BALANCETYPE -amount=AMOUNT -cur=CURRENCY -first=FIRSTNAME -last=LASTNAME -email=EMAIL -phone=PHONE - create new account\n", " USERNAME - username.\n", " PASSWORD - password.\n", " NOMAIL - if 1 do not send notification email to new account.\n", " BALANCETYPE - balancetype: 'unlim' for unlimited balance.\n", " AMOUNT - initial balance.\n", " CURRENCY - 3 char currency code for initil balance. USD will be used by default.\n", " FIRSTNAME - first name of customer.\n", " LASTNAME - last name of customer.\n", " EMAIL - email of customer.\n", " PHONE - phone in format: country code + area code + number.\n", " $name -action=acc_info -accId=ACCID\n", " ACCID - account Id.\n", " $name -action=acc_recharge -accId=ACCID -amount=AMOUNT -cur=CURRENCY\n", " ACCID - account Id.\n", " AMOUNT - initial balance.\n", " CURRENCY - 3 char currency code for initil balance. USD will be used by default.\n", " $name -action=acc_update -accId=ACCID -status=STATUS -speedd=SPEEDDIAL -pinl=PINLESS -ani=CALLERDID\n", " ACCID - account Id.\n", " STATUS - status ON - active, OFF - not active.\n", " SPEEDDIAL - list of SPEED dial numbers. Code and number pairs separate by coma. CODE1_NUMBER1,CODE2_NUMBER2. 'OFF' value will remove all sdial numbers.\n", " PINLESS - pinless numbers separated by coma. 'OFF' value will remove all pinless numbers.\n", " CALLERDID - callerId number in format: 'ON_NUMBER' - turn ON and set the number OR 'ON_NUMBER_NAME' - turn ON, set the number and caller name. 'OFF' - turning OFF this feature.\n", " $name -action=did_info -number=NUMBER - load DID info by number\n", " NUMBER - number .\n", " $name -action=did_assign -number=NUMBER -accId=ACCID -exp=EXPDATE - assign number to sub account\n", " NUMBER - number .\n", " ACCID - account Id we want to assign DID to. 'OFF' in case if we need to remove DID from sub account\n", " EXPDATE - expiration date. Format MM/DD/YYYY, empty for no expiration date.\n", " $name -action=did_update -number=NUMBER -accId=ACCID -redir=REDIRECT -arenew=AUTORENEW - change settings for DID.\n", " NUMBER - number.\n", " ACCID - account Id. Defining accId we'll change redirect settings for sub account. No account - redirect settings for main accoutn will be changed\n", " REDIRECT - redirect number or 'OFF' to remove redirect rules.\n", " AUTORENEW - autorenew ON/OFF.\n", " $name -action=did_renew -number=NUMBER -dur=DURATION - renew DID\n", " NUMBER - number.\n", " DURATION - duration in months, available values: 1,3,6,12.\n\n"; exit 1; } # -- Usage -- sub CheckParams { # Check input args if ($opts{action} =~ /^(did_listcountries|did_liststates|did_list|order_did|order_info|acc_add|acc_info|acc_recharge|acc_update|did_info|did_assign|did_update|did_renew)$/){ $action = $opts{action}; } else { print STDERR "Invalid action\n"; return 0; } return 1; } # -- LoadPars -- # # You can use SOAP::Lite or SOAP::WSDL packages for sending requests. # # # sendRequest - send request on gateway using SOAP::Lite # Call: ($err,$result) = sendRequest($func, $params) # sub sendRequest { my ($func, $params) = @_; my $soap = SOAP::Lite -> uri($URI) -> proxy($PROXY) -> on_fault(sub {return {'ErrMsg' =>ref $_[1] ? $_[1]->faultstring : $_[0]->transport->status}}) -> $func($params); return {'ErrMsg' => 'SOAP creation error'} if (!$soap); return {'ErrMsg' => $soap->{ErrMsg}} if $soap->{ErrMsg}; return $soap->result(); } # -- sendRequest -- # # sendRequest_WSDL - send request on gateway using SOAP::WSDL # Call: ($err,$result) = sendRequest($func, $params) # sub sendRequest1 { my ($func, $params) = @_; my $soap = SOAP::WSDL->new( wsdl => $WSDL); $soap->wsdlinit(); my $som = $soap->call( $func, ('inParams' => $params)); return $som->result(); } # -- sendRequest_WSDL --