17 require_once SQ_CORE_PACKAGE_PATH.
'/page/page.inc';
38 public static $CUSTOM_PARAM_SPLIT_STR =
'::';
46 private static $_post_url = Array(
47 'live' =>
'https://www.paypal.com/cgi-bin/webscr',
48 'sandbox' =>
'https://www.sandbox.paypal.com/cgi-bin/webscr',
59 parent::__construct($assetid);
74 $paypal_config_id = $this->
attr(
'paypal_config_id');
75 if (empty($paypal_config_id)) {
76 trigger_error(
'Paypal Configuration is not specified for this asset.', E_USER_WARNING);
80 $paypal_config = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($paypal_config_id);
81 require_once $paypal_config->attr(
'encryption_lib_path');
83 $paypal_account_id = $this->
attr(
'paypal_account_id');
84 if (empty($paypal_account_id)) {
85 trigger_error(
'Paypal Business Account is not specified for this asset.', E_USER_WARNING);
89 $paypal_account = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($paypal_account_id);
92 $data = $this->_concatButtonData($paypal_config, $paypal_account);
94 $encryptedButton = PPCrypto::signAndEncrypt($data, $paypal_account->attr(
'public_cert_path'), $paypal_account->attr(
'private_key_path'),
'', $paypal_account->attr(
'paypal_cert_path'));
96 if (!$encryptedButton[
'status']) {
97 trigger_error(
'FAIL TO ENCRYPT BUTTON. ERROR: '.$encryptedButton[
'error_msg'], E_USER_WARNING);
101 $post_url = self::$_post_url[$paypal_account->attr(
'account_type')];
104 if (($this->
attr(
'button_type') ==
'_cart') || ($this->
attr(
'button_type') ==
'_view_cart')) {
105 $target =
'target="'.$this->attr(
'cart_target_window').
'"';
108 $content = <<<HEREDOC
109 <form $target action=
"{$post_url}" method=
"post">
110 <input
type=
"hidden" name=
"cmd" value=
"_s-xclick">
111 <input type=
"hidden" name=
"encrypted" value=
"-----BEGIN PKCS7-----{$encryptedButton['encryptedData']}-----END PKCS7-----">
112 <input type=
"image" src=
"{$this->attr('image_url')}" border=
"0" name=
"submit" alt=
"Paypal payment button">
127 private function _concatButtonData($paypal_config, $paypal_business_account)
130 $cmd = $this->
attr(
'button_type');
131 if ($cmd ==
'_view_cart') {
135 $data =
'cmd='.$cmd.
"\n";
137 if ($cmd ==
'_cart') {
138 if ($this->
attr(
'button_type') ==
'_cart') {
139 $data .=
'add=1'.
"\n";
141 $data .=
'display=1'.
"\n";
144 $shopping_url = trim($this->
attr(
'shopping_url'));
145 if ($shopping_url !=
'') {
146 $data .=
'shopping_url='.$shopping_url.
"\n";
150 $data .=
'business='.$paypal_business_account->attr(
'account_id').
"\n";
151 $data .=
'cert_id='.$paypal_business_account->attr(
'public_cert_id').
"\n";
153 if ($this->
attr(
'button_type') !=
'_view_cart') {
154 $item_name = $this->
attr(
'item_name');
156 replace_global_keywords($item_name);
157 $data .=
'item_name='.$item_name.
"\n";
159 if ($this->
attr(
'item_id') !=
'') {
160 $data .=
'item_number='.$this->attr(
'item_id').
"\n";
163 $price = trim($this->
attr(
'price'));
166 replace_global_keywords($price);
167 $data .=
'amount='.$price.
"\n";
170 $data .=
'currency_code='.$this->attr(
'currency').
"\n";
172 if (($this->
attr(
'button_type') ==
'_xclick') && $this->
attr(
'undefined_quantity')) {
173 $data .=
'undefined_quantity=1'.
"\n";
176 $data .=
'charset='.SQ_CONF_DEFAULT_CHARACTER_SET.
"\n";
179 $custom = $this->
attr(
'custom_var');
181 replace_global_keywords($custom);
182 $custom = urlencode($custom);
186 $custom_var = $this->
id.self::$CUSTOM_PARAM_SPLIT_STR.$custom;
189 $notify_url_query = parse_url($this->
attr(
'notify_url'), PHP_URL_QUERY);
190 if (is_null($notify_url_query)) {
191 $notify_url_query =
'';
198 $custom_hash = md5($paypal_config->attr(
'custom_param_secret_str').$custom_var.$paypal_config->attr(
'custom_param_secret_str').$notify_url_query);
200 $data .=
'custom='.$custom_hash.self::$CUSTOM_PARAM_SPLIT_STR.$custom_var.
"\n";
202 if ($this->
attr(
'notify_url') !=
'') {
203 $data .=
'notify_url='.$this->attr(
'notify_url').
"\n";
206 if ($this->
attr(
'return_url') !=
'') {
207 $data .=
'return='.$this->attr(
'return_url').
"\n";
210 if ($this->
attr(
'cancel_return_url') !=
'') {
211 $data .=
'cancel_return='.$this->attr(
'cancel_return_url').
"\n";
215 $data .= $this->
attr(
'extra_vars');