Text Size

Code Examples

Setting the default date to GMT in a CFWheels ColdFusion App

I've been working on an app in ColdFusion for a client that needs to use dates/times across multiple countries, and thought I'd post this in case anyone else needs it. We are using CFWheels and I needed a way to ensure that the dates the framework automatically creates for the createdat and updatedat fields are in GMT time. There are a couple of  ways to do this, the first being to set your ColdFusion JVM settings to include a default timezone using an argument like "-Duser.timezone=GMT" in the JVM arguments fields of the CF Administrator. However, this will then set it for all apps on the ColdFusion server. If you only need it for the one CFWheels app, you can override the following method in your Model.cfc class:

<!--- Override the default timestamping with the GMT time --->
<cffunction access="public" name="$timestampProperty" output="false" returntype="void">
        <cfargument name="property" required="true" type="string">
                        this[arguments.property] = DateConvert(&quot;local2utc&quot;, now());

This will ensure that all date values saved to the database will useUTC/GMT time.  

Doculicious PayPal IPN

        //Your PayPal Seller email address (Change to your PayPal sandbox email for testing)
        $paypal_email = "chris@digitalcarpenter.com.au";
        //The currency you use for your PayPal buttons
        $currency = "USD";
        //The cost of your form. Make sure you add the cents
        $cost = "10.00";
        //The email address used to send you notification emails
        $from_email = "email@YOURDOMAIN.com";
        //The email address used to receive notification emails on success or failure
        $notification_email = "notifyme@YOURDOMAIN.com";
        //Your API access ID and Security key from the API link on the dashboard 
        $api_access_id = "xxxxxxxxxxxxxxxx";
        $api_security_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        //Send this back to PayPal to validate the fields
        $req = 'cmd=_notify-validate';
        //Add each field to a value we will post back to PayPal
        foreach ($_POST as $key => $value) 
                $value = urlencode(stripslashes($value));
                $req .= "&$key=$value";
        //Post back to PayPal
        $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
        //PayPal Production
        $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
        //PayPal Development - uncomment for testing
        //$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
        if (!$fp) 
                //Send an email on failure with the error message (errstr) from paypal
                $from = "From: " . $from_email;
                $to = $notification_email;
                $subject = "HTTP ERROR";
                $body = $errstr;
                mail($to, $subject, $body, $from);      
                fputs ($fp, $header . $req);
                while (!feof($fp)) {
                        $res = fgets ($fp, 1024);
                        if (strcmp ($res, "VERIFIED") == 0) 
                                //These are only the fields required for this simple purchase. See the following page for all variables you can get at:
                                //The custom field contains the unique Doculicious Document ID of the completed form and the item_number contains the Template ID.
                                $item_number = $_POST['item_number'];
                                $document_uid = $_POST['custom'];
                                $item_name = $_POST['item_name'];
                                $payment_status = $_POST['payment_status'];
                                $payment_amount = $_POST['mc_gross']; 
                                $payment_currency = $_POST['mc_currency'];
                                $txn_id = $_POST['txn_id'];
                                $receiver_email = $_POST['receiver_email'];
                                $payer_email = $_POST['payer_email'];
                                //Check that all the variables are valid        
                                if (($payment_status == 'Completed') &&                 //check the payment_status is Completed
                                        ($receiver_email == $paypal_email) &&           //check that receiver_email is your Primary PayPal email
                                        ($payment_amount == $cost ) &&                  //check they payed what they should have
                                        ($payment_currency == $currency))               //check that payment_amount/payment_currency are correct 
                                        //OK, everything is fine, so now process their order.
                                        //We are going to get the completed PDF from Doculicious and save it to our website and then send the purchaser an email with a link to where they can download it.                                     
                                        //The full authorization string - needed for the header. API access and security codes are set at the top of the page
                                        $auth = "authorization:DCS " . $api_access_id . ":" . $api_security_key;
                                        //This is the URL to get completed the completed PDF for a specific entry. 
                                        $resource_uri = "https://api.doculicious.com/entries/" . $item_number . "/" . $document_uid . ".pdf";
                                        //Initialize curl and set URL and other appropriate options
                                        $ch = curl_init();
                                        curl_setopt($ch, CURLOPT_URL, $resource_uri);
                                        //Send the authorization details in the header
                                        curl_setopt($ch, CURLOPT_HTTPHEADER, array($auth));
                                        //This tells curl to return the actual result of the API call (ie, the PDF or XML error message). Returns False if HTTPS get fails.
                                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
                                        //SSL Settings
                                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                                        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
                                        //Execute the API call
                                        $output = curl_exec($ch);
                                                $info = curl_getinfo($ch);
                                                //Use this for debugging if needed
                                                //echo("<pre>" . var_export($info, true) . "</pre>");
                                                //200 means success.
                                                if($info['http_code'] == 200)
                                                        //Save the PDF file to our website
                                                        $my_file = "/YOUR/WEB/ROOT/customer-files/" . $document_uid . ".pdf";
                                                        $fh = fopen($my_file, 'wb') or die("can't open file");
                                                        fwrite($fh, $output);
                                                        //Create the URL where the customer can download their PDF
                                                        $download_url = "http://www.YOURDOMAIN.com/files/" . $document_uid . ".pdf";
                                                        //Send an email to the purchaser with a link to download the PDF file
                                                        $from = "From: " . $from_email;
                                                        $to = $payer_email;
                                                        $subject = "Your PDF form is ready.";
                                                        $body =  'Thank you for your purchase. Please use the link below to download your PDF file:' . PHP_EOL . PHP_EOL .  
                                                                         $download_url . PHP_EOL . PHP_EOL .  
                                                                         'Regards,' . PHP_EOL .
                                                                         'YOUR NAME' . PHP_EOL;
                                                        mail($to, $subject, $body, $from); 
                                                else //there was an error, so output the content
                                                        header("Content-type: " . $info['content_type']);
                                                        echo $output;
                                        // close cURL resource, and free up system resources
                                        //Send an email to yourself with details of the purchase
                                        $meFrom = "From: " . $from_email;
                                        $meTo = $notification_email;
                                        $meSubject = "PayPal order completed.";
                                        $meBody =  'Someone just purchased a PDF document using our Doculicious form. Here are the details:' . PHP_EOL . PHP_EOL .  
                                                           'Item Name: ' . $item_name . PHP_EOL .  
                                                           'Item Number: ' . $item_number . PHP_EOL .  
                                                           'Document UID: ' . $document_uid . PHP_EOL . 
                                                           'Price: ' . $payment_amount . PHP_EOL . 
                                                           'Transaction ID: ' . $txn_id . PHP_EOL . 
                                                           'Purchaser Email: ' . $payer_email . PHP_EOL;
                                        mail($meTo, $meSubject, $meBody, $meFrom);           
                                        //Something went wrong, so email yourself with the details
                                        $meFrom = "From: " . $from_email;
                                        $meTo = $notification_email;
                                        $meSubject = "PayPal IPN failed.";
                                        $meBody =  'Something went wrong with a PayPal order. Here are the details of the transaction:' . PHP_EOL . PHP_EOL .  
                                                           'Item Name: ' . $item_name . PHP_EOL .
                                                           'Item Number: ' . $item_number . PHP_EOL .   
                                                           'Document UID: ' . $document_uid . PHP_EOL . 
                                                           'Price: ' . $payment_amount . PHP_EOL . 
                                                           'Transaction ID: ' . $txn_id . PHP_EOL .
                                                           'Payment Status: ' . $payment_status . PHP_EOL . 
                                                           'Currency: ' . $payment_currency . PHP_EOL . 
                                                           'Receiver Email: ' . $receiver_email . PHP_EOL . 
                                                           'Purchaser Email: ' . $payer_email . PHP_EOL;
                                        mail($meTo, $meSubject, $meBody, $meFrom);
                        else if (strcmp ($res, "INVALID") == 0) 
                                //Paypal didn't like what we sent.
                                $meFrom = "From: " . $from_email;
                                $meTo = $notification_email;
                                $meSubject = "PayPal - Invalid IPN";
                                $meBody = "We have had an INVALID response. Check that PayPal hasn't changed their IPN API.";
                                mail($meTo, $meSubject, $meBody, $meFrom);
                fclose ($fp);

Doculicious PayPal Form

                <title>PayPal Form</title>
                //Check that the doculicious URI is set - It should be
                        $uri = $_GET['dclcs_entry_uri'];
                        //Put all the parts of the URI into an array
                        $uri_parts = explode("/", $uri);
                        //Get the last item in the array. This is the uniqeID of the submitted form 
                        $document_id = array_pop($uri_parts);
                <!-- Display a message to the customer. -->
                <div style="width: 450px; margin: 0 auto;">
                        <p>Thank you for using our service. Please click the button below to pay for your form using PayPal. Once confirmed we will send through a link where you can download the completed PDF file.</p>
                        <div align="center">
                                <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
                                        <input type="hidden" name="cmd" value="_s-xclick">
                                        <input type="hidden" name="hosted_button_id" value="957FC8G74KTR4">
                                        <input name="custom" type="hidden" value="<?php echo($document_id);?>" />
                                        <input type="image" src="https://www.sandbox.paypal.com/WEBSCR-640-20110429-1/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
                                        <img alt="" border="0" src="https://www.sandbox.paypal.com/WEBSCR-640-20110429-1/en_US/i/scr/pixel.gif" width="1" height="1">