$products[$i]['name'] = $model; $products[$i]['price'] = $details['price']; $products[$i]['quantity'] = $details['quantity']; $total = $total + $products[$i]['quantity'] * $products[$i]['price']; //adding the total price $i++; } } } if ($total == 0) { echo 'You have not selected any item! Click \'Add to cart\' to add your items.<br><a href=\'http://teststore.site90.net/testsite//main.php\'>Click here to go back<a>'; } else { $params = array('RETURNURL' => "http://teststore.site90.net/testsite/accept.php", 'CANCELURL' => "http://teststore.site90.net/testsite/decline.php", 'PAYMENTREQUEST_0_PAYMENTACTION' => "Sale", 'PAYMENTREQUEST_0_AMT' => $total); //adding fields required for the API request foreach ($products as $key => $product) { $params['L_PAYMENTREQUEST_0_NAME' . $key] = $product['name']; $params['L_PAYMENTREQUEST_0_AMT' . $key] = $product['price']; $params['L_PAYMENTREQUEST_0_QTY' . $key] = $product['quantity']; } $obj = new details(); $response_arr = $obj->apimethod('SetExpressCheckout', $params); //calling the SetExpressCheckout API method. This is the first API request. if ($response_arr) { header("Location: https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=" . $response_arr['TOKEN']); } else { echo 'Error'; } } ?>
<?php session_start(); include 'details.php'; $params = array('TOKEN' => $_GET['token']); //get token from url $obj = new details(); $response_arr1 = $obj->apimethod('GetExpressCheckoutDetails', $params); //getting details of the check out. This is the 2nd API request. if ($response_arr1) { $params['PAYERID'] = $_GET['PayerID']; //get payer id from url $params['PAYMENTREQUEST_0_PAYMENTACTION'] = "Sale"; $params['PAYMENTREQUEST_0_AMT'] = $response_arr1['PAYMENTREQUEST_0_AMT']; $response_arr2 = $obj->apimethod('DoExpressCheckoutPayment', $params); //doing out the check out. This is the 3rd and last API request. if ($response_arr2) { echo '<img src="https://image.freepik.com/free-photo/green-tick-in-circle_21335495.jpg" width="200" align="middle" height="200">'; echo ' Congratulations! Your payment is successful. '; } else { echo 'error2'; } } else { echo 'Error1'; } echo '<a href="http://teststore.site90.net/testsite/main.php">Click here to go back<a>'; session_unset(); //closing session so that the cart becomes empty again session_destroy();