TomorrowIdeas / plaid-sdk-php

PHP bindings for the Plaid API
MIT License
111 stars 42 forks source link

Support for Identity Verification #86

Open luxor99 opened 6 months ago

luxor99 commented 6 months ago

Any plans?

zagarskas commented 4 months ago

Turns out this was all we needed to do the Identity verification process. I had considered working this into the SDK and making a class, but for now, this is all ya need:

Simple PHP stack workflow:

  1. Send a CURL request
  2. detect if status = "active | failed | success"
  3. Catch the shareable_url variable - tell the user to visit that link

After that

Step 1 - CURL Request.

//setup json data and using json_encode() encode it into JSON string
$data = array(
    'client_user_id'  => $user_id_for_webhook, // make up an ID for user in our database
    'client_id'  => $_client_id, // ours
    'secret'  => $_secret, // ours
    'is_shareable'  => true, //we need them to click the link
    'template_id'  => $_template, // ours
    'gave_consent' => false, //If true accept_tos step marked as skipped
    'is_idempotent'  => true,
    //is_idempotent means only be created if one does not already exist
);

// encode that array
$send_data = json_encode( (object) $data, JSON_HEX_APOS | JSON_HEX_QUOT );

//initialize CURL
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");          
curl_setopt($ch,CURLOPT_HTTPHEADER, ["content-type: application/json"]);
curl_setopt($ch,CURLOPT_POSTFIELDS, $send_data);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); 

$resp = curl_exec( $ch );
// catch the data
curl_close( $ch );
//close the cURL 

///////////////////////////// here is your Identity verification URL
//decode the response
$response = json_decode( $resp, true );

if(!empty($response['status']) && $response['status'] == "active") {
    // then make the user click this link
    echo "Click this: ". $response['shareable_url'];
}

if(!empty($response['status']) && $response['status'] == "success") {
    // then the user filled out the form, and you need to "do stuff" or catch a webhook
    echo "Your application is completed. Thank you";
}

if(!empty($response['status']) && $response['status'] == "failed") {
    // sus
    echo "Your application is sus. We will take a look... (wait right here)";
}

// here is the whole response 
echo '<pre>'.print_r($response, true ).'</pre>';

Now, from here, that same JSON response is sent back to you via WEBHOOKS\ Give these a peek, they are well done:

Closing meditation: Having this as a class would be nice within this SDK, but I can see how the usage cases would be so different for everyone that may as well just custom code this.

For example, consider our end logic here, with the different outcomes...


//when WEBHOOK received, we catch the JSON and "do stuff" with it.
//In our case, just conditional logic and DB updates.

/*###########  During Step 2 - this is a PASS in our case 
[status] => success
[steps] => Array
    (
        [documentary_verification] => not_applicable
        [kyc_check] => success
        [risk_check] => success
        [selfie_check] => not_applicable
        [verify_sms] => success
    )
###########

###########  During Step 2 - this is a FAIL in our case
[status] => failed
[steps] => Array
    (
        [documentary_verification] => not_applicable
        [kyc_check] => failed
        [risk_check] => success
        [selfie_check] => not_applicable
        [verify_sms] => success
    )
###########

###########  During Step 2 - this is a REDO 
[steps] => Array
        (
            [documentary_verification] => not_applicable
            [kyc_check] => waiting_for_prerequisite
            [risk_check] => waiting_for_prerequisite
            [selfie_check] => not_applicable
            [verify_sms] => active
        )
###########*/