Stripe - The requested address was not found on this server

Author - Varun Chopra

Stripe Payment Gateway Integration – The requested address was not found on this server

Stripe Payment Issue while capturing payment:

I was implementing Stripe payment in one of my cake3 project. My Stripe Charge Capture Request was:

1) Check out Form :

<div class="col-sm-12 nopadd mrgn-10">
 <div class="container">
 <div class="page-content">
 <h3><?= __('Payment') ?></h3>
 <div class="col-sm-12 header-one">
  <form action="/Stripe/payment" method="POST">
 <script  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
	  data-key=<?php echo $publisherKey;?>
	  data-amount="999"
	  data-name="Stripe.com"
	  data-description="Example charge"
	 data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
 data-locale="auto"
	 data-zip-code="true">
 </script>
 </form>
</div>						
</div>
</div>
</div>	
</div>	
<div class="clearfix"></div>

action="/Stripe/payment" - is the call back url where stripe will redirect after payment.

2) Controller Action for payment:

StripeStripe::setApiKey($stripePaymentKey);
$response = StripeCharge::create(array(
			"amount" => 2000,
			"currency" => "eur",
			'source' => $stripeToken,
			"description" => "Charge for test.sharma@test.com",
			"capture"=>false, //set this to true if you immediately want to capture it
			));
$response = $response ->jsonSerialize();
			
 $stripePaymentKey - Stripe gateway secret key 
 $stripeToken - Token provided by the Stripe gateway after submitting card details.

I got Stripe token to capture payments successfully. After that I hit the above action. Payment captured successfully but in the response instead of returning to the return url I was getting below error in the browser (In this case Stripe is the controller name and payment is the method name where control should return. In your case this path will be different.):
Stripe Payment Gateway Integration

Solution :
After inspecting the response of the above request using Inspect element, I found the actual cause of the issue.

Stripe Payment Gateway Integration

Stripe Payment Gateway Integration

CSRF token is sent by cakephp in each of its request from view to controller to find out that each time request is coming from same origin or not. It doesn’t allow request from other origin, as in our case. To resolve the issue, add below code in your controller’s beforeFilter method:

public function beforeFilter(Event $event){
		parent::beforeFilter($event);
		$this->Auth->allow(['payment']);
		if (in_array($this->request->action, ['payment'])) {
			$this->eventManager()->off($this->Csrf);
		}
	}

After making the above change it worked fine and I returned back to the
Correct location by stripe.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *