Capture order returning 404 HTTP error

rodrigomsr
Contributor
Contributor

Hello, I'm trying to create an order and capture the payment (I don't know if this is the correct way to do it). However, when I try to capture the order, I simply get a 404 error. I am doing this in the sandbox environment.

 

First, I create an order and use the approval link to pay. Then, when the payer accesses application_context.return_url I try to capture the order from the "token" GET parameter.

 

Code (PHP):

 

<?php
	$clientId = "xxxx";
	$secret = "xxxx";


	//Get Authorization Token
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

	$result = curl_exec($ch);
	$err = curl_error($ch);

	$access_token = FALSE;

	if($err)
	{
		echo "cURL Error #:" . $err;
	}
	else
	{
		$json = json_decode($result);
		// print_r($json->access_token);
		$access_token = $json->access_token;
		echo "<br>Access token success<br><br>";
	}

	curl_close($ch);


	


	//Capture order
	if(isset($_GET['token']))
	{
		$token = $_GET['token'];
		
		$ch = curl_init();
		
		curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v2/checkout/orders/$token/capture");
		curl_setopt($ch, CURLOPT_HTTPHEADER,
			array(
				"Content-Type: application/json",
				"Authorization: Bearer $access_token",
				"Prefer: return=representation"
			)
		);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		
		$result = curl_exec($ch);
		$err = curl_error($ch);

		if($err)
		{
			echo "cURL Error #:" . $err;
		}
		else
		{
			$json = json_decode($result);
			print_r($json);
		}
		
		echo "<br><br><br>Order capture. HTTP: ".curl_getinfo($ch, CURLINFO_HTTP_CODE);
		
		curl_close($ch);
		exit();
	}






	//Create order
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v2/checkout/orders");
	curl_setopt($ch, CURLOPT_HTTPHEADER,
		array(
			"Content-Type: application/json",
			"Authorization: Bearer $access_token"
		)
	);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, '
	{
		"intent": "CAPTURE",
		"purchase_units": [
			{
				"description": "VIP - 30 dias",
				"amount": {
					"currency_code": "BRL",
					"value": "7.00"
				}
			}
		],
		"application_context": {
			"brand_name": "STT Server",
			"user_action": "PAY_NOW",
			"return_url": "http://localhost/paypal.php",
			"shipping_preference": "NO_SHIPPING"
		}
	}
	');
	
	$result = curl_exec($ch);
	$err = curl_error($ch);

	if($err)
	{
		echo "cURL Error #:" . $err;
	}
	else
	{
		$json = json_decode($result);
		print_r($json);
		echo "<br><br>Link: ".$json->links[1]->href;
		echo "<br><br><br>";
		$href_link = $json->links[0]->href;
	}

	curl_close($ch);

 

Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.