<?php
/**********************************************************
DoExpressCheckoutPayment.php

This functionality is called to complete the payment with
PayPal and display the result to the buyer.

The code constructs and sends the DoExpressCheckoutPayment
request string to the PayPal server.

Called by GetExpressCheckoutDetails.php.

Calls CallerService.php and APIError.php.

**********************************************************/
include_once 'func.php';
require_once 'CallerService.php';

session_start();


ini_set('session.bug_compat_42',0);
ini_set('session.bug_compat_warn',0);

/* Gather the information to make the final call to
   finalize the PayPal payment.  The variable nvpstr
   holds the name value pairs
*/
$token =urlencode( $_SESSION['token']);
$paymentAmount =urlencode ($_SESSION['TotalAmount']);
$paymentType = urlencode($_SESSION['paymentType']);
$currCodeType = urlencode($_SESSION['currCodeType']);
$payerID = urlencode($_SESSION['payer_id']);
$serverName = urlencode($_SERVER['SERVER_NAME']);

$cardid = $_SESSION['cardid'];

$nvpstr='&TOKEN='.$token.'&PAYERID='.$payerID.'&PAYMENTACTION='.$paymentType.'&AMT='.$paymentAmount.'&CURRENCYCODE='.$currCodeType.'&IPADDRESS='.$serverName ;



/* Make the call to PayPal to finalize payment
    If an error occured, show the resulting errors
*/
$resArray=hash_call("DoExpressCheckoutPayment",$nvpstr);

/* Display the API response back to the browser.
   If the response from PayPal was a success, display the response parameters'
   If the response was an error, display the errors received using APIError.php.
*/
$ack = strtoupper($resArray["ACK"]);


if($ack != 'SUCCESS' && $ack != 'SUCCESSWITHWARNING') {
    $_SESSION['reshash']=$resArray;
    $location = "APIError.php";
    header("Location: $location");
}else {
    if (!empty($cardid) && is_numeric($cardid)) {
        $qry = 'select count(*) as cnt from usercard where usercard_ = ' . mysql_escape_string($cardid);
        $res = mysql_query($qry) or die(mysql_error());
        if (($row=mysql_fetch_assoc($res))!==false && isset($row['cnt'])) {
            $qry = 'insert into job (usercard_, postdate, status) values (' . mysql_escape_string($cardid) . ', \'' . mysql_escape_string(date('Y-m-d H:i:s')) . '\', \'pending\')';
            $res = @mysql_query($qry);
        
//           echo 'query executed, cardid not null';
        }
        $cardid='';

    }
    else
    {
//        echo $_SESSION['cardid'];
//        echo 'query not executed, cardid null';
    }
    
}

?>


<html>
    <head>
        <title>PayPal PHP SDK - DoExpressCheckoutPayment API</title>
        <link href="sdk.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <img alt="Smashhit Solutions Online Design Center" src="ban.jpg">
        <br>
        <center>
            <font size=2 color=black face=Verdana><b></b></font>
            <br><br>

            <b>Order Processed! Thank you for your payment!</b><br><br>


            <table width =400>

                <?php
                require_once 'ShowAllResponse.php';
                ?>
            </table>
        </center>
        <a href="http://www.smashhit.org/">Return To Business Card</a>
    </body>
</html>