Allopass_Hipay - Version 1.0.0

Version Notes

-New iframe templates

Download this release

Release Info

Developer Kassim_Profileo
Extension Allopass_Hipay
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

Files changed (104) hide show
  1. app/code/community/Allopass/Hipay/.DS_Store +0 -0
  2. app/code/community/Allopass/Hipay/Block/Adminhtml/System/Config/Form/Field/3dsRule.php +44 -0
  3. app/code/community/Allopass/Hipay/Block/Checkout/Pending.php +30 -0
  4. app/code/community/Allopass/Hipay/Block/Form/Abstract.php +74 -0
  5. app/code/community/Allopass/Hipay/Block/Form/Cc.php +113 -0
  6. app/code/community/Allopass/Hipay/Block/Form/Hosted.php +10 -0
  7. app/code/community/Allopass/Hipay/Block/Info/Cc.php +103 -0
  8. app/code/community/Allopass/Hipay/Block/Info/Hosted.php +9 -0
  9. app/code/community/Allopass/Hipay/Controller/Payment.php +271 -0
  10. app/code/community/Allopass/Hipay/Helper/Data.php +203 -0
  11. app/code/community/Allopass/Hipay/Model/Api/Http/Client/Adapter/Curl.php +483 -0
  12. app/code/community/Allopass/Hipay/Model/Api/Http/Client/Adapter/Stream.php +20 -0
  13. app/code/community/Allopass/Hipay/Model/Api/Request.php +219 -0
  14. app/code/community/Allopass/Hipay/Model/Api/Response/Abstract.php +17 -0
  15. app/code/community/Allopass/Hipay/Model/Api/Response/Error.php +12 -0
  16. app/code/community/Allopass/Hipay/Model/Api/Response/Gateway.php +114 -0
  17. app/code/community/Allopass/Hipay/Model/Api/Response/Notification.php +33 -0
  18. app/code/community/Allopass/Hipay/Model/Api/Response/Vault.php +9 -0
  19. app/code/community/Allopass/Hipay/Model/Config.php +234 -0
  20. app/code/community/Allopass/Hipay/Model/Log/Adapter.php +118 -0
  21. app/code/community/Allopass/Hipay/Model/Method/Abstract.php +1003 -0
  22. app/code/community/Allopass/Hipay/Model/Method/Cbc.php +5 -0
  23. app/code/community/Allopass/Hipay/Model/Method/Cc.php +517 -0
  24. app/code/community/Allopass/Hipay/Model/Method/Dexia.php +5 -0
  25. app/code/community/Allopass/Hipay/Model/Method/Giropay.php +5 -0
  26. app/code/community/Allopass/Hipay/Model/Method/Hosted.php +118 -0
  27. app/code/community/Allopass/Hipay/Model/Method/Ideal.php +5 -0
  28. app/code/community/Allopass/Hipay/Model/Method/Ing.php +5 -0
  29. app/code/community/Allopass/Hipay/Model/Method/Kbc.php +5 -0
  30. app/code/community/Allopass/Hipay/Model/Method/Przelewy24.php +5 -0
  31. app/code/community/Allopass/Hipay/Model/Method/Przelewy24Api.php +83 -0
  32. app/code/community/Allopass/Hipay/Model/Method/Qiwi.php +5 -0
  33. app/code/community/Allopass/Hipay/Model/Method/Sisal.php +5 -0
  34. app/code/community/Allopass/Hipay/Model/Method/SisalApi.php +83 -0
  35. app/code/community/Allopass/Hipay/Model/Method/Sofort.php +5 -0
  36. app/code/community/Allopass/Hipay/Model/Method/SofortApi.php +83 -0
  37. app/code/community/Allopass/Hipay/Model/Method/Webmoney.php +5 -0
  38. app/code/community/Allopass/Hipay/Model/Method/WebmoneyApi.php +83 -0
  39. app/code/community/Allopass/Hipay/Model/Method/Yandex.php +5 -0
  40. app/code/community/Allopass/Hipay/Model/Method/YandexApi.php +83 -0
  41. app/code/community/Allopass/Hipay/Model/Observer.php +96 -0
  42. app/code/community/Allopass/Hipay/Model/Resource/Rule.php +19 -0
  43. app/code/community/Allopass/Hipay/Model/Resource/Rule/Collection.php +20 -0
  44. app/code/community/Allopass/Hipay/Model/Rule.php +44 -0
  45. app/code/community/Allopass/Hipay/Model/Rule/Condition/Address.php +231 -0
  46. app/code/community/Allopass/Hipay/Model/Rule/Condition/Combine.php +183 -0
  47. app/code/community/Allopass/Hipay/Model/Rule/Condition/Customer.php +186 -0
  48. app/code/community/Allopass/Hipay/Model/Rule/Condition/Product.php +128 -0
  49. app/code/community/Allopass/Hipay/Model/Rule/Condition/Product/Combine.php +172 -0
  50. app/code/community/Allopass/Hipay/Model/Rule/Condition/Product/Found.php +70 -0
  51. app/code/community/Allopass/Hipay/Model/Rule/Condition/Product/Subselect.php +102 -0
  52. app/code/community/Allopass/Hipay/Model/Rule/Config.php +93 -0
  53. app/code/community/Allopass/Hipay/Model/Source/3ds.php +39 -0
  54. app/code/community/Allopass/Hipay/Model/Source/CcType.php +24 -0
  55. app/code/community/Allopass/Hipay/Model/Source/CcTypeHosted.php +24 -0
  56. app/code/community/Allopass/Hipay/Model/Source/Order/HipayStatusValidate.php +20 -0
  57. app/code/community/Allopass/Hipay/Model/Source/Order/Status.php +28 -0
  58. app/code/community/Allopass/Hipay/Model/Source/Order/Status/Accepted.php +12 -0
  59. app/code/community/Allopass/Hipay/Model/Source/Order/Status/Canceled.php +11 -0
  60. app/code/community/Allopass/Hipay/Model/Source/Order/Status/New.php +11 -0
  61. app/code/community/Allopass/Hipay/Model/Source/Order/Status/Refused.php +12 -0
  62. app/code/community/Allopass/Hipay/Model/Source/PaymentAction.php +17 -0
  63. app/code/community/Allopass/Hipay/Model/Source/Pendingredirect.php +19 -0
  64. app/code/community/Allopass/Hipay/Model/Source/Template.php +35 -0
  65. app/code/community/Allopass/Hipay/controllers/Adminhtml/RuleController.php +37 -0
  66. app/code/community/Allopass/Hipay/controllers/CbcController.php +8 -0
  67. app/code/community/Allopass/Hipay/controllers/CcController.php +15 -0
  68. app/code/community/Allopass/Hipay/controllers/CheckoutController.php +40 -0
  69. app/code/community/Allopass/Hipay/controllers/DexiaController.php +8 -0
  70. app/code/community/Allopass/Hipay/controllers/GiropayController.php +8 -0
  71. app/code/community/Allopass/Hipay/controllers/HostedController.php +12 -0
  72. app/code/community/Allopass/Hipay/controllers/IdealController.php +8 -0
  73. app/code/community/Allopass/Hipay/controllers/IngController.php +8 -0
  74. app/code/community/Allopass/Hipay/controllers/KbcController.php +8 -0
  75. app/code/community/Allopass/Hipay/controllers/NotifyController.php +193 -0
  76. app/code/community/Allopass/Hipay/controllers/Przelewy24Controller.php +8 -0
  77. app/code/community/Allopass/Hipay/controllers/Przelewy24apiController.php +8 -0
  78. app/code/community/Allopass/Hipay/controllers/QiwiController.php +8 -0
  79. app/code/community/Allopass/Hipay/controllers/SisalController.php +8 -0
  80. app/code/community/Allopass/Hipay/controllers/SisalapiController.php +8 -0
  81. app/code/community/Allopass/Hipay/controllers/SofortController.php +8 -0
  82. app/code/community/Allopass/Hipay/controllers/SofortapiController.php +8 -0
  83. app/code/community/Allopass/Hipay/controllers/WebmoneyController.php +8 -0
  84. app/code/community/Allopass/Hipay/controllers/WebmoneyapiController.php +8 -0
  85. app/code/community/Allopass/Hipay/controllers/YandexController.php +8 -0
  86. app/code/community/Allopass/Hipay/controllers/YandexapiController.php +8 -0
  87. app/code/community/Allopass/Hipay/etc/config.xml +637 -0
  88. app/code/community/Allopass/Hipay/etc/system.xml +2586 -0
  89. app/code/community/Allopass/Hipay/sql/allopass_hipay_setup/mysql4-install-0.1.0.php +45 -0
  90. app/code/community/Allopass/Hipay/sql/allopass_hipay_setup/mysql4-upgrade-0.1.0-0.1.1.php +123 -0
  91. app/code/community/Allopass/Hipay/sql/allopass_hipay_setup/mysql4-upgrade-0.1.1-0.1.2.php +38 -0
  92. app/code/community/Allopass/Hipay/sql/allopass_hipay_setup/mysql4-upgrade-0.1.2-0.1.3.php +72 -0
  93. app/code/community/Allopass/Hipay/sql/allopass_hipay_setup/mysql4-upgrade-0.1.4-0.1.5.php +24 -0
  94. app/design/adminhtml/default/default/layout/hipay.xml +12 -0
  95. app/design/adminhtml/default/default/template/hipay/system/config/form/field/rules.phtml +17 -0
  96. app/design/frontend/default/default/layout/hipay.xml +12 -0
  97. app/design/frontend/default/default/template/hipay/checkout/pending.phtml +8 -0
  98. app/design/frontend/default/default/template/hipay/form/cc.phtml +144 -0
  99. app/design/frontend/default/default/template/hipay/form/hosted.phtml +134 -0
  100. app/etc/modules/Allopass_Hipay.xml +9 -0
  101. app/locale/en_US/Allopass_Hipay.csv +107 -0
  102. app/locale/fr_FR/Allopass_Hipay.csv +107 -0
  103. package.xml +18 -0
  104. skin/adminhtml/default/default/hipay/js/rules.js +386 -0
app/code/community/Allopass/Hipay/.DS_Store ADDED
Binary file
app/code/community/Allopass/Hipay/Block/Adminhtml/System/Config/Form/Field/3dsRule.php ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Allopass_Hipay_Block_Adminhtml_System_Config_Form_Field_3dsRule extends Mage_Adminhtml_Block_System_Config_Form_Field
3
+ {
4
+
5
+ /**
6
+ * Check if columns are defined, set template
7
+ *
8
+ */
9
+ public function __construct()
10
+ {
11
+ if (!$this->_addButtonLabel) {
12
+ $this->_addButtonLabel = Mage::helper('adminhtml')->__('Add');
13
+ }
14
+ parent::__construct();
15
+ if (!$this->getTemplate()) {
16
+ $this->setTemplate('hipay/system/config/form/field/rules.phtml');
17
+ }
18
+ }
19
+
20
+ public function getNewChildUrl(){
21
+ return Mage::getUrl('hipay/adminhtml_rule/newConditionHtml/form/rule_conditions_fieldset');
22
+ }
23
+
24
+ /**
25
+ * Enter description here...
26
+ *
27
+ * @param Varien_Data_Form_Element_Abstract $element
28
+ * @return string
29
+ */
30
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
31
+ {
32
+ $partsId = explode("_", $element->getId());
33
+ $method_code = $partsId[1]. "_" . $partsId[2];
34
+ $rule = Mage::getModel('hipay/rule');
35
+ $rule->setMethodCode($method_code);
36
+ if($element->getValue())
37
+ $rule->load($element->getValue());
38
+
39
+ $element->setRule($rule);
40
+
41
+ $this->setElement($element);
42
+ return $this->_toHtml();
43
+ }
44
+ }
app/code/community/Allopass/Hipay/Block/Checkout/Pending.php ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Allopass_Hipay_Block_Checkout_Pending extends Mage_Core_Block_Template
3
+ {
4
+ public function getRealOrderId()
5
+ {
6
+ return Mage::getSingleton('checkout/session')->getLastRealOrderId();
7
+ }
8
+
9
+ /**
10
+ * Payment custom error message
11
+ *
12
+ * @return string
13
+ */
14
+ public function getErrorMessage ()
15
+ {
16
+ $error = Mage::getSingleton('checkout/session')->getErrorMessage();
17
+ // Mage::getSingleton('checkout/session')->unsErrorMessage();
18
+ return $error;
19
+ }
20
+
21
+ /**
22
+ * Continue shopping URL
23
+ *
24
+ * @return string
25
+ */
26
+ public function getContinueShoppingUrl()
27
+ {
28
+ return Mage::getUrl('checkout/cart');
29
+ }
30
+ }
app/code/community/Allopass/Hipay/Block/Form/Abstract.php ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ abstract class Allopass_Hipay_Block_Form_Abstract extends Mage_Payment_Block_Form
3
+ {
4
+
5
+ /**
6
+ * Retrieve payment configuration object
7
+ *
8
+ * @return Allopass_Hipay_Model_Config
9
+ */
10
+ protected function _getConfig()
11
+ {
12
+ return Mage::getSingleton('hipay/config');
13
+ }
14
+
15
+
16
+ public function getCustomerHasAlias()
17
+ {
18
+ return $this->getCustomer()->getHipayAliasOneclick() != "";
19
+
20
+ }
21
+
22
+ public function getCustomer()
23
+ {
24
+ return Mage::getSingleton('customer/session')->getCustomer();
25
+ }
26
+
27
+ public function ccExpDateIsValid()
28
+ {
29
+ return $this->helper('hipay')->checkIfCcExpDateIsValid((int)Mage::getSingleton('customer/session')->getCustomerId());
30
+ }
31
+
32
+ public function oneClickIsAllowed()
33
+ {
34
+ $checkoutMethod = Mage::getSingleton('checkout/session')->getQuote()->getCheckoutMethod();
35
+
36
+ if($checkoutMethod == Mage_Checkout_Model_Type_Onepage::METHOD_GUEST || !$this->allowUseOneClick())
37
+ return false;
38
+
39
+ return true;
40
+
41
+ }
42
+
43
+ public function getQuote()
44
+ {
45
+ return Mage::getSingleton('checkout/session')->getQuote();
46
+ }
47
+
48
+
49
+ public function allowUseOneClick()
50
+ {
51
+ return $this->getMethod()->getConfigData('allow_use_oneclick');
52
+ }
53
+
54
+ public function getIframeConfig()
55
+ {
56
+ $iframe['iframe_width'] = $this->getMethod()->getConfigData('iframe_width');
57
+ $iframe['iframe_height'] = $this->getMethod()->getConfigData('iframe_height');
58
+ $iframe['iframe_style'] = $this->getMethod()->getConfigData('iframe_style');
59
+ return $iframe;
60
+ }
61
+
62
+ /**
63
+ * Render block HTML
64
+ *
65
+ * @return string
66
+ */
67
+ protected function _toHtml()
68
+ {
69
+ Mage::dispatchEvent('payment_form_block_to_html_before', array(
70
+ 'block' => $this
71
+ ));
72
+ return parent::_toHtml();
73
+ }
74
+ }
app/code/community/Allopass/Hipay/Block/Form/Cc.php ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Allopass_Hipay_Block_Form_Cc extends Allopass_Hipay_Block_Form_Abstract
3
+ {
4
+ protected function _construct()
5
+ {
6
+ parent::_construct();
7
+ $this->setTemplate('hipay/form/cc.phtml');
8
+ }
9
+
10
+
11
+ /**
12
+ * Retrieve availables credit card types
13
+ *
14
+ * @return array
15
+ */
16
+ public function getCcAvailableTypes()
17
+ {
18
+ $types =$this->_getConfig()->getCcTypes();
19
+ if ($method = $this->getMethod()) {
20
+ $availableTypes = $method->getConfigData('cctypes');
21
+ if ($availableTypes) {
22
+ $availableTypes = explode(',', $availableTypes);
23
+ foreach ($types as $code=>$name) {
24
+ if (!in_array($code, $availableTypes)) {
25
+ unset($types[$code]);
26
+ }
27
+ }
28
+ }
29
+ }
30
+ return $types;
31
+ }
32
+
33
+ /**
34
+ * Retrieve credit card expire months
35
+ *
36
+ * @return array
37
+ */
38
+ public function getCcMonths()
39
+ {
40
+ $months = $this->getData('cc_months');
41
+ if (is_null($months)) {
42
+ $months["0"] = $this->__('Month');
43
+ $months = array_merge($months, $this->_getConfig()->getMonths());
44
+ $this->setData('cc_months', $months);
45
+ }
46
+ return $months;
47
+ }
48
+
49
+ /**
50
+ * Retrieve credit card expire years
51
+ *
52
+ * @return array
53
+ */
54
+ public function getCcYears()
55
+ {
56
+ $years = $this->getData('cc_years');
57
+ if (is_null($years)) {
58
+ $years = $this->_getConfig()->getYears();
59
+ $years = array(0=>$this->__('Year'))+$years;
60
+ $this->setData('cc_years', $years);
61
+ }
62
+ return $years;
63
+ }
64
+
65
+ /**
66
+ * Retrive has verification configuration
67
+ *
68
+ * @return boolean
69
+ */
70
+ public function hasVerification()
71
+ {
72
+ if ($this->getMethod()) {
73
+ $configData = $this->getMethod()->getConfigData('useccv');
74
+ if(is_null($configData)){
75
+ return true;
76
+ }
77
+ return (bool) $configData;
78
+ }
79
+ return true;
80
+ }
81
+
82
+ /*
83
+ * Whether switch/solo card type available
84
+ */
85
+ public function hasSsCardType()
86
+ {
87
+ $availableTypes = explode(',', $this->getMethod()->getConfigData('cctypes'));
88
+ $ssPresenations = array_intersect(array('SS', 'SM', 'SO'), $availableTypes);
89
+ if ($availableTypes && count($ssPresenations) > 0) {
90
+ return true;
91
+ }
92
+ return false;
93
+ }
94
+
95
+ /*
96
+ * solo/switch card start year
97
+ * @return array
98
+ */
99
+ public function getSsStartYears()
100
+ {
101
+ $years = array();
102
+ $first = date("Y");
103
+
104
+ for ($index=5; $index>=0; $index--) {
105
+ $year = $first - $index;
106
+ $years[$year] = $year;
107
+ }
108
+ $years = array(0=>$this->__('Year'))+$years;
109
+ return $years;
110
+ }
111
+
112
+
113
+ }
app/code/community/Allopass/Hipay/Block/Form/Hosted.php ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Allopass_Hipay_Block_Form_Hosted extends Allopass_Hipay_Block_Form_Abstract
3
+ {
4
+ protected function _construct()
5
+ {
6
+ parent::_construct();
7
+ $this->setTemplate('hipay/form/hosted.phtml');
8
+ }
9
+
10
+ }
app/code/community/Allopass/Hipay/Block/Info/Cc.php ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Credit card hipay payment info
5
+ */
6
+ class Allopass_Hipay_Block_Info_Cc extends Mage_Payment_Block_Info
7
+ {
8
+ /**
9
+ * Retrieve credit card type name
10
+ *
11
+ * @return string
12
+ */
13
+ public function getCcTypeName()
14
+ {
15
+ $types = Mage::getSingleton('payment/config')->getCcTypes();
16
+ $ccType = $this->getInfo()->getCcType();
17
+ if (isset($types[$ccType])) {
18
+ return $types[$ccType];
19
+ }
20
+ return (empty($ccType)) ? Mage::helper('payment')->__('N/A') : $ccType;
21
+ }
22
+
23
+ /**
24
+ * Whether current payment method has credit card expiration info
25
+ *
26
+ * @return bool
27
+ */
28
+ public function hasCcExpDate()
29
+ {
30
+ return (int)$this->getInfo()->getCcExpMonth() || (int)$this->getInfo()->getCcExpYear();
31
+ }
32
+
33
+ /**
34
+ * Retrieve CC expiration month
35
+ *
36
+ * @return string
37
+ */
38
+ public function getCcExpMonth()
39
+ {
40
+ $month = $this->getInfo()->getCcExpMonth();
41
+ if ($month<10) {
42
+ $month = '0'.$month;
43
+ }
44
+ return $month;
45
+ }
46
+
47
+ /**
48
+ * Retrieve CC expiration date
49
+ *
50
+ * @return Zend_Date
51
+ */
52
+ public function getCcExpDate()
53
+ {
54
+ $date = Mage::app()->getLocale()->date(0);
55
+ $date->setYear($this->getInfo()->getCcExpYear());
56
+ $date->setMonth($this->getInfo()->getCcExpMonth());
57
+ return $date;
58
+ }
59
+
60
+ /**
61
+ * Prepare credit card related payment info
62
+ *
63
+ * @param Varien_Object|array $transport
64
+ * @return Varien_Object
65
+ */
66
+ protected function _prepareSpecificInformation($transport = null)
67
+ {
68
+ if (null !== $this->_paymentSpecificInformation) {
69
+ return $this->_paymentSpecificInformation;
70
+ }
71
+ $transport = parent::_prepareSpecificInformation($transport);
72
+ $data = array();
73
+ if ($ccType = $this->getCcTypeName()) {
74
+ $data[Mage::helper('payment')->__('Credit Card Type')] = $ccType;
75
+ }
76
+ if ($this->getInfo()->getCcLast4()) {
77
+ $data[Mage::helper('payment')->__('Credit Card Number')] = sprintf('xxxx-%s', $this->getInfo()->getCcLast4());
78
+ }
79
+ if (!$this->getIsSecureMode()) {
80
+ if ($ccSsIssue = $this->getInfo()->getCcSsIssue()) {
81
+ $data[Mage::helper('payment')->__('Switch/Solo/Maestro Issue Number')] = $ccSsIssue;
82
+ }
83
+ $year = $this->getInfo()->getCcSsStartYear();
84
+ $month = $this->getInfo()->getCcSsStartMonth();
85
+ if ($year && $month) {
86
+ $data[Mage::helper('payment')->__('Switch/Solo/Maestro Start Date')] = $this->_formatCardDate($year, $month);
87
+ }
88
+ }
89
+ return $transport->setData(array_merge($data, $transport->getData()));
90
+ }
91
+
92
+ /**
93
+ * Format year/month on the credit card
94
+ *
95
+ * @param string $year
96
+ * @param string $month
97
+ * @return string
98
+ */
99
+ protected function _formatCardDate($year, $month)
100
+ {
101
+ return sprintf('%s/%s', sprintf('%02d', $month), $year);
102
+ }
103
+ }
app/code/community/Allopass/Hipay/Block/Info/Hosted.php ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * hosted hipay payment info
5
+ */
6
+ class Allopass_Hipay_Block_Info_Hosted extends Mage_Payment_Block_Info
7
+ {
8
+
9
+ }
app/code/community/Allopass/Hipay/Controller/Payment.php ADDED
@@ -0,0 +1,271 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Allopass_Hipay_Controller_Payment extends Mage_Core_Controller_Front_Action
3
+ {
4
+ /**
5
+ *
6
+ * @var Mage_Sales_Model_Order $order
7
+ */
8
+ protected $_order = null;
9
+
10
+
11
+ /**
12
+ * @return Mage_Core_Controller_Front_Action
13
+ */
14
+ public function preDispatch() {
15
+ parent::preDispatch();
16
+
17
+ if (!$this->_validateSignature()) {
18
+ $this->getResponse()->setBody("NOK. Wrong Signature!");
19
+ $this->setFlag('', 'no-dispatch', true);
20
+ }
21
+ }
22
+
23
+
24
+ protected function _validateSignature()
25
+ {
26
+ /* @var $_helper Allopass_Hipay_Helper_Data */
27
+ $_helper = Mage::helper('hipay');
28
+ $signature = $this->getRequest()->getParam('hash');
29
+ return $_helper->checkSignature($signature);
30
+ }
31
+
32
+ /**
33
+ *
34
+ * @return Allopass_Hipay_Model_Method_Abstract $methodInstance
35
+ */
36
+ protected function _getMethodInstance()
37
+ {
38
+ Mage::throwException("Method: '" . __METHOD__ . "' must be implemented!");
39
+ }
40
+
41
+ public function sendRequestAction()
42
+ {
43
+ $order = $this->getOrder();
44
+ $payment = $order->getPayment();
45
+ $methodInstance = $this->_getMethodInstance();
46
+
47
+ try
48
+ {
49
+ $redirectUrl = $methodInstance->place($payment,$order->getBaseTotalDue());
50
+ }
51
+ catch (Exception $e)
52
+ {
53
+ Mage::logException($e);
54
+ $this->getCheckout()->addError($e->getMessage());
55
+ $this->_redirect('checkout/cart');
56
+ return $this;
57
+ }
58
+
59
+
60
+ $this->_redirectUrl($redirectUrl);
61
+
62
+ return $this;
63
+
64
+ }
65
+
66
+
67
+ public function acceptAction()
68
+ {
69
+ if(($profileIds = Mage::getSingleton('checkout/session')->getLastRecurringProfileIds()))
70
+ {
71
+ if(is_array($profileIds))
72
+ {
73
+ /* @var $gatewayResponse Allopass_Hipay_Model_Api_Response_Gateway */
74
+ $gatewayResponse = Mage::getSingleton('hipay/api_response_gateway',$this->getRequest()->getParams());
75
+ $collection = Mage::getModel('sales/recurring_profile')->getCollection()
76
+ ->addFieldToFilter('profile_id', array('in' => $profileIds))
77
+ ;
78
+ $profiles = array();
79
+ foreach ($collection as $profile) {
80
+ //$referenceId = $gatewayResponse->getToken()."-".$profile->getId();
81
+ $additionalInfo = array();
82
+ $additionalInfo['ccType'] = $gatewayResponse->getBrand();
83
+ $additionalInfo['ccExpMonth'] = $gatewayResponse->getCardExpiryMonth() ;
84
+ $additionalInfo['ccExpYear'] = $gatewayResponse->getCardExpiryYear();
85
+ $additionalInfo['token'] = $gatewayResponse->getToken();
86
+ $additionalInfo['transaction_id'] = $gatewayResponse->getTransactionReference();
87
+ $profile->setAdditionalInfo($additionalInfo);
88
+ //$profile->setReferenceId($referenceId);
89
+ $profile->setState(Mage_Sales_Model_Recurring_Profile::STATE_ACTIVE);
90
+
91
+ $profile->save();
92
+ }
93
+ }
94
+ }
95
+ /*else
96
+ {
97
+ $this->processResponse();
98
+ }*/
99
+ $this->processResponse();
100
+ $this->_redirect('checkout/onepage/success');
101
+
102
+ return $this;
103
+ }
104
+
105
+ public function pendingAction()
106
+ {
107
+ $this->processResponse();
108
+ $this->_redirect($this->_getMethodInstance()->getConfigData('pending_redirect_page'));
109
+
110
+ return $this;
111
+ }
112
+
113
+ public function declineAction()
114
+ {
115
+ $this->processResponse();
116
+ $this->_redirect('checkout/onepage/failure');
117
+ return $this;
118
+ }
119
+
120
+
121
+ public function exceptionAction()
122
+ {
123
+ $this->_redirect('checkout/onepage/failure');
124
+ return $this;
125
+ }
126
+
127
+
128
+ public function cancelAction()
129
+ {
130
+ $this->processResponse();
131
+ $this->_redirect('checkout/cart');
132
+ return $this;
133
+ }
134
+
135
+ protected function processResponse()
136
+ {
137
+ $order = $this->getOrder();
138
+ $payment = $order->getPayment();
139
+
140
+ /* @var $gatewayResponse Allopass_Hipay_Model_Api_Response_Gateway */
141
+ $gatewayResponse = Mage::getSingleton('hipay/api_response_gateway',$this->getRequest()->getParams());
142
+
143
+ $this->_getMethodInstance()->processResponseToRedirect($gatewayResponse, $payment, $order->getBaseTotalDue());
144
+ }
145
+
146
+
147
+
148
+ /**
149
+ *
150
+ * @return Mage_Sales_Model_Order
151
+ */
152
+ protected function getOrder()
153
+ {
154
+ if(is_null($this->_order))
155
+ {
156
+
157
+ if(($profileIds = $this->getCheckout()->getLastRecurringProfileIds()))
158
+ {
159
+
160
+ if (is_array($profileIds)) {
161
+
162
+ foreach ($profileIds as $profileId)
163
+ {
164
+ /* @var $profile Mage_Sales_Model_Recurring_Profile */
165
+ $profile = Mage::getModel('sales/recurring_profile')->load($profileId);
166
+ /* @var $_helperRecurring Allopass_Hipayrecurring_Helper_Data */
167
+ $_helperRecurring = Mage::helper('hipayrecurring');
168
+
169
+ if($_helperRecurring->isInitialProfileOrder($profile))
170
+ $this->_order = $_helperRecurring->createOrderFromProfile($profile);
171
+ else
172
+ {
173
+ $orderId = current($profile->getChildOrderIds());
174
+ $this->_order = Mage::getModel('sales/order')->load($orderId);
175
+
176
+ $additionalInfo = $profile->getAdditionalInfo();
177
+
178
+ $this->_order->getPayment()->setCcType(isset($additionalInfo['ccType']) ? $additionalInfo['ccType'] : "");
179
+ $this->_order->getPayment()->setCcExpMonth(isset($additionalInfo['ccExpMonth']) ? $additionalInfo['ccExpMonth'] : "");
180
+ $this->_order->getPayment()->setCcExpYear(isset($additionalInfo['ccExpYear']) ? $additionalInfo['ccExpYear'] : "");
181
+ $this->_order->getPayment()->setAdditionalInformation('token',isset($additionalInfo['token']) ? $additionalInfo['token'] : "");
182
+ $this->_order->getPayment()->setAdditionalInformation('create_oneclick',isset($additionalInfo['create_oneclick']) ? $additionalInfo['create_oneclick'] : 1);
183
+ $this->_order->getPayment()->setAdditionalInformation('use_oneclick',isset($additionalInfo['use_oneclick']) ? $additionalInfo['use_oneclick'] : 0);
184
+ }
185
+
186
+
187
+
188
+ return $this->_order; //because only one nominal item in cart is authorized and Hipay not manage many profiles
189
+
190
+ //$amount = $this->getAmountFromProfile($profile);
191
+
192
+ /*$productItemInfo = new Varien_Object;
193
+ $type = "Regular";
194
+ if ($type == 'Trial') {
195
+ $productItemInfo->setPaymentType(Mage_Sales_Model_Recurring_Profile::PAYMENT_TYPE_TRIAL);
196
+ } elseif ($type == 'Regular') {
197
+ $productItemInfo->setPaymentType(Mage_Sales_Model_Recurring_Profile::PAYMENT_TYPE_REGULAR);
198
+ }
199
+
200
+
201
+ if($this->isInitialProfileOrder($profile))// because is not additonned in prodile obj
202
+ $productItemInfo->setPrice($profile->getBillingAmount() + $profile->getInitAmount());
203
+
204
+ $this->_order = $profile->createOrder($productItemInfo);
205
+
206
+ $additionalInfo = $profile->getAdditionalInfo();
207
+
208
+ $this->_order->getPayment()->setCcType($additionalInfo['ccType']);
209
+ $this->_order->getPayment()->setCcExpMonth($additionalInfo['ccExpMonth']);
210
+ $this->_order->getPayment()->setCcExpYear($additionalInfo['ccExpYear']);
211
+ $this->_order->getPayment()->setAdditionalInformation('token',$additionalInfo['token']);
212
+ $this->_order->getPayment()->setAdditionalInformation('create_oneclick',$additionalInfo['create_oneclick']);
213
+ $this->_order->getPayment()->setAdditionalInformation('use_oneclick',$additionalInfo['use_oneclick']);
214
+
215
+ $orderId = 'create-recurring';
216
+ $orderId .= "-".$profileId;
217
+ //$this->_order->setIncrementId($orderId);
218
+ $this->_order->save();
219
+ $profile->addOrderRelation($this->_order->getId());
220
+ $profile->save();*/
221
+ return $this->_order; //because only one nominal item in cart is authorized and Hipay not manage many profiles
222
+ //break;
223
+ }
224
+
225
+
226
+ }
227
+
228
+ Mage::throwException("An error occured. Profile Ids not present!");
229
+
230
+
231
+
232
+ }
233
+ else
234
+ $this->_order = Mage::getModel('sales/order')->load($this->getCheckout()->getLastOrderId());
235
+ }
236
+
237
+ return $this->_order;
238
+ }
239
+
240
+ /**
241
+ * Add method to calculate amount from recurring profile
242
+ * @param Mage_Sales_Model_Recurring_Profile $profile
243
+ * @return int $amount
244
+ **/
245
+ public function getAmountFromProfile(Mage_Sales_Model_Recurring_Profile $profile) {
246
+ $amount = $profile->getBillingAmount() + $profile->getTaxAmount() + $profile->getShippingAmount();
247
+
248
+ if($this->isInitialProfileOrder($profile))
249
+ $amount += $profile->getInitAmount() ;
250
+
251
+ return $amount;
252
+ }
253
+
254
+ protected function isInitialProfileOrder(Mage_Sales_Model_Recurring_Profile $profile)
255
+ {
256
+ if(count($profile->getChildOrderIds()) && current($profile->getChildOrderIds()) == "-1")
257
+ return true;
258
+
259
+ return false;
260
+ }
261
+
262
+
263
+ /**
264
+ *
265
+ * @return Mage_Checkout_Model_Session
266
+ */
267
+ protected function getCheckout()
268
+ {
269
+ return Mage::getSingleton('checkout/session');
270
+ }
271
+ }
app/code/community/Allopass/Hipay/Helper/Data.php ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Allopass_Hipay_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+
5
+
6
+ public function checkSignature($signature,$fromNotification = false)
7
+ {
8
+ $passphrase = Mage::getStoreConfig('hipay/hipay_api/secret_passphrase');
9
+ if(empty($passphrase) || empty($signature))
10
+ return true;
11
+
12
+ if($fromNotification)
13
+ {
14
+ $rawPostData = file_get_contents("php://input");
15
+ if($signature == sha1($rawPostData . $passphrase));
16
+ return true;
17
+
18
+ return false;
19
+ }
20
+
21
+
22
+ $parameters = $this->_getRequest()->getParams();
23
+ $string2compute = "";
24
+ unset($parameters['hash']);
25
+ ksort($parameters);
26
+ foreach ($parameters as $name => $value) {
27
+ if (!empty($value)) {
28
+ $string2compute .= $name . $value . $passphrase;
29
+ }
30
+ }
31
+
32
+ if(sha1($string2compute) == $signature)
33
+ return true;
34
+
35
+ return false;
36
+ }
37
+
38
+ public function checkIfCcExpDateIsValid($customer)
39
+ {
40
+ if(is_int($customer))
41
+ $customer = Mage::getModel('customer/customer')->load($customer);
42
+
43
+ $expDate = $customer->getHipayCcExpDate();
44
+ $alias = $customer->getHipayAliasOneclick();
45
+ if(!empty($expDate) && !empty($alias))
46
+ {
47
+ list($expMonth,$expYear) = explode("-", $expDate);
48
+ $today = new Zend_Date(Mage::app()->getLocale()->storeTimeStamp());
49
+
50
+ $currentYear = (int)$today->getYear()->toString("YY");
51
+ $currentMonth = (int)$today->getMonth()->toString("MM");
52
+
53
+ if($currentYear > (int)$expYear)
54
+ return false;
55
+
56
+ if($currentYear == (int)$expYear && $currentMonth > (int)$expMonth)
57
+ return false;
58
+
59
+ return true;
60
+
61
+ }
62
+
63
+ return false;
64
+ }
65
+
66
+ /**
67
+ *
68
+ * @param Mage_Customer_Model_Customer $customer
69
+ * @param Allopass_Hipay_Model_Api_Response_Gateway $response
70
+ */
71
+ public function responseToCustomer($customer,$response,$isRecurring = false)
72
+ {
73
+
74
+ $paymentMethod = $response->getPaymentMethod();
75
+ $token = isset($paymentMethod['token']) ? $paymentMethod['token'] : $response->getData('cardtoken');
76
+
77
+ if($isRecurring)
78
+ $customer->setHipayAliasRecurring($token);
79
+ else
80
+ $customer->setHipayAliasOneclick($token );
81
+
82
+ if(isset($paymentMethod['card_expiry_month']) && $paymentMethod['card_expiry_year'])
83
+ $customer->setHipayCcExpDate($paymentMethod['card_expiry_month'] . "-" . $paymentMethod['card_expiry_year'] );
84
+ else
85
+ $customer->setHipayCcExpDate(substr($response->getData('cardexpiry'), 4,2) . "-" . substr($response->getData('cardexpiry'), 0,4) );
86
+ $customer->setHipayCcNumberEnc(isset($paymentMethod['pan']) ? $paymentMethod['pan'] : $response->getData('cardpan'));
87
+ $customer->setHipayCcType(isset($paymentMethod['brand']) ? strtolower($paymentMethod['brand']) : strtolower($response->getData('cardbrand')));
88
+
89
+
90
+ $customer->getResource()->saveAttribute($customer, 'hipay_alias_oneclick');
91
+ $customer->getResource()->saveAttribute($customer, 'hipay_cc_exp_date');
92
+ $customer->getResource()->saveAttribute($customer, 'hipay_cc_number_enc');
93
+ $customer->getResource()->saveAttribute($customer, 'hipay_cc_type');
94
+
95
+ return $this;
96
+ }
97
+
98
+ public function reAddToCart($incrementId) {
99
+
100
+ $cart = Mage::getSingleton('checkout/cart');
101
+ $order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
102
+
103
+ if ($order->getId()) {
104
+ $items = $order->getItemsCollection();
105
+ foreach ($items as $item) {
106
+ try {
107
+ $cart->addOrderItem($item);
108
+ } catch (Mage_Core_Exception $e) {
109
+ if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
110
+ Mage::getSingleton('checkout/session')->addNotice($e->getMessage());
111
+ } else {
112
+ Mage::getSingleton('checkout/session')->addError($e->getMessage());
113
+ }
114
+ } catch (Exception $e) {
115
+ Mage::getSingleton('checkout/session')->addException($e, Mage::helper('checkout')->__('Cannot add the item to shopping cart.')
116
+ );
117
+ }
118
+ }
119
+ }
120
+
121
+ $cart->save();
122
+ }
123
+
124
+
125
+ /**
126
+ * Return message for gateway transaction request
127
+ *
128
+ * @param Mage_Payment_Model_Info $payment
129
+ * @param string $requestType
130
+ * @param string $lastTransactionId
131
+ * @param float $amount
132
+ * @param string $exception
133
+ * @return bool|string
134
+ */
135
+ public function getTransactionMessage($payment, $requestType, $lastTransactionId, $amount = false,
136
+ $exception = false,$additionalMessage = false
137
+ ) {
138
+ return $this->getExtendedTransactionMessage(
139
+ $payment, $requestType, $lastTransactionId, $amount, $exception,$additionalMessage
140
+ );
141
+ }
142
+
143
+ /**
144
+ * Return message for gateway transaction request
145
+ *
146
+ * @param Mage_Payment_Model_Info $payment
147
+ * @param string $requestType
148
+ * @param string $lastTransactionId
149
+ * @param float $amount
150
+ * @param string $exception
151
+ * @param string $additionalMessage Custom message, which will be added to the end of generated message
152
+ * @return bool|string
153
+ */
154
+ public function getExtendedTransactionMessage($payment, $requestType, $lastTransactionId, $amount = false,
155
+ $exception = false, $additionalMessage = false
156
+ ) {
157
+ $operation = 'Operation: ' . $requestType;// $this->_getOperation($requestType);
158
+
159
+ if (!$operation) {
160
+ return false;
161
+ }
162
+
163
+ if ($amount) {
164
+ $amount = $this->__('amount: %s', $this->_formatPrice($payment, $amount));
165
+ }
166
+
167
+ if ($exception) {
168
+ $result = $this->__('failed');
169
+ } else {
170
+ $result = $this->__('successful');
171
+ }
172
+
173
+ $card = $this->__('Credit Card: xxxx-%s', $payment->getCcLast4());
174
+
175
+ $pattern = '%s - %s. %s %s.';
176
+ $texts = array($operation,$result,$card, $amount);
177
+
178
+ if (!is_null($lastTransactionId)) {
179
+ $pattern .= ' %s.';
180
+ $texts[] = $this->__('Hipay Transaction ID %s', $lastTransactionId);
181
+ }
182
+
183
+ if ($additionalMessage) {
184
+ $pattern .= ' %s.';
185
+ $texts[] = $additionalMessage;
186
+ }
187
+ $pattern .= ' %s';
188
+ $texts[] = $exception;
189
+
190
+ return call_user_func_array(array($this, '__'), array_merge(array($pattern), $texts));
191
+ }
192
+
193
+ /**
194
+ * Format price with currency sign
195
+ * @param Mage_Payment_Model_Info $payment
196
+ * @param float $amount
197
+ * @return string
198
+ */
199
+ protected function _formatPrice($payment, $amount)
200
+ {
201
+ return $payment->getOrder()->getBaseCurrency()->formatTxt($amount);
202
+ }
203
+ }
app/code/community/Allopass/Hipay/Model/Api/Http/Client/Adapter/Curl.php ADDED
@@ -0,0 +1,483 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+
4
+
5
+ /**
6
+ * @see Zend_Uri_Http
7
+ */
8
+ #require_once 'Zend/Uri/Http.php';
9
+
10
+ /**
11
+ * @see Zend_Http_Client_Adapter_Interface
12
+ */
13
+ #require_once 'Zend/Http/Client/Adapter/Interface.php';
14
+ /**
15
+ * @see Zend_Http_Client_Adapter_Stream
16
+ */
17
+ #require_once 'Zend/Http/Client/Adapter/Stream.php';
18
+
19
+ /**
20
+ * An adapter class for Zend_Http_Client based on the curl extension.
21
+ * Curl requires libcurl. See for full requirements the PHP manual: http://php.net/curl
22
+ *
23
+ */
24
+ class Allopass_Hipay_Model_Api_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interface, Allopass_Hipay_Model_Api_Http_Client_Adapter_Stream
25
+ {
26
+ /**
27
+ * Parameters array
28
+ *
29
+ * @var array
30
+ */
31
+ protected $_config = array();
32
+
33
+ /**
34
+ * What host/port are we connected to?
35
+ *
36
+ * @var array
37
+ */
38
+ protected $_connected_to = array(null, null);
39
+
40
+ /**
41
+ * The curl session handle
42
+ *
43
+ * @var resource|null
44
+ */
45
+ protected $_curl = null;
46
+
47
+ /**
48
+ * List of cURL options that should never be overwritten
49
+ *
50
+ * @var array
51
+ */
52
+ protected $_invalidOverwritableCurlOptions;
53
+
54
+ /**
55
+ * Response gotten from server
56
+ *
57
+ * @var string
58
+ */
59
+ protected $_response = null;
60
+
61
+ /**
62
+ * Stream for storing output
63
+ *
64
+ * @var resource
65
+ */
66
+ protected $out_stream;
67
+
68
+ /**
69
+ * Adapter constructor
70
+ *
71
+ * Config is set using setConfig()
72
+ *
73
+ * @return void
74
+ * @throws Zend_Http_Client_Adapter_Exception
75
+ */
76
+ public function __construct()
77
+ {
78
+ if (!extension_loaded('curl')) {
79
+ #require_once 'Zend/Http/Client/Adapter/Exception.php';
80
+ throw new Zend_Http_Client_Adapter_Exception('cURL extension has to be loaded to use this Zend_Http_Client adapter.');
81
+ }
82
+ $this->_invalidOverwritableCurlOptions = array(
83
+ CURLOPT_HTTPGET,
84
+ CURLOPT_POST,
85
+ CURLOPT_PUT,
86
+ CURLOPT_CUSTOMREQUEST,
87
+ CURLOPT_HEADER,
88
+ CURLOPT_RETURNTRANSFER,
89
+ CURLOPT_HTTPHEADER,
90
+ CURLOPT_POSTFIELDS,
91
+ CURLOPT_INFILE,
92
+ CURLOPT_INFILESIZE,
93
+ CURLOPT_PORT,
94
+ CURLOPT_MAXREDIRS,
95
+ CURLOPT_CONNECTTIMEOUT,
96
+ CURL_HTTP_VERSION_1_1,
97
+ CURL_HTTP_VERSION_1_0,
98
+ );
99
+ }
100
+
101
+ /**
102
+ * Set the configuration array for the adapter
103
+ *
104
+ * @throws Zend_Http_Client_Adapter_Exception
105
+ * @param Zend_Config | array $config
106
+ * @return Zend_Http_Client_Adapter_Curl
107
+ */
108
+ public function setConfig($config = array())
109
+ {
110
+ if ($config instanceof Zend_Config) {
111
+ $config = $config->toArray();
112
+
113
+ } elseif (! is_array($config)) {
114
+ #require_once 'Zend/Http/Client/Adapter/Exception.php';
115
+ throw new Zend_Http_Client_Adapter_Exception(
116
+ 'Array or Zend_Config object expected, got ' . gettype($config)
117
+ );
118
+ }
119
+
120
+ if(isset($config['proxy_user']) && isset($config['proxy_pass'])) {
121
+ $this->setCurlOption(CURLOPT_PROXYUSERPWD, $config['proxy_user'].":".$config['proxy_pass']);
122
+ unset($config['proxy_user'], $config['proxy_pass']);
123
+ }
124
+
125
+ foreach ($config as $k => $v) {
126
+ $option = strtolower($k);
127
+ switch($option) {
128
+ case 'proxy_host':
129
+ $this->setCurlOption(CURLOPT_PROXY, $v);
130
+ break;
131
+ case 'proxy_port':
132
+ $this->setCurlOption(CURLOPT_PROXYPORT, $v);
133
+ break;
134
+ default:
135
+ $this->_config[$option] = $v;
136
+ break;
137
+ }
138
+ }
139
+
140
+ return $this;
141
+ }
142
+
143
+ /**
144
+ * Retrieve the array of all configuration options
145
+ *
146
+ * @return array
147
+ */
148
+ public function getConfig()
149
+ {
150
+ return $this->_config;
151
+ }
152
+
153
+ /**
154
+ * Direct setter for cURL adapter related options.
155
+ *
156
+ * @param string|int $option
157
+ * @param mixed $value
158
+ * @return Zend_Http_Adapter_Curl
159
+ */
160
+ public function setCurlOption($option, $value)
161
+ {
162
+ if (!isset($this->_config['curloptions'])) {
163
+ $this->_config['curloptions'] = array();
164
+ }
165
+ $this->_config['curloptions'][$option] = $value;
166
+ return $this;
167
+ }
168
+
169
+ /**
170
+ * Initialize curl
171
+ *
172
+ * @param string $host
173
+ * @param int $port
174
+ * @param boolean $secure
175
+ * @return void
176
+ * @throws Zend_Http_Client_Adapter_Exception if unable to connect
177
+ */
178
+ public function connect($host, $port = 80, $secure = false)
179
+ {
180
+ // If we're already connected, disconnect first
181
+ if ($this->_curl) {
182
+ $this->close();
183
+ }
184
+
185
+ // If we are connected to a different server or port, disconnect first
186
+ if ($this->_curl
187
+ && is_array($this->_connected_to)
188
+ && ($this->_connected_to[0] != $host
189
+ || $this->_connected_to[1] != $port)
190
+ ) {
191
+ $this->close();
192
+ }
193
+
194
+ // Do the actual connection
195
+ $this->_curl = curl_init();
196
+ if ($port != 80) {
197
+ curl_setopt($this->_curl, CURLOPT_PORT, intval($port));
198
+ }
199
+
200
+ // Set timeout
201
+ curl_setopt($this->_curl, CURLOPT_CONNECTTIMEOUT, $this->_config['timeout']);
202
+
203
+ // Set Max redirects
204
+ curl_setopt($this->_curl, CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
205
+
206
+ if (!$this->_curl) {
207
+ $this->close();
208
+
209
+ #require_once 'Zend/Http/Client/Adapter/Exception.php';
210
+ throw new Zend_Http_Client_Adapter_Exception('Unable to Connect to ' . $host . ':' . $port);
211
+ }
212
+
213
+ if ($secure !== false) {
214
+ // Behave the same like Zend_Http_Adapter_Socket on SSL options.
215
+ if (isset($this->_config['sslcert'])) {
216
+ curl_setopt($this->_curl, CURLOPT_SSLCERT, $this->_config['sslcert']);
217
+ }
218
+ if (isset($this->_config['sslpassphrase'])) {
219
+ curl_setopt($this->_curl, CURLOPT_SSLCERTPASSWD, $this->_config['sslpassphrase']);
220
+ }
221
+ }
222
+
223
+ // Update connected_to
224
+ $this->_connected_to = array($host, $port);
225
+ }
226
+
227
+ /**
228
+ * Send request to the remote server
229
+ *
230
+ * @param string $method
231
+ * @param Zend_Uri_Http $uri
232
+ * @param float $http_ver
233
+ * @param array $headers
234
+ * @param string $body
235
+ * @return string $request
236
+ * @throws Zend_Http_Client_Adapter_Exception If connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option
237
+ */
238
+ public function write($method, $uri, $httpVersion = 1.1, $headers = array(), $body = '')
239
+ {
240
+ // Make sure we're properly connected
241
+ if (!$this->_curl) {
242
+ #require_once 'Zend/Http/Client/Adapter/Exception.php';
243
+ throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected");
244
+ }
245
+
246
+ if ($this->_connected_to[0] != $uri->getHost() || $this->_connected_to[1] != $uri->getPort()) {
247
+ #require_once 'Zend/Http/Client/Adapter/Exception.php';
248
+ throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong host");
249
+ }
250
+
251
+ // set URL
252
+ curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString());
253
+
254
+ // ensure correct curl call
255
+ $curlValue = true;
256
+ switch ($method) {
257
+ case Zend_Http_Client::GET:
258
+ $curlMethod = CURLOPT_HTTPGET;
259
+ break;
260
+
261
+ case Zend_Http_Client::POST:
262
+ $curlMethod = CURLOPT_POST;
263
+ break;
264
+
265
+ case Zend_Http_Client::PUT:
266
+ // There are two different types of PUT request, either a Raw Data string has been set
267
+ // or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
268
+ if(is_resource($body)) {
269
+ $this->_config['curloptions'][CURLOPT_INFILE] = $body;
270
+ }
271
+ if (isset($this->_config['curloptions'][CURLOPT_INFILE])) {
272
+ // Now we will probably already have Content-Length set, so that we have to delete it
273
+ // from $headers at this point:
274
+ foreach ($headers AS $k => $header) {
275
+ if (preg_match('/Content-Length:\s*(\d+)/i', $header, $m)) {
276
+ if(is_resource($body)) {
277
+ $this->_config['curloptions'][CURLOPT_INFILESIZE] = (int)$m[1];
278
+ }
279
+ unset($headers[$k]);
280
+ }
281
+ }
282
+
283
+ if (!isset($this->_config['curloptions'][CURLOPT_INFILESIZE])) {
284
+ #require_once 'Zend/Http/Client/Adapter/Exception.php';
285
+ throw new Zend_Http_Client_Adapter_Exception("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE.");
286
+ }
287
+
288
+ if(is_resource($body)) {
289
+ $body = '';
290
+ }
291
+
292
+ $curlMethod = CURLOPT_PUT;
293
+ } else {
294
+ $curlMethod = CURLOPT_CUSTOMREQUEST;
295
+ $curlValue = "PUT";
296
+ }
297
+ break;
298
+
299
+ case Zend_Http_Client::DELETE:
300
+ $curlMethod = CURLOPT_CUSTOMREQUEST;
301
+ $curlValue = "DELETE";
302
+ break;
303
+
304
+ case Zend_Http_Client::OPTIONS:
305
+ $curlMethod = CURLOPT_CUSTOMREQUEST;
306
+ $curlValue = "OPTIONS";
307
+ break;
308
+
309
+ case Zend_Http_Client::TRACE:
310
+ $curlMethod = CURLOPT_CUSTOMREQUEST;
311
+ $curlValue = "TRACE";
312
+ break;
313
+
314
+ case Zend_Http_Client::HEAD:
315
+ $curlMethod = CURLOPT_CUSTOMREQUEST;
316
+ $curlValue = "HEAD";
317
+ break;
318
+
319
+ default:
320
+ // For now, through an exception for unsupported request methods
321
+ #require_once 'Zend/Http/Client/Adapter/Exception.php';
322
+ throw new Zend_Http_Client_Adapter_Exception("Method currently not supported");
323
+ }
324
+
325
+ if(is_resource($body) && $curlMethod != CURLOPT_PUT) {
326
+ #require_once 'Zend/Http/Client/Adapter/Exception.php';
327
+ throw new Zend_Http_Client_Adapter_Exception("Streaming requests are allowed only with PUT");
328
+ }
329
+
330
+ // get http version to use
331
+ $curlHttp = ($httpVersion == 1.1) ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
332
+
333
+ // mark as HTTP request and set HTTP method
334
+ curl_setopt($this->_curl, $curlHttp, true);
335
+ curl_setopt($this->_curl, $curlMethod, $curlValue);
336
+
337
+ if($this->out_stream) {
338
+ // headers will be read into the response
339
+ curl_setopt($this->_curl, CURLOPT_HEADER, false);
340
+ curl_setopt($this->_curl, CURLOPT_HEADERFUNCTION, array($this, "readHeader"));
341
+ // and data will be written into the file
342
+ curl_setopt($this->_curl, CURLOPT_FILE, $this->out_stream);
343
+ } else {
344
+ // ensure headers are also returned
345
+ curl_setopt($this->_curl, CURLOPT_HEADER, true);
346
+
347
+ // ensure actual response is returned
348
+ curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
349
+ }
350
+
351
+ // set additional headers
352
+ $headers['Accept'] = '';
353
+ curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
354
+
355
+ /**
356
+ * Make sure POSTFIELDS is set after $curlMethod is set:
357
+ * @link http://de2.php.net/manual/en/function.curl-setopt.php#81161
358
+ */
359
+ if ($method == Zend_Http_Client::POST) {
360
+ curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
361
+ } elseif ($curlMethod == CURLOPT_PUT) {
362
+ // this covers a PUT by file-handle:
363
+ // Make the setting of this options explicit (rather than setting it through the loop following a bit lower)
364
+ // to group common functionality together.
365
+