OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



Beanstream payment method plugin; List of provinces & states

The Plugin Gallery is a venue for users of PKP software to share their plugins.

Moderators: jmacgreg, michael, John

Forum rules
If you post a plugin here, please include information on which versions of which applications it is compatible with. Attach a tarball of the plugin (e.g. tar cvzf myPlugin-0.1.tar.gz plugins/generic/myPlugin). Thanks for contributing!

Beanstream payment method plugin; List of provinces & states

Postby derekp » Wed Mar 19, 2008 4:17 pm

I have implemented a payment method plugin for OCS-2.0.0-1 to handle credit card payments through Beanstream. Beanstream is a payment gateway for Canadian institutions, such as TD Bank.

The code for the plugin is posted on my website. Two of the patches on that page, ocs-2.0.0-1-backport.patch and ocs-2.0.0-1-provinces.patch, are required; the others are highly recommended. I would like to propose that ocs-2.0.0-1-provinces.patch be incorporated into OCS: it contains a list of US states and Canadian provinces that are used in the form where the user enters his/her billing address, and the list could be useful for future payment method plugins as well.

The code suggests that payment method plugins should be able to handle payments for things other than conference registration, but I have not made any attempt to do so for this plugin.

I welcome your comments.

Here is ocs-2.0.0-1-provinces.patch:
Code: Select all
--- ocs-2.0.0-1/classes/user/CountryDAO.inc.php.provinces   2008-02-19 21:27:57.000000000 +0000
+++ ocs-2.0.0-1/classes/user/CountryDAO.inc.php   2008-02-21 06:53:42.000000000 +0000
@@ -32,5 +32,5 @@
    }
 
-   function &_getCountryCache($locale = null) {
+   function &_getCountryProvinceCache($locale = null) {
       static $caches;
 
@@ -46,5 +46,5 @@
          $caches[$locale] =& $cacheManager->getFileCache(
             'country', $locale,
-            array(&$this, '_countryCacheMiss')
+            array(&$this, '_countryProvinceCacheMiss')
          );
 
@@ -58,8 +58,10 @@
    }
 
-   function _countryCacheMiss(&$cache, $id) {
-      static $countries;
+   function _countryProvinceCacheMiss(&$cache, $id) {
+      static $countries, $provinces;
       if (!isset($countries)) {
          $countries = array();
+         $provByCountryCode = array();
+         $provByCountryName = array();
       }
 
@@ -67,13 +69,31 @@
          // Reload country registry file
          $xmlDao = &new XMLDAO();
-         $data = $xmlDao->parseStruct($this->getFilename(), array('countries', 'country'));
-   
-         if (isset($data['countries'])) {
-            foreach ($data['country'] as $countryData) {
-               $countries[$id][$countryData['attributes']['code']] = $countryData['attributes']['name'];
+         $data = $xmlDao->parse($this->getFilename());
+
+         if (isset($data)) {
+            foreach ($data->getChildren() as $countryData) {
+               $countryCode = $countryData->getAttribute('code');
+               $countryName = $countryData->getAttribute('name');
+
+               $countries[$id][$countryCode] = $countryName;
+
+               if (count($countryData->getChildren())) {
+                  $p = array();
+                  foreach ($countryData->getChildren() as $provinceData) {
+                     $provinceCode = $provinceData->getAttribute('code');
+                     $provinceName = $provinceData->getAttribute('name');
+                     $p[$provinceCode] = $provinceName;
+                  }
+                  $provByCountryCode[$id][$countryCode] =
+                    $provByCountryName[$id][$countryName] = $p;
+               }
             }
          }
-         asort($countries[$id]);
-         $cache->setEntireCache($countries[$id]);
+         $result = array(
+            'countries' => $countries[$id],
+            'provByCountryCode' => $provByCountryCode[$id],
+            'provByCountryName' => $provByCountryName[$id],
+         );
+         $cache->setEntireCache($result);
       }
       return null;
@@ -86,6 +106,7 @@
     */
    function &getCountries($locale = null) {
-      $cache =& $this->_getCountryCache($locale);
-      return $cache->getContents();
+      $cache =& $this->_getCountryProvinceCache($locale);
+      $c_p = $cache->getContents();
+      return $c_p['countries'];
    }
    
@@ -96,6 +117,35 @@
     */
    function getCountry($code, $locale = null) {
-      $cache =& $this->_getCountryCache($locale);
-      return $cache->get($code);
+      $countries =& $this->getCountries();
+      return array_key_exists($code, $countries) ? $countries[$code] : null;
+   }
+
+   /**
+    * Return a list of provinces/states/territories.
+    * If $countryCode is given, returns an array keyed by province code.
+    * If $countryCode is null, returns a two-level array keyed by country name
+    * and province code.
+    * @param $countryCode string Country of interest (2 letters, optional)
+    * @param $locale string Name of locale (optional)
+    * @return array
+    */
+   function getProvinces($countryCode = null, $locale = null) {
+      $cache =& $this->_getCountryProvinceCache($locale);
+      $c_p = $cache->getContents();
+      return isset($countryCode) ?
+          (isset($c_p['provByCountryCode'][$countryCode]) ? $c_p['provByCountryCode'][$countryCode] : array()) :
+          $c_p['provByCountryName'];
+   }
+   
+   /**
+    * Return a translated province/state/territory name, given a code.
+    * @param $countryCode string Country of interest (2 letters)
+    * @param $code string Code of province of interest
+    * @param $locale string Name of locale (optional)
+    * @return array
+    */
+   function getProvince($countryCode, $code, $locale = null) {
+      $provinces =& $this->getProvinces($countryCode);
+      return array_key_exists($code, $provinces) ? $provinces[$code] : null;
    }
 }
--- ocs-2.0.0-1/registry/locale/en_US/countries.xml.provinces   2007-04-10 20:45:11.000000000 +0000
+++ ocs-2.0.0-1/registry/locale/en_US/countries.xml   2008-02-21 06:41:52.000000000 +0000
@@ -14,8 +14,12 @@
 <!DOCTYPE countries [
    <!ELEMENT countries (country+)>
-   <!ELEMENT country EMPTY>
+   <!ELEMENT country (province*)>
       <!ATTLIST country
          code CDATA #REQUIRED
          name CDATA #REQUIRED>
+   <!ELEMENT province EMPTY>
+      <!ATTLIST province
+         code CDATA #REQUIRED
+         name CDATA #REQUIRED>
 ]>
    
@@ -59,5 +63,19 @@
    <country name="Cambodia" code="KH"/>
    <country name="Cameroon" code="CM"/>
-   <country name="Canada" code="CA"/>
+   <country name="Canada" code="CA">
+      <province name="Alberta" code="AB"/>
+      <province name="British Columbia" code="BC"/>
+      <province name="Manitoba" code="MB"/>
+      <province name="New Brunswick" code="NB"/>
+      <province name="Newfoundland and Labrador" code="NL"/>
+      <province name="Northwest Territories" code="NT"/>
+      <province name="Nova Scotia" code="NS"/>
+      <province name="Nunavut" code="NU"/>
+      <province name="Ontario" code="ON"/>
+      <province name="Prince Edward Island" code="PE"/>
+      <province name="Quebec" code="QC"/>
+      <province name="Saskatchewan" code="SK"/>
+      <province name="Yukon" code="YT"/>
+   </country>
    <country name="Cape Verde" code="CV"/>
    <country name="Cayman Islands" code="KY"/>
@@ -247,5 +265,57 @@
    <country name="United Arab Emirates" code="AE"/>
    <country name="United Kingdom" code="GB"/>
-   <country name="United States" code="US"/>
+   <country name="United States" code="US">
+      <province name="Alabama" code="AL"/>
+      <province name="Alaska" code="AK"/>
+      <province name="Arizona" code="AZ"/>
+      <province name="Arkansas" code="AK"/>
+      <province name="California" code="CA"/>
+      <province name="Colorado" code="CO"/>
+      <province name="Connecticut" code="CT"/>
+      <province name="Delaware" code="DE"/>
+      <province name="District of Columbia" code="DC"/>
+      <province name="Florida" code="FL"/>
+      <province name="Georgia" code="GA"/>
+      <province name="Hawaii" code="HI"/>
+      <province name="Idaho" code="ID"/>
+      <province name="Illinois" code="IL"/>
+      <province name="Indiana" code="IN"/>
+      <province name="Iowa" code="IA"/>
+      <province name="Kansas" code="KS"/>
+      <province name="Kentucky" code="KY"/>
+      <province name="Louisiana" code="LA"/>
+      <province name="Maine" code="ME"/>
+      <province name="Maryland" code="MD"/>
+      <province name="Massachussetts" code="MA"/>
+      <province name="Michigan" code="MI"/>
+      <province name="Minnesota" code="MN"/>
+      <province name="Mississippi" code="MS"/>
+      <province name="Missouri" code="MO"/>
+      <province name="Montana" code="MT"/>
+      <province name="Nebraska" code="NE"/>
+      <province name="Nevada" code="NV"/>
+      <province name="New Hampshire" code="NH"/>
+      <province name="New Jersey" code="NJ"/>
+      <province name="New Mexico" code="NM"/>
+      <province name="New York" code="NY"/>
+      <province name="North Carolina" code="NC"/>
+      <province name="North Dakota" code="ND"/>
+      <province name="Ohio" code="OH"/>
+      <province name="Oklahoma" code="OK"/>
+      <province name="Oregon" code="OR"/>
+      <province name="Pennsylvania" code="PA"/>
+      <province name="Rhode Island" code="RI"/>
+      <province name="South Carolina" code="SC"/>
+      <province name="South Dakota" code="SD"/>
+      <province name="Tennessee" code="TN"/>
+      <province name="Texas" code="TX"/>
+      <province name="Utah" code="UT"/>
+      <province name="Vermont" code="VT"/>
+      <province name="Virginia" code="VA"/>
+      <province name="Washington" code="WA"/>
+      <province name="West Virginia" code="WV"/>
+      <province name="Wisconsin" code="WI"/>
+      <province name="Wyoming" code="WY"/>
+   </country>
    <country name="United States Minor Outlying Islands" code="UM"/>
    <country name="Uruguay" code="UY"/>
derekp
 
Posts: 16
Joined: Wed Oct 10, 2007 12:45 am
Location: University of British Columbia

Re: Beanstream payment method plugin; List of provinces & states

Postby Morten Poulsen » Wed Oct 01, 2008 3:34 am

This is a minor bugreport to the Beanstream payment plugin. After installing this plugin in a PHP 5 + OCS 2.1.1 environment, I got this error when trying to access Payment Methods:

Fatal error: Cannot redeclare class BeanstreamPlugin in /var/www/ocs2/plugins/paymethod/beanstream/BeanstreamPlugin.inc.php on line 444

I managed to fix it by changing the line /plugins/paymethod/beanstream/index.php, so that the line

Code: Select all
require('BeanstreamPlugin.inc.php');


became

Code: Select all
require_once('BeanstreamPlugin.inc.php');
Morten Poulsen
 
Posts: 1
Joined: Wed Oct 01, 2008 3:17 am

Re: Beanstream payment method plugin; List of provinces & states

Postby asmecher » Wed Oct 01, 2008 10:54 am

Hi Morten,

Thanks for posting -- this is due to a change in plugin structure between OCS 2.0 and 2.1 (and, for that matter, OJS 2.1 and 2.2). We try not to break backwards compatibility with subsequent releases, but sometimes it's necessary. Your fix addresses the problem fine.

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 9099
Joined: Wed Aug 10, 2005 12:56 pm


Return to Plugin Gallery

Who is online

Users browsing this forum: No registered users and 1 guest