1
votes

i am trying to use ACH stripe getaway without palid third party to get manually account number ,routing number ,account holder name nd account type stripe.js/v3 create bank token i use this token create customer and then create charge all thing is done but when we test using stripe testng account num and reuting number this give me fatal error "this account cannot create payment"

this is my onclick button function that create abnk token
function test1(){
  
   var one_coach = $('input.checkbox-coaches').is(':checked');
                if(one_coach == false){
                  alert('Please Choose at least one coach');
                  return false;
                }
                  var one_athlete = $('input.checkbox-athletes').is(':checked');
            if(one_athlete == false){
              alert('Please Choose at least one athlete');
              return false;
            }
       
                   //create single-use token to charge the use
 var stripe = Stripe('pk_test_UM0qUCRVFnrem3uAIGtWcPiJ');
                   //create single-use token to charge the use
stripe.createToken('bank_account', {
 country: 'US',
  currency: 'usd',
  routing_number: $('#routing_number').val(),
  account_number: $('#account_number').val(),
  account_holder_name: $('#account_holder_name').val(),
  account_holder_type: $('#account_holder_type').val(),
}).then(function(result) {

 //console.log(result);
 if (result.error) {
                //enable the submit button
              
                $('#payBtn').removeAttr("disabled");
                //display the errors on the form
                // $('#payment-errors').attr('hidden', 'false');
                $('#payment-errors').addClass('alert alert-danger');
                $("#payment-errors").html(result.error.message);
            } else {
                var form$ = $("#order_form");
                //get token id
                 var token_ach = result.token.id;
                 var bank_account_id=result.token.bank_account.id;

                 //alert(token_ach);
                //insert the token into the form
                form$.append("<input type='hidden' id='bank_account' name='bank_account' value='" + bank_account_id + "' />");
              form$.append("<input type='hidden' id='stripeToken' name='stripeToken' value='" + token_ach + "' />");
                //submit form to the server
               //  form$.get(0).submit();
       
               function ach(){
  var charges = $( "#service_charges" ).text(); 
  var formdata=$('#order_form').serialize()+ '&charges=' + charges;
   
      
  $('#ach_button').attr('disabled','disabled').attr('value','Ordering...'); 
  
  $.ajax({
    type : 'POST',
    url :   'register/ach',
    data : formdata,
    beforeSend: function() {
              $("#loader").show();
           },
    success:
      function(data){
        
        $('#results').removeClass('hide');
        
        var result = JSON.parse(data);
          

        if(result.error == 100)
        {
           $("#loader").hide();
          $('#alert_msg').addClass('alert-error');
          $('#results h4').text('On snap!');
          $('#results p').text(result.msg);
          $('#place_order').removeClass('disabled').attr('value','Place Order').removeAttr('disabled');
          
        }

        else
        {
          $("#loader").hide();
          $('#alert_msg').removeClass('alert-error').addClass('alert-success');
          $('#results h4').text('Congratulations!');
          $('#results p').html(result.msg + '<br/>An email confirmation has been sent to you. You can visit the <a href="/account">My Account</a> area to view your invoice.');
 
 return false;
        }
        
         return false;
         
      }
  });


}

                 
               // $('#stripeToken').val(token);
            }
});
               // var token = $('#stripeToken').val();
               // alert(token); return;
               //  //submit from callback
               //   process_order(token);
             
}



php code that get all data 
public function ach()
{
  $post = array_2_obj($this->input->post());
 if(!empty($post->stripeToken))
    {  

    $contact_id = $this->club->infusion_contact_id;

    $post->event = $this->events_model->get('events',array('id' => $post->event_id));
 
    $attending = $this->get_attending_details($post->event_id,$this->input->post('athletes'));
 //echo "<pre>";print_r($attending); 
    $teams = $this->get_team_comps($post->event_id,$attending);
 // echo "<pre>";print_r($teams);
     $this->session->unset_userdata("register_{$post->event_id}");
 

    /**
     * Check if Order has already been created by Session register_{event_id} Create Blank Order inside Infusionsoft
     */
 
    if(!isset($this->session->userdata["register_{$post->event_id}"])):
      $invoice_id = $this->infusion->create_order($contact_id,$post,$attending,$teams);
      $this->session->set_userdata("register_{$post->event_id}",$invoice_id);
    else :
      $invoice_id = $this->session->userdata["register_{$post->event_id}"];
    endif;
 


    /**
     * Charge Invoice
     */
    // $merchant_id = $this->db_model->get_setting_value('merchant_id');

   


    /**
     * Unset the Invoice Id
     */
    //$this->session->unset_userdata("register_{$post->event_id}");
//print_r($post); exit;
    //  echo "<pre>";print_r($_POST); 
      //get token, card and user info from the form
      $bank_a_token  = $post->stripeToken;
      $account_holder_name = $post->account_holder_name;
     // $email = $_POST['email'];
      $routing_number = $post->routing_number;
      $account_number =$post->account_number;
    //  $bank_account_id=$post->bank_account;
      //include Stripe PHP library
        require_once APPPATH."third_party/stripe/init.php";
      
      //set api key
      $stripe = array(
        "secret_key"      => "sk_test_HfRFUzMnvuq1rY1CfBZZUgKv",
        "publishable_key" => "pk_test_UM0qUCRVFnrem3uAIGtWcPiJ"
      );
      
      \Stripe\Stripe::setApiKey($stripe['secret_key']);
$create_customer=\Stripe\Customer::create(array(
  "source" => $bank_a_token,
  "description" => $account_holder_name
));
 //print_r($create_customer); exit;
  $customer_id=$create_customer->id;
  $source=$create_customer->default_source;


//get the existing bank account

$customer = \Stripe\Customer::retrieve($customer_id);
$bank_account = $customer->sources->retrieve($source);

// verify the account
$bank_account->verify(array('amounts' => array(32, 45)));
 //print_r($bank_account); exit;
       $label="";
         $price=0;
          $teamprice=0;
          $teamlable="";
      //item information
      foreach ($attending as $product) {
       
         $label.=$product->label.",";
      $price=($product->total)+$price;

      }

      if (!empty($teams)) {
       foreach ($teams as $tprice) {

   $teamlable.=$tprice->label.",";
         $teamprice=$tprice->team_price;
      }

      }

 $charges=$post->charges;
$new_price=$price+$teamprice+$charges;
      $itemName = $label;
      $itemNumber = $post->event->product_id;
      $itemPrice = $new_price*100;
      $currency = "usd";
      $orderID = $post->event->action_set_id;
     //print_r($customer);

     
 
    
  try {
  $charge = \Stripe\Charge::create(array(
  "amount" => $itemPrice,
  "currency" => $currency,
  "customer" => $customer_id // Previously stored, then retrieved
));
    
} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  // param is '' in this case
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");

} catch (\Stripe\Error\RateLimit $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
  $msg = array('error' => 100,'msg' => "this account cannot create payment  ");
    echo json_encode($msg);
   exit;
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  $msg = array('error' => 0,'msg' => "this account cannot create payment  ");
    echo json_encode($msg);
   exit;
  // Something else happened, completely unrelated to Stripe
}

      //retrieve charge details
      $chargeJson = $charge->jsonSerialize();
      //check whether the charge is successful
      
      if($chargeJson['amount_refunded'] == 0  && $chargeJson['paid'] == 1 && $chargeJson['captured'] == 1)
       {
        //order details 
       
        $amount = $chargeJson['amount'];
        $balance_transaction = $chargeJson['balance_transaction'];
        $currency = $chargeJson['currency'];
        $status = $chargeJson['status'];
        $date = date("Y-m-d H:i:s");
      
        
        //insert tansaction data into the database
        $data = new stdClass();
    $data->attending_details = $attending;
    $data->post = $post;
    $data->teams = $teams;
    $data->club = new stdClass();
    $data->club = $this->club;

    $data->bank = new stdClass();
    $data->bank->last4 = substr($post->account_number,-4);
    $data->bank->account_holder_type = $post->account_holder_type;
    $data->bank->account_number = $post->account_number;
    $data->bank->routing_number = $post->routing_number;
    // $data->card->stripeToken=$post->stripeToken;
 
    unset($data->post->account_number);
    unset($data->post->routing_number);



    /**
     * Add Coaches and Athlete Data so it does not get deleted
     */
    $data->coaches = array();
    foreach($data->post->coaches as $coach_id):
      if($coach_id > 0):
        $data->coaches[] = $this->coaches_model->get(array('id' => $coach_id));
      endif;
    endforeach;


    $data->athletes = array();
    foreach($data->post->athletes as $athlete_id):
      if($athlete_id > 0):
        $data->athletes[] = $this->athletes_model->get(array('id' => $athlete_id));
      endif;
    endforeach;
    //Insert Data into Database
    $order_data = $this->db_model->add_order($this->club->id,$invoice_id,$data->post->event_id,$data);
              $pdf_base64 = execute_curl(base_url() . 'invoice/base/' . $order_data->key,array(),TRUE);
 
    // if(strlen($pdf_base64) > 10 ):
    //   $file_name = $data->post->event->name . ' - ' . $data->club->club . ' Invoice.pdf';
    //   $this->infusion->uploadFile($file_name,$pdf_base64,$contact_id);
    // endif;
   
    // //Update Invoice Key and then send email
    // $this->infusion->updateCon($contact_id,array('_InvoiceKey' => $order_data->key));
    // $this->infusion->sendTemplate(array($contact_id),$this->db_model->get_setting_value('invoice_email_template'));
    // $this->infusion->sendTemplate(array($contact_id),$this->db_model->get_setting_value('admin_notification_email_template'));

    // $global_action_set = $this->db_model->get_setting_value('all_events_action_set_id');
    // if(isset($global_action_set) && $global_action_set > 0):
    //   $this->infusion->runAS($contact_id,$global_action_set);
    // endif;
}
        
   
         // $msg = array('error' => 0,'msg' => "You have successfully registered your club for " .  $data->post->event->name. ' Event');
 $msg = array('error' => 0,'msg' => "You have successfully registered your club for " . "$chargeforach->". ' Event');
    echo json_encode($msg);
   exit;
    }
      else
      {
       $msg = array('error' => 100,'msg' => "Invalid TOken");
    echo json_encode($msg);
    exit;
      }






}
1
Did you manage to solve the issue ?slothy

1 Answers

0
votes

Are you based in US? Currently according to the documents. It only supports ACH charges in the US

https://stripe.com/docs/ach