|
2 | 2 |
|
3 | 3 | class Paymentwall_Widget extends Paymentwall_Instance |
4 | 4 | { |
5 | | - const CONTROLLER_PAYMENT_VIRTUAL_CURRENCY = 'ps'; |
6 | | - const CONTROLLER_PAYMENT_DIGITAL_GOODS = 'subscription'; |
7 | | - const CONTROLLER_PAYMENT_CART = 'cart'; |
8 | | - |
9 | | - protected $userId; |
10 | | - protected $widgetCode; |
11 | | - protected $products; |
12 | | - protected $extraParams; |
13 | | - |
14 | | - public function __construct($userId, $widgetCode = '', $products = array(), $extraParams = array()) { |
15 | | - $this->userId = $userId; |
16 | | - $this->widgetCode = $widgetCode; |
17 | | - $this->products = $products; |
18 | | - $this->extraParams = $extraParams; |
19 | | - } |
20 | | - |
21 | | - public function getUrl() |
22 | | - { |
23 | | - $params = array( |
24 | | - 'key' => $this->getPublicKey(), |
25 | | - 'uid' => $this->userId, |
26 | | - 'widget' => $this->widgetCode |
27 | | - ); |
28 | | - |
29 | | - $productsNumber = count($this->products); |
30 | | - |
31 | | - if ($this->getApiType() == Paymentwall_Config::API_GOODS) { |
32 | | - |
33 | | - if (!empty($this->products)) { |
34 | | - |
35 | | - if ($productsNumber == 1) { |
36 | | - |
37 | | - $product = current($this->products); |
38 | | - |
39 | | - if ($product->getTrialProduct() instanceof Paymentwall_Product) { |
40 | | - $postTrialProduct = $product; |
41 | | - $product = $product->getTrialProduct(); |
42 | | - } |
43 | | - |
44 | | - $params['amount'] = $product->getAmount(); |
45 | | - $params['currencyCode'] = $product->getCurrencyCode(); |
46 | | - $params['ag_name'] = $product->getName(); |
47 | | - $params['ag_external_id'] = $product->getId(); |
48 | | - $params['ag_type'] = $product->getType(); |
49 | | - |
50 | | - if ($product->getType() == Paymentwall_Product::TYPE_SUBSCRIPTION) { |
51 | | - $params['ag_period_length'] = $product->getPeriodLength(); |
52 | | - $params['ag_period_type'] = $product->getPeriodType(); |
53 | | - if ($product->isRecurring()) { |
54 | | - |
55 | | - $params['ag_recurring'] = intval($product->isRecurring()); |
56 | | - |
57 | | - if (isset($postTrialProduct)) { |
58 | | - $params['ag_trial'] = 1; |
59 | | - $params['ag_post_trial_external_id'] = $postTrialProduct->getId(); |
60 | | - $params['ag_post_trial_period_length'] = $postTrialProduct->getPeriodLength(); |
61 | | - $params['ag_post_trial_period_type'] = $postTrialProduct->getPeriodType(); |
62 | | - $params['ag_post_trial_name'] = $postTrialProduct->getName(); |
63 | | - $params['post_trial_amount'] = $postTrialProduct->getAmount(); |
64 | | - $params['post_trial_currencyCode'] = $postTrialProduct->getCurrencyCode(); |
65 | | - } |
66 | | - |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - } else { |
71 | | - //TODO: $this->appendToErrors('Only 1 product is allowed in flexible widget call'); |
72 | | - } |
73 | | - |
74 | | - } |
75 | | - |
76 | | - } else if ($this->getApiType() == Paymentwall_Config::API_CART) { |
77 | | - |
78 | | - $index = 0; |
79 | | - foreach ($this->products as $product) { |
80 | | - $params['external_ids[' . $index . ']'] = $product->getId(); |
81 | | - |
82 | | - if (isset($product->amount)) { |
83 | | - $params['prices[' . $index . ']'] = $product->getAmount(); |
84 | | - } |
85 | | - if (isset($product->currencyCode)) { |
86 | | - $params['currencies[' . $index . ']'] = $product->getCurrencyCode(); |
87 | | - } |
88 | | - if (isset($product->name)) { |
89 | | - $params['names[' . $index . ']'] = $product->getName(); |
90 | | - } |
91 | | - |
92 | | - $index++; |
93 | | - } |
94 | | - unset($index); |
95 | | - } |
96 | | - |
97 | | - $params['sign_version'] = $signatureVersion = $this->getDefaultSignatureVersion(); |
98 | | - |
99 | | - if (!empty($this->extraParams['sign_version'])) { |
100 | | - $signatureVersion = $params['sign_version'] = $this->extraParams['sign_version']; |
101 | | - } |
102 | | - |
103 | | - $params = array_merge($params, $this->extraParams); |
104 | | - |
105 | | - $widgetSignatureModel = new Paymentwall_Signature_Widget(); |
106 | | - $params['sign'] = $widgetSignatureModel->calculate( |
107 | | - $params, |
108 | | - $signatureVersion |
109 | | - ); |
110 | | - |
111 | | - return $this->getApiBaseUrl() . '/' . $this->buildController($this->widgetCode) . '?' . http_build_query($params); |
112 | | - } |
113 | | - |
114 | | - public function getHtmlCode($attributes = array()) |
115 | | - { |
116 | | - $defaultAttributes = array( |
117 | | - 'frameborder' => '0', |
118 | | - 'width' => '750', |
119 | | - 'height' => '800' |
120 | | - ); |
121 | | - |
122 | | - $attributes = array_merge($defaultAttributes, $attributes); |
123 | | - |
124 | | - $attributesQuery = ''; |
125 | | - foreach ($attributes as $attr => $value) { |
126 | | - $attributesQuery .= ' ' . $attr . '="' . $value . '"'; |
127 | | - } |
128 | | - |
129 | | - return '<iframe src="' . $this->getUrl() . '" ' . $attributesQuery . '></iframe>'; |
130 | | - |
131 | | - } |
132 | | - |
133 | | - protected function getDefaultSignatureVersion() { |
134 | | - return $this->getApiType() != Paymentwall_Config::API_CART ? Paymentwall_Signature_Abstract::DEFAULT_VERSION : Paymentwall_Signature_Abstract::VERSION_TWO; |
135 | | - } |
136 | | - |
137 | | - protected function buildController($widget = '') |
138 | | - { |
139 | | - $controller = null; |
140 | | - $isPaymentWidget = !preg_match('/^w|s|mw/', $widget); |
141 | | - |
142 | | - if ($this->getApiType()== Paymentwall_Config::API_VC) { |
143 | | - if ($isPaymentWidget) { |
144 | | - $controller = self::CONTROLLER_PAYMENT_VIRTUAL_CURRENCY; |
145 | | - } |
146 | | - } else if ($this->getApiType() == Paymentwall_Config::API_GOODS) { |
147 | | - /** |
148 | | - * @todo cover case with offer widget for digital goods for non-flexible widget call |
149 | | - */ |
150 | | - $controller = self::CONTROLLER_PAYMENT_DIGITAL_GOODS; |
151 | | - } else { |
152 | | - $controller = self::CONTROLLER_PAYMENT_CART; |
153 | | - } |
154 | | - |
155 | | - return $controller; |
156 | | - } |
| 5 | + const CONTROLLER_PAYMENT_VIRTUAL_CURRENCY = 'ps'; |
| 6 | + const CONTROLLER_PAYMENT_DIGITAL_GOODS = 'subscription'; |
| 7 | + const CONTROLLER_PAYMENT_CART = 'cart'; |
| 8 | + |
| 9 | + protected $userId; |
| 10 | + protected $widgetCode; |
| 11 | + protected $products; |
| 12 | + protected $extraParams; |
| 13 | + |
| 14 | + public function __construct($userId, $widgetCode = '', $products = array(), $extraParams = array()) { |
| 15 | + $this->userId = $userId; |
| 16 | + $this->widgetCode = $widgetCode; |
| 17 | + $this->products = $products; |
| 18 | + $this->extraParams = $extraParams; |
| 19 | + } |
| 20 | + |
| 21 | + public function getUrl() |
| 22 | + { |
| 23 | + $params = array( |
| 24 | + 'key' => $this->getPublicKey(), |
| 25 | + 'uid' => $this->userId, |
| 26 | + 'widget' => $this->widgetCode |
| 27 | + ); |
| 28 | + |
| 29 | + $productsNumber = count($this->products); |
| 30 | + |
| 31 | + if ($this->getApiType() == Paymentwall_Config::API_GOODS) { |
| 32 | + |
| 33 | + if (!empty($this->products)) { |
| 34 | + |
| 35 | + if ($productsNumber == 1) { |
| 36 | + |
| 37 | + $product = current($this->products); |
| 38 | + |
| 39 | + if ($product->getTrialProduct() instanceof Paymentwall_Product) { |
| 40 | + $postTrialProduct = $product; |
| 41 | + $product = $product->getTrialProduct(); |
| 42 | + } |
| 43 | + |
| 44 | + $params['amount'] = $product->getAmount(); |
| 45 | + $params['currencyCode'] = $product->getCurrencyCode(); |
| 46 | + $params['ag_name'] = $product->getName(); |
| 47 | + $params['ag_external_id'] = $product->getId(); |
| 48 | + $params['ag_type'] = $product->getType(); |
| 49 | + |
| 50 | + if ($product->getType() == Paymentwall_Product::TYPE_SUBSCRIPTION) { |
| 51 | + $params['ag_period_length'] = $product->getPeriodLength(); |
| 52 | + $params['ag_period_type'] = $product->getPeriodType(); |
| 53 | + if ($product->isRecurring()) { |
| 54 | + |
| 55 | + $params['ag_recurring'] = intval($product->isRecurring()); |
| 56 | + |
| 57 | + if (isset($postTrialProduct)) { |
| 58 | + $params['ag_trial'] = 1; |
| 59 | + $params['ag_post_trial_external_id'] = $postTrialProduct->getId(); |
| 60 | + $params['ag_post_trial_period_length'] = $postTrialProduct->getPeriodLength(); |
| 61 | + $params['ag_post_trial_period_type'] = $postTrialProduct->getPeriodType(); |
| 62 | + $params['ag_post_trial_name'] = $postTrialProduct->getName(); |
| 63 | + $params['post_trial_amount'] = $postTrialProduct->getAmount(); |
| 64 | + $params['post_trial_currencyCode'] = $postTrialProduct->getCurrencyCode(); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + } else { |
| 71 | + //TODO: $this->appendToErrors('Only 1 product is allowed in flexible widget call'); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + } else if ($this->getApiType() == Paymentwall_Config::API_CART) { |
| 77 | + |
| 78 | + $external_ids = array(); |
| 79 | + $prices = array(); |
| 80 | + $currencies = array(); |
| 81 | + $names = array(); |
| 82 | + foreach ($this->products as $product) { |
| 83 | + $external_ids[] = $product->getId(); |
| 84 | + $prices[] = $product->amount ?: 0; |
| 85 | + $currencies[] = $product->currencyCode ?: ''; |
| 86 | + $names[] = $product->name ?: ''; |
| 87 | + } |
| 88 | + $params['external_ids'] = $external_ids; |
| 89 | + if (!empty($prices)) { |
| 90 | + $params['prices'] = $prices; |
| 91 | + } |
| 92 | + if (!empty($currencies)) { |
| 93 | + $params['currencies'] = $currencies; |
| 94 | + } |
| 95 | + if (array_filter($names)) { |
| 96 | + $params['names'] = $names; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + $params['sign_version'] = $signatureVersion = $this->getDefaultSignatureVersion(); |
| 101 | + |
| 102 | + if (!empty($this->extraParams['sign_version'])) { |
| 103 | + $signatureVersion = $params['sign_version'] = $this->extraParams['sign_version']; |
| 104 | + } |
| 105 | + |
| 106 | + $params = array_merge($params, $this->extraParams); |
| 107 | + |
| 108 | + $widgetSignatureModel = new Paymentwall_Signature_Widget(); |
| 109 | + $params['sign'] = $widgetSignatureModel->calculate( |
| 110 | + $params, |
| 111 | + $signatureVersion |
| 112 | + ); |
| 113 | + |
| 114 | + return $this->getApiBaseUrl() . '/' . $this->buildController($this->widgetCode) . '?' . http_build_query($params); |
| 115 | + } |
| 116 | + |
| 117 | + public function getHtmlCode($attributes = array()) |
| 118 | + { |
| 119 | + $defaultAttributes = array( |
| 120 | + 'frameborder' => '0', |
| 121 | + 'width' => '750', |
| 122 | + 'height' => '800' |
| 123 | + ); |
| 124 | + |
| 125 | + $attributes = array_merge($defaultAttributes, $attributes); |
| 126 | + |
| 127 | + $attributesQuery = ''; |
| 128 | + foreach ($attributes as $attr => $value) { |
| 129 | + $attributesQuery .= ' ' . $attr . '="' . $value . '"'; |
| 130 | + } |
| 131 | + |
| 132 | + return '<iframe src="' . $this->getUrl() . '" ' . $attributesQuery . '></iframe>'; |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + protected function getDefaultSignatureVersion() { |
| 137 | + return $this->getApiType() != Paymentwall_Config::API_CART ? Paymentwall_Signature_Abstract::DEFAULT_VERSION : Paymentwall_Signature_Abstract::VERSION_TWO; |
| 138 | + } |
| 139 | + |
| 140 | + protected function buildController($widget = '') |
| 141 | + { |
| 142 | + $controller = null; |
| 143 | + $isPaymentWidget = !preg_match('/^w|s|mw/', $widget); |
| 144 | + |
| 145 | + if ($this->getApiType()== Paymentwall_Config::API_VC) { |
| 146 | + if ($isPaymentWidget) { |
| 147 | + $controller = self::CONTROLLER_PAYMENT_VIRTUAL_CURRENCY; |
| 148 | + } |
| 149 | + } else if ($this->getApiType() == Paymentwall_Config::API_GOODS) { |
| 150 | + /** |
| 151 | + * @todo cover case with offer widget for digital goods for non-flexible widget call |
| 152 | + */ |
| 153 | + $controller = self::CONTROLLER_PAYMENT_DIGITAL_GOODS; |
| 154 | + } else { |
| 155 | + $controller = self::CONTROLLER_PAYMENT_CART; |
| 156 | + } |
| 157 | + |
| 158 | + return $controller; |
| 159 | + } |
157 | 160 | } |
0 commit comments