Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. Example PHP script
  2.  
  3. This example script should give you an idea on how to use the PayCo.re IPN.
  4. It supports multiple products and sorts them by their IPN secret.
  5.  
  6.  
  7. /* This example demonstrates how to use the PayCo.re IPN for multiple products.
  8. ** This could be used for multiple license types (different durations).
  9. ** It utilizes mysqli as it is more easy to understand for
  10. ** inexperienced programmers. */
  11.  
  12. //Database Configuration
  13. $dbhost = "localhost";
  14. $dbname = "dbname";
  15. $dbuser = "dbuser";
  16. $dbpass = "dbpass";
  17.  
  18. //Get default variables
  19. $transactionID = getPostVariable('tx_id');
  20. $ipnSecret = getPostVariable('ipn_secret');
  21. $paymentStatus = getPostVariable('status');
  22. $invoiceID = getPostVariable('invoice_id');
  23. $productID = getPostVariable('product_id');
  24. $amount = getPostVariable('amount');
  25. $currency = getPostVariable('currency');
  26. $paymentMethod = getPostVariable('payment_method');
  27. $receiverUsername = getPostVariable('receiver_username');
  28. $customerEmail = getPostVariable('customer_email');
  29.  
  30. //Get custom form field varialbes
  31. $hwid = $_POST['custom_form_fields']['HWID'];
  32. $username = $_POST['custom_form_fields']['Username'];
  33.  
  34. //Step 1 - Validate IPN secret & get product details:
  35.  
  36. $productCurrency = "USD";
  37. $sellerUsername = "MyUsername";
  38.  
  39. switch ($ipnSecret) {
  40. case 'productsecret1':
  41. $licenseDuration = 2592000; //30 days
  42. $productPrice = 10;
  43. break;
  44. case 'productsecret2':
  45. $licenseDuration = 7776000; //90 days
  46. $productPrice = 15;
  47. break;
  48. case 'productsecret3':
  49. $licenseDuration = 15552000; //180 days
  50. $productPrice = 20;
  51. break;
  52. case 'productsecret4':
  53. $licenseDuration = 315569260; //10 years
  54. $productPrice = 35;
  55. break;
  56. default:
  57. //If no valid secret was provided:
  58. debugDie('incorrect secret');
  59. break;
  60. }
  61.  
  62. //Step 2 - Validate payment status:
  63. if($paymentStatus != "complete") debugDie('payment not completed yet');
  64. //Step 3 - Validate payment amount:
  65. if($amount != $productPrice) debugDie('invalid payment amount');
  66. //Step 4 - Validate payment currency:
  67. if($currency != $productCurrency) debugDie('invalid currency');
  68. //Step 5 - Validate receiver username:
  69. if($receiverUsername != $sellerUsername) debugDie('incorrect receiver');
  70.  
  71. /* Now you should check if the transaction is already known, so you don't proccess it twice
  72. ** This is very unlikely to happen, but you still should do this to avoid conflicting database
  73. ** entries */
  74. $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  75. if(mysqli_connect_errno()) debugDie('unable to connect to database');
  76. $tx_id = $mysqli->real_escape_string($tx_id);
  77. $query = "SELECT * FROM paycore_transactions WHERE tx_id = '$tx_id'";
  78. if($result = $mysqli->query($query)){
  79. if($result->num_rows != 0) debugDie('transaction is already known');
  80. /* If the transaction isn't known yet you should insert it into the the table that contains the known
  81. ** transactions, always make sure to escape the strings to avoid SQLi vulnerability */
  82. $result->close();
  83. $time = time();
  84. $invoiceID = $mysqli->real_escape_string($invoiceID);
  85. $paymentMethod = $mysqli->real_escape_string($paymentMethod);
  86. $hwid = $mysqli->real_escape_string($hwid);
  87. $username= $mysqli->real_escape_string($username);
  88. $query = "INSERT INTO paycore_transactions (tx_id, invoice_id, payment_method, time)
  89. VALUES('$tx_id', '$invoiceID', '$paymentMethod', '$time')";
  90. $mysqli->query($query);
  91. /* After inserting the transaction you can continue to perform any required queries your system needs.
  92. ** In example inserting some data from custom form fields into an user table in order to provide access
  93. ** to your product to your customer.
  94. ** In this example we calculate a timestamp the license expires on and insert the provided details into
  95. ** a table that holds the licenses */
  96. $time = time();
  97. $expiryTime = $time + $licenseDuration;
  98. $query = "INSERT INTO users VALUES(NULL, '$hwid', '$username', '$expiryTime', NULL, NULL)";
  99. $mysqli->query($query);
  100. $mysqli->close();
  101. }else{
  102. debugDie('database error');
  103. }
  104.  
  105.  
  106. function debugDie($sMessage){
  107. //Your debugging code
  108. die($sMessage);
  109. }
  110.  
  111. function getPostVariable($sKey){
  112. return (isset($_POST[$sKey]) ? $_POST[$sKey] : false);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement