<?php
/***********************************************************
DoDirectPaymentReceipt.php

Submits a credit card transaction to PayPal using a
DoDirectPayment request.

The code collects transaction parameters from the form
displayed by DoDirectPayment.php then constructs and sends
the DoDirectPayment request string to the PayPal server.
The paymentType variable becomes the PAYMENTACTION parameter
of the request string.

After the PayPal server returns the response, the code
displays the API request and response in the browser.
If the response from PayPal was a success, it displays the
response parameters. If the response was an error, it
displays the errors.

Called by DoDirectPayment.php.

Calls CallerService.php and APIError.php.

***********************************************************/
include_once 'func.php';
require_once 'CallerService.php';
session_start();


/**
 * Get required parameters from the web form for the request
 */
$cardid =urlencode( $_POST['cardid']);
$paymentType =urlencode( $_POST['paymentType']);
$firstName =urlencode( $_POST['firstName']);
$lastName =urlencode( $_POST['lastName']);
$creditCardType =urlencode( $_POST['creditCardType']);
$creditCardNumber = urlencode($_POST['creditCardNumber']);
$expDateMonth =urlencode( $_POST['expDateMonth']);

// Month must be padded with leading zero
$padDateMonth = str_pad($expDateMonth, 2, '0', STR_PAD_LEFT);

$expDateYear =urlencode( $_POST['expDateYear']);
$cvv2Number = urlencode($_POST['cvv2Number']);
$address1 = urlencode($_POST['address1']);
$city = urlencode($_POST['city']);
$state =urlencode( $_POST['state']);
$zip = urlencode($_POST['zip']);
$amount = urlencode($_POST['amount']);
//$currencyCode=urlencode($_POST['currency']);
$currencyCode="CAD";
$paymentType=urlencode($_POST['paymentType']);

/* Construct the request string that will be sent to PayPal.
   The variable $nvpstr contains all the variables and is a
   name value pair string with & as a delimiter */
$nvpstr="&PAYMENTACTION=$paymentType&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber&EXPDATE=".         $padDateMonth.$expDateYear."&CVV2=$cvv2Number&FIRSTNAME=$firstName&LASTNAME=$lastName&STREET=$address1&CITY=$city&STATE=$state".
        "&ZIP=$zip&COUNTRYCODE=US&CURRENCYCODE=$currencyCode";



/* Make the API call to PayPal, using API signature.
   The API response is stored in an associative array called $resArray */
$resArray=hash_call("doDirectPayment",$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") {
    $_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);
                }
               $cardid='';
        	}
}

?>

<html>
    <head>
        <title>PayPal PHP SDK - DoDirectPayment API</title>
        <link href="sdk.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <br>
        <center>
            <font size=2 color=black face=Verdana><b>Do Direct Payment</b></font>
            <br><br>

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

            <table width=400>

                <table class="api" width=400 height="400">
                    <?php
                     //var_dump($resArray);
                     echo "<tr><td>"."TIMESTAMP"."</td>";
                     echo "<td>".$resArray['TIMESTAMP']."</td></tr>";
                     echo "<tr><td>"."ACK"."</td>";
                     echo "<td>".$resArray['ACK']."</td></tr>";
                     echo "<tr><td>"."AMT"."</td>";
                     echo "<td>".$resArray['AMT']."</td></tr>";
                     echo "<tr><td>"."CURRENCYCODE"."</td>";
                     echo "<td>".$resArray['CURRENCYCODE']."</td></tr>";
                     //echo "<tr><td>"."TRANSACTIONID"."</td>";
                    // echo "<td>".$resArray['TRANSACTIONID']."</td></tr>";
                     
                    /*foreach($resArray as $key => $value) {

                        echo "<tr><td> $key:</td><td>$value</td>";
                    }*/
                    ?>
                </table>

            </table>
            <br>
            <br>
            <br>
            <br>
            <br>
            <a href="http://www.smashhit.org/">Return To Business Card</a>
        </center>

    </body>

</html>


