Advertisement
Guest User

Untitled

a guest
Dec 28th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.93 KB | None | 0 0
  1. Overview
  2.  
  3. This tutorial shows you how to add a new payment method to the vBulletin Payment Gateway. It uses my pgStormPay hack as the example.
  4.  
  5. Step 1: The database
  6.  
  7. The first thing you need to understand is the paymentapi table in the database.
  8.  
  9. Code:
  10. Field Type
  11. ----- ----
  12. Title varchar(250)
  13. Currency varchar(250)
  14. Recurring smallint(6)
  15. Classname varchar(250)
  16. Active smallint(6)
  17. Settings Mediumtext
  18. Title
  19. The title of the payment method, in our case “StormPay”.
  20.  
  21. Currency
  22. Comma delimited list of currency types this payment method can accept. StormPay only accepts us dollars, so in our case it’s just “usd”.
  23.  
  24. Recurring
  25. Whether or not this payment method can handle recurring payments (monthly, yearly, etc…).
  26.  
  27. Classname
  28. The name of the class in yourforum/includes/paymentapi that contains the functions needed to use this payment method. This field name is a little deceptive, because it you look in yourforum/payment_gateway.php you see the following:
  29.  
  30. PHP Code:
  31. require_once(DIR . '/includes/paymentapi/class_' . $api['classname'] . '.php');
  32. $api_class = 'vB_PaidSubscriptionMethod_' . $api['classname'];
  33. So what this field really represents is the unique part of the filename and the classname. class_classname.php and vB_PaidSubscriptionMethod_classname. Or in our case: class_stormpay.php and vB_PaidSubscriptionMethod_stormpay. So you can see the value of this field for us should be just “stormpay”.
  34.  
  35. Active
  36. Is this payment method active? In general you should set this to 0 initially and let the admin activate your payment method from the admincp.
  37.  
  38. Settings
  39. This is a critical part of the payment method, and needs some explaining. It represents the information the admin must fill in for this payment method to work. It will be different for each payment method. It is an array of arrays, with each inner array representing a field to be filled out. The structure of the inner array is similar to a html input element. It has a type, a value, and how to validate the value. For StormPay, we create four fields, as shown bellow.
  40.  
  41. PHP Code:
  42. $email = array("type" => "text", "value" => "", "validate" => "string");
  43. $secret_code = array("type" => "text", "value" => "", "validate" => "string");
  44. $md5 = array("type" => "yesno", "value" => "0", "validate" => "yesno");
  45. $test_mode = array("type" => "yesno", "value" => "0", "validate" => "yesno");
  46. $settings = array("secret_code" => $secret_code, "MD5" => $md5, "test_mode" => $test_mode, "email" => $email);
  47. Putting it into the database
  48. Once you know the values you want for your payment method, you can write them into the database in your install section of your product xml file.
  49.  
  50. PHP Code:
  51. // add the storm pay record to the paymentapi table
  52. $db->hide_errors();
  53. $db->query_write("
  54. INSERT INTO " . TABLE_PREFIX . "paymentapi
  55. (title, currency, recurring, classname, active, settings)
  56. VALUES
  57. ('StormPay', 'usd', 1, 'stormpay', 0, '" . $db->escape_string(serialize($settings)) . )
  58. ");
  59. $db->show_errors();
  60. Step 2: The template
  61.  
  62. Next you’ll need to create a template for your payment form. This is the form that is filled out automatically by vBulletin before it submits data to your payment method. In the case of StormPay, we are going to use a simple single item payment, as specified in their integration api: http://www.stormpay.com/stormpay/user/manual.php
  63.  
  64. Note: I use unit_price instead of amount. It works the same, and seems to clear up some minor bugs.
  65.  
  66. HTML Code:
  67. <input type="hidden" name="test_mode" value="$settings[test_mode]">
  68. <input type="hidden" name="payee_email" value="$settings[email]">
  69. <input type="hidden" name="product_name" value="$subinfo[title] Subscription">
  70. <input type="hidden" name="description" value="$subinfo[description]">
  71. <input type="hidden" name="unit_price" value="$cost">
  72. <input type="hidden" name="user1" value="$hash">
  73. <input type="hidden" name="require_IPN" value="1">
  74. <input type="hidden" name="return_URL" value="$vboptions[bburl]/payment_gateway.php?method=stormpay">
  75. In the case of StormPay we are telling it we require IPN (Immediate Payment Notification). StormPay will post back to the ‘return_URL’, which we set to the payment_gateway.php file and tell it the method is stormpay. No code changes need to be made to the payment_gateway.php as it just uses the method to create an instance of the class we will look at later in this tutorial. Notice we are using some of the settings we created when we added our payment method to the database.
  76.  
  77. To create this template, vBulletin expects it to have the name subscription_payment_classname. So in our case the template section of the product xml would look like this:
  78.  
  79. HTML Code:
  80. <templates>
  81. <template name="subscription_payment_stormpay" templatetype="template" date="0" username="Hambil" version="3.6.0">
  82. <![CDATA[…]]>
  83. </template>
  84. </templates>
  85. Step 3: The plugin
  86. This is not the only way to accomplish this step, but it is the way I have chosen. You need to add the StormPay phrase to the vbphrases array, so it can appear on the “Order Using” button. Since these are service names, and not translated, vBulletin hard codes them in yourforum/payment.php. However, it does provide a hook (paidsub_order_start).
  87.  
  88. The hook code we need is very simple:
  89.  
  90. PHP Code:
  91. $vbphrase += array( 'stormpay' => 'StormPay' );
  92. The plugins section of your product xml would look like this:
  93.  
  94. HTML Code:
  95. <plugins>
  96. <plugin active="1" executionorder="5">
  97. <title><![CDATA[StormPay - add 'order using']]></title>
  98. <hookname>paidsub_order_start</hookname>
  99. <phpcode><![CDATA[…]]></phpcode>
  100. </plugin>
  101. </plugins>
  102. Step 4: The phrases
  103.  
  104. Code:
  105. Global
  106. setting_stormpay_email_desc This is the email address you want to receive payment (the pay to email address).
  107. setting_stormpay_email_title Email
  108. setting_stormpay_secret_code_desc This must be the EXACT secret code value you set in your "Profile" > "IPN Configuration" form in StormPay.
  109. setting_stormpay_secret_code_title Secrect Code
  110. setting_stormpay_test_mode_desc Enable Test Mode. Transactions in test mode are 'fake' and not charged to an account.
  111. setting_stormpay_test_mode_title Test Mode
  112. setting_stormpay_MD5_desc If you enable MD5 encryption you MUST set the list of IPN variables for Hashing in the "IPN Configuration" form of your StormPay Merchant Account to transaction_id; transaction_date; amount; user_id; user1.
  113. setting_stormpay_MD5_title Use MD5 Encryption
  114. stormpay_order_instructions To pay for your subscription via StormPay click the button below and follow the onscreen instructions.
  115.  
  116. Error Messages
  117. stormpay_pending Your subscription is Pending. Please check the StormPay website.
  118. stormpay_cancel Your subscription was canceled.
  119. stormpay_error An error was encountered processing your subscription. You have not been charged.
  120. The phrases section of your product xml would look like this:
  121.  
  122. HTML Code:
  123. <phrases>
  124. <phrasetype name="GLOBAL" fieldname="global">…</phrasetype>
  125. <phrasetype name="Error Messages" fieldname="error">…</phrasetype>
  126. </phrases>
  127. Step 5: The payment method class
  128.  
  129. This is the final part, and the real meat of the payment method. It ties everything else together. It extends the vBulletin class vB_PaidSubscriptionMethod, and the easiest thing is probably to copy one of the existing classes and modify it.
  130.  
  131. It has three functions you need to deal with:
  132.  
  133. verify_payment() function
  134. This function will depend on what the payment method you are using sends back to the payment_gateway.php file. In general you’ll want to get the variables from the submitted request, check them for successful payment, and return true or false to the payment_gateway.php file. Here is a look at the verify_payment() function for StormPay:
  135.  
  136. PHP Code:
  137. function verify_payment()
  138. {
  139. $this->registry->input->clean_array_gpc('r', array(
  140. 'secret_code' => TYPE_STR,
  141. 'product_name' => TYPE_STR,
  142. 'status' => TYPE_STR,
  143. 'unit_price' => TYPE_STR,
  144. 'transaction_id' => TYPE_NUM,
  145. 'transaction_date' => TYPE_STR,
  146. 'user_id' => TYPE_STR,
  147. 'user1' => TYPE_STR
  148. ));
  149.  
  150. $this->secret_code = $this->registry->GPC['secret_code'];
  151. $this->product_name = $this->registry->GPC['product_name'];
  152. $this->status = $this->registry->GPC['status'];
  153. $this->unit_price = $this->registry->GPC['unit_price'];
  154. $this->transaction_id = $this->registry->GPC['transaction_id'];
  155. $this->transaction_date = $this->registry->GPC['transaction_date'];
  156. $this->user_id = $this->registry->GPC['user_id'];
  157. $this->user1 = $this->registry->GPC['user1'];
  158.  
  159. // Check MD5 hash
  160. if ($this->settings['MD5'] AND $this->status != 'TEST')
  161. {
  162. $calc_hash_value = MD5($this->transaction_id.":".$this->transaction_date.":".MD5($this->settings['secret_code']). ":".$this->amount.":".$this->user_id.":".$this->user1);
  163. $sent_hash_value = rawurldecode($this->secret_code);
  164. }
  165.  
  166. if (!$this->settings['Md5'] OR $this->status == 'TEST' OR $calc_hash_value === $sent_hash_value)
  167. {
  168. $this->paymentinfo = $this->registry->db->query_first("
  169. SELECT paymentinfo.*, user.username
  170. FROM " . TABLE_PREFIX . "paymentinfo AS paymentinfo
  171. INNER JOIN " . TABLE_PREFIX . "user AS user USING (userid)
  172. WHERE hash = '" . $this->registry->db->escape_string($this->registry->GPC['user1']) . "'
  173. ");
  174. // lets check the values
  175. if (!empty($this->paymentinfo))
  176. {
  177. $sub = $this->registry->db->query_first("SELECT * FROM " . TABLE_PREFIX . "subscription WHERE subscriptionid = " . $this->paymentinfo['subscriptionid']);
  178. $cost = unserialize($sub['cost']);
  179. $this->paymentinfo['currency'] = 'usd';
  180. $this->paymentinfo['amount'] = floatval($this->unit_price);
  181. if ($this->status == 'SUCCESS' OR $this->status == 'COMPLETE' OR $this->status == 'TEST')
  182. {
  183. $this->type = 1;
  184. return true;
  185. }
  186. else
  187. {
  188. if ($this->status == 'PENDING')
  189. $this->error = fetch_error('stormpay_pending');
  190. else if ($this->status == 'CANCEL')
  191. $this->error = fetch_error('stormpay_cancel');
  192. else
  193. $this->error = fetch_error('stormpay_error');
  194. }
  195. }
  196. }
  197. return false;
  198. }
  199. Note: $this->type defaults to 0, and if the payment is successful you must set it to 1, as well as returning true. A type of 2 is a delete, and handled by vBulletin so you don’t need to worry about it.
  200.  
  201. test() function
  202. This function is used by the Test Communications link under the Paid Subscriptions menu in the admincp. In general it is just a validation of the specific data (settings) needed by the payment method. In our case, we need a valid email and a ‘secret code’. So, or method looks like this:
  203.  
  204. PHP Code:
  205. function test()
  206. {
  207. return (!empty($this->settings['secret_code']) AND !empty($this->settings['email']));
  208. }
  209. generate_form_html() function
  210. This function generates the form that is sent to your payment method, using the template you created earlier. Several variables get passed into the method, and you can also retrieve any values you put into settings. Here is a look at the StormPay function.
  211.  
  212. PHP Code:
  213. function generate_form_html($hash, $cost, $currency, $subinfo, $userinfo, $timeinfo)
  214. {
  215. $form['action'] = 'https://www.stormpay.com/stormpay/handle_gen.php';
  216. $form['method'] = 'post';
  217.  
  218. // load settings into array so the template system can access them
  219. $settings =& $this->settings;
  220.  
  221. $settings['email'] = htmlspecialchars_uni($settings['email']);
  222. $settings['product_name'] = htmlspecialchars_uni($this->registry->subinfo['title']);
  223.  
  224. eval('$form[\'hiddenfields\'] .= "' . fetch_template('subscription_payment_stormpay') . '";');
  225. return $form;
  226. }
  227. Feedback
  228. One final thing to be aware of is that in order to display feedback from your payment method payment_gateway.php requires the value “display_feedback” be set to true. It defaults to false in the vBulletin class you extend. So, you must force it true:
  229.  
  230. PHP Code:
  231. var $display_feedback = true;
  232. That’s all!
  233. That’s it folks! Check out the pgStormPay hack if you still need more details.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement