Using PIN code for dialing out

Hi

I’m trying to configure asterisk for asking users to enter a PIN code after dialing out a external number, I want to check if the user PIN code has rights for making the call that is attempting (for example long distance), The PIN code is located in a Mysql Dababase:
What I’m trying to do is something like this:

  • Users dial 9 + external number
    -The number is saved in a variable
    -The user is asked to enter the PIN code
    -I run agiphp script that check if the PIN code exists and has rights for the type of number dialed
    -I send the call out for DAHDI channel 1 or restrict the call if the PIN used has not permision

What I’m trying to do in the dial plan is the following:

exten => _9X.,n,Set(OUTGOINGNUMBER=${EXTEN:1}); save dialed number in OUTGOINGNUMBER
exten => _9X.,Read(pin,astcc-digit-account-number&astcc-followed-by-the-pound-key); ask for PIN and #
same => n,Playback(“one-moment-please”)
same => n,AGI(ckeckpinlevel.php,${pin}${OUTGOINGNUMBER}); agiphp script to check if PIN has rights
same => n,GotoIf($[${EXISTS(${permisionok)}]?call); If has pemition goto call
same => n,Congestion(2); User PIN is restricted
same => n,Hangup
same => n(call),Dial(DAHDI/1/${OUTGOINGNUMBER}); Dialing external number

Please, Any ideas?

What goes wrong? Where is your priority 1?