Guest User

PaymentSettings.php

a guest
Dec 16th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.43 KB | None | 0 0
  1. <?php
  2. //TODO: Set the following constants. All URLs must be publicly accessible.
  3. // See http://wyday.com/limelm/help/how-to-generate-product-keys-after-order/
  4. // to learn how to configure the payment form and set everything up.
  5.  
  6. //==== General Config ====
  7.  
  8. //TODO: disable debug & sandbox when you're using this on your live site.
  9.  
  10. // Enable debug logging
  11. $debug = true;
  12. $debug_log = dirname(__FILE__).'/payment_debug.log';
  13.  
  14. // api key found in http://wyday.com/limelm/settings/
  15. $LimeLM_ApiKey = 'API KEY';
  16.  
  17. // the version id is found in the url.
  18. // For example http://wyday.com/limelm/version/100/   the version id is '100'.
  19. $LimeLM_VersionID = 'ID';
  20.  
  21. function debug_log($message, $success, $end = false)
  22. {
  23.     global $debug, $debug_log;
  24.  
  25.     if (!$debug || !$debug_log)
  26.         return;
  27.  
  28.     // Timestamp
  29.     $text = '['.date('m/d/Y g:i A').'] - '.(($success)?'SUCCESS :':'FAILURE :').$message. "\n";
  30.  
  31.     if ($end)
  32.         $text .= "\n------------------------------------------------------------------\n\n";
  33.  
  34.     // Write to log
  35.     $fp=fopen($debug_log, 'a');
  36.     fwrite($fp, $text);
  37.     fclose($fp);
  38. }
  39.  
  40. function SendPKeys($quantity, $email, $first, $last, $CompanyName, $AppName)
  41. {
  42.     //Note: we put LimeLM in this directory. Change it as needed.
  43.     require('LimeLM.php');
  44.  
  45.     global $LimeLM_VersionID, $LimeLM_ApiKey;
  46.  
  47.     $errors = false;
  48.  
  49.     // set your API key
  50.     LimeLM::SetAPIKey($LimeLM_ApiKey);
  51.  
  52.     try
  53.     {
  54.         // Generate the product key - set the number of activations using the quantity
  55.         $xml = new SimpleXMLElement(LimeLM::GeneratePKeys($LimeLM_VersionID, 1, $quantity, $email));
  56.  
  57.         if ($xml['stat'] == 'ok')
  58.         {
  59.             foreach ($xml->pkeys->pkey as $pkey)
  60.             {
  61.                 // add a newline if you're generating more than one key
  62.                 if ($product_keys)
  63.                     $product_keys .= "\r\n";
  64.  
  65.                 // set the product key
  66.                 $product_keys .= $pkey['key']."\r\n";
  67.             }
  68.         }
  69.         else //failure
  70.         {
  71.             // use the error code & message
  72.             debug_log('Failed to generate product keys: ('.$xml->err['code'].') '.$xml->err['msg'],false);
  73.         }
  74.     }
  75.     catch (Exception $e)
  76.     {
  77.         debug_log('Failed to generate product keys, caught exception: '.$e->getMessage(),false);
  78.     }
  79.  
  80.     if (empty($product_keys))
  81.     {
  82.         // the product key didn't generate, don't send e-mail to the customer yet.
  83.         $errors = true;
  84.     }
  85.  
  86.     // form the customer name
  87.     $customerName = $first;
  88.  
  89.     if (!empty($last))
  90.         // append the last name
  91.         $customerName .= ' '.$last;
  92.  
  93.     $emailBody = $customerName.',
  94.  
  95.    Thank you for your order. Your product key is:
  96.  
  97. '.$product_keys.'
  98.  
  99.  
  100. This product key is licensed for '.$quantity.( $quantity > 1 ? ' users' : ' user' ).' of '.$AppName.'.
  101.  
  102. Thank you,
  103.  
  104. '.$CompanyName;
  105.  
  106.     if (!empty($product_keys))
  107.     {
  108.         // Send Email to the buyer
  109.         $emailSent = mail($email, 'Your '.$AppName.' product key', $emailBody, $headers);
  110.     }
  111.  
  112.     // generate an array you can insert into your database
  113.     $new_order_info = array(
  114.         'quantity_licenses'     => $quantity,
  115.         'pkey'                  => $product_keys,
  116.         'pkey_emailed'          => $emailSent ? '1' : '0', // 0 = no, 1 = yes
  117.         'first_name'            => $first,
  118.         'last_name'             => $last,
  119.         'email'                 => $email
  120.     );
  121.  
  122.     //TODO: delete logging, or log to a safe location (not accessible to outside world)
  123.     debug_log('TODO: Insert array into db: '."\r\n\r\n".print_r($new_order_info, true), true);
  124.  
  125.     if (!$emailSent)
  126.     {
  127.         $errors = true;
  128.         debug_log('Error sending product Email to '.$email.'.',false);
  129.     }
  130.  
  131.     LimeLM::CleanUp();
  132.    
  133.     return $product_keys;
  134. }
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment