Search code examples
controlleryii2tokengocardless

How to capture a GoCardless redirect sent back by Go Cardless to my website once the direct debit mandate has been accepted?


This is my CustomerController which gets a checklist of customers to be sent the direct debit mandate with their details that I already have on database. Once the customer fills in the mandate Gocardless sends a redirect uri to the setting "success_redirect_url" which is the action under the site controller ie. site/gocardlesscustomercreated

public function actionCreategocardlesscustomer()
{
   $keylist = Yii::$app->request->get('keylist');
   $comp = Company::findOne(1);
   if (!empty($keylist)){
   foreach ($keylist as $key => $value)
   {
     $model = $this->findModel($value);
     if ($model !== null) 
       {
         $client = new \GoCardlessPro\Client([
        'access_token' => 'sandbox_b__7gf_Vn6dYEKFTy3C-GMRamuFz_siKhQsMiZ-',
        'environment' => \GoCardlessPro\Environment::SANDBOX
        ]);
        $redirectFlow = $client->redirectFlows()->create([
        'params' => [
            "description" => "Clean",
            "session_token" => Yii::$app->session->getId(),
            "success_redirect_url" => Url::to(['site/gocardlesscustomercreated'], 'http'),
            "prefilled_customer" => [
                "given_name" => $model->name,
                "family_name" => $model->surname,
                "email" => $model->email,
                "address_line1" => $model->productnumber." ".$model->productsubcategory->name,
                "city" => $comp->address_area2,
                "postal_code" => $model->postcodefirsthalf." ".$model->postcodesecondhalf,
                ]
            ]
        ]);
       Yii::$app->session->setFlash('success', "ID: " . $redirectFlow->id ." Create Customer on Gocardless: ".Html::a($redirectFlow->redirect_url,$redirectFlow->redirect_url));
     }
   } 
   Yii::$app->session['redirectflowid'] = $redirectFlow->id;
   Yii::$app->session['redirectflowredirecturl'] = $redirectFlow->redirect_url;
  }
  else throw new NotFoundHttpException('No ticks selected.');

}

The complete Url that I get in my browser from Gocardless once redirected is: http://frontend.dev/site/gocardlesscustomercreated?redirect_flow_id=RE0000Y6TRAAQHGHWR4C5ZTEA18S2QJG

How do I capture this

 ?redirect_flow_id=RE0000Y6TRAAQHGHWR4C5ZTEA18S2QJG

that Gocardless has added onto my Url::to and sent back?

Do I use a rule under UrlManager like:

'site/gocardlesscustomercreated/<id:\d+>'=>'site/gocardlesscustomercreated'

or would I be better off using

'rules' => [
    ['class' => 'yii\rest\UrlRule', 'controller' => 'gocardlesscustomercreated'],
],

or adjust .htaccess. ?


Solution

  • All you need to do is to be able to pass the parameter sent by gocardless so you can get it and use it you dont need to specifically assign any url rule in the urlManager just add a parameter to your action gocardlesscustomercreated with the same name as the parameter passed from the gocardless like below

    public function actionGocardlesscustomercreated($redirect_flow_id)
    

    Now you can echo $redirect_flow_id inside the action to print it or save it any where you like