DeveloperBreeze

Laravel Service Class Development Tutorials, Guides & Insights

Unlock 1+ expert-curated laravel service class tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your laravel service class skills on DeveloperBreeze.

Integrating and Using NMI Payment Gateway in Laravel

Tutorial August 14, 2024
php

   public function addCustomerVault($creditCard)
   {
       $data = [
           'ccnumber' => $creditCard['cc_number'],
           'ccexp' => $creditCard['exp_date'],
           'cvv' => $creditCard['cvv'],
           'customer_vault' => 'add_customer',
           'security_key' => $this->securityKey,
       ];

       if ($this->production === false) {
           $data['test_mode'] = 'enabled';
       }

       $createVaultCurl = curl_init();
       curl_setopt_array($createVaultCurl, [
           CURLOPT_URL => $this->url,
           CURLOPT_RETURNTRANSFER => true,
           CURLOPT_POST => true,
           CURLOPT_POSTFIELDS => http_build_query($data),
           CURLOPT_SSL_VERIFYHOST => false,
           CURLOPT_SSL_VERIFYPEER => false,
       ]);
       $createVaultResponse = curl_exec($createVaultCurl);
       curl_close($createVaultCurl);

       \Log::info('NMI Vault Response: ' . $createVaultResponse);

       if ($createVaultResponse === false) {
           return [
               'responsetext' => 'error',
               'error' => 'Customer vault creation failed',
           ];
       }

       parse_str($createVaultResponse, $vaultResponseArray);
       return isset($vaultResponseArray['customer_vault_id']) ? $vaultResponseArray['customer_vault_id'] : null;
   }

This method takes in credit card details, formats them, and sends a CURL request to the NMI API to add the customer to the vault. If the application is in test mode, it returns a mock response. The method logs the response using Laravel's standard logging mechanism.