-
Notifications
You must be signed in to change notification settings - Fork 1
/
klarna-partpayment.merchant.php
182 lines (151 loc) · 5.38 KB
/
klarna-partpayment.merchant.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
global $num, $klarna_gateways;
require_once( 'klarna_library/WPKlarna.php');
require_once( 'klarna_library/WPKlarnaHTML.php');
$Klarna = new WPKlarna('part');
$num = count( $klarna_gateways ) + 1;
$klarna_gateways[$num] = array(
'name' => 'Klarna Part Payment' . $Klarna->updateMessage,
'api_version' => 2.0,
'class_name' => 'wpsc_merchant_klarna_part',
'display_name' => 'Klarna Part Payment',
'requirements' => array('php_version' => 5.0),
'form' => 'form_klarna_part',
'submit_function' => 'submit_klarna_part',
'internalname' => 'wpsc_merchant_klarna_part'
);
$pclass_var = ( $Klarna->enabled && ! is_admin() && is_object( $Klarna ) ) ? $Klarna->getPClasses( KlarnaPClass::ACCOUNT ) : 'set';
if ( ! is_admin() && ( 'set' == $pclass_var || empty( $pclass_var ) ) )
unset( $klarna_gateways[$num] );
if ( $Klarna->enabled && ! is_admin() ) {
global $gateway_checkout_form_fields;
$gateway_checkout_form_fields['wpsc_merchant_klarna_part'] = $Klarna->getCheckoutForm();
$payment_gateway_names = (array) get_option('payment_gateway_names');
$payment_gateway_names['wpsc_merchant_klarna_part'] = $Klarna->getTitle();
update_option('payment_gateway_names', $payment_gateway_names);
}
class wpsc_merchant_klarna_part extends wpsc_merchant {
/**
* The Klarna API
*
* @var WPKlarna
**/
var $Klarna;
/**
* Constructor
*
* @return void
* @author Niklas Malmgren
**/
public function __construct($purchase_id = null, $is_receiving = false) {
parent::__construct($purchase_id, $is_receiving);
$this->Klarna = new WPKlarna('part');
}
/**
* Run when the order is submitted
*
* @return void
* @author Niklas Malmgren
**/
public function submit() {
global $wpdb;
$result = $this->Klarna->checkoutSubmit(&$this);
if($result === false) {
$this->Klarna->deleteWPOrder($this->purchase_id);
$this->return_to_checkout();
exit();
}
$this->set_purchase_processed_by_purchid($this->Klarna->getKlarnaOption('order_status'));
switch($result[1]) {
case KlarnaFlags::ACCEPTED:
$orderNotes = 'Order is APPROVED by Klarna.';
break;
case KlarnaFlags::PENDING:
$orderNotes = 'Order is PENDING APPROVAL by Klarna. Please visit Klarna Online for the latest status on this order.';
break;
case KlarnaFlags::DENIED:
$orderNotes = 'Order is DENIED by Klarna.';
break;
default:
$orderNotes = 'Unknown response from Klarna.';
break;
}
$orderNotes .= "\n";
$klarnaInvoiceNumber = $result[0];
$orderNotes .= 'Klarna invoice number: ' . $klarnaInvoiceNumber;
$wpdb->update(
WPSC_TABLE_PURCHASE_LOGS,
array('transactid' => $klarnaInvoiceNumber, 'notes' => $orderNotes),
array('id' => absint($this->purchase_id))
);
$this->go_to_transaction_results($this->cart_data['session_id']);
exit();
}
}
/**
* Handles submission of settings form
*
* @return void
* @author Niklas Malmgren
**/
function submit_klarna_part() {
// Make sure that payment option name doesn't get set due to some user tomfoolery
$payment_gateway_names = (array)get_option('payment_gateway_names');
$payment_gateway_names['wpsc_merchant_klarna_part'] = '';
update_option('payment_gateway_names', $payment_gateway_names);
WPKlarnaHTML::saveSettings('part');
}
/**
* Returns the settings forms
*
* @return string
* @author Niklas Malmgren
**/
function form_klarna_part() {
WPKlarnaHTML::setDefaults('part');
return WPKlarnaHTML::getSettingsForm('part');
}
/**
* Print javscript that forces a page reload when the billing country is changed. This is because
* we need to know if the Klarna module should be enabled (if shipping == billing).
*
* @return void
* @author Niklas Malmgren
**/
if(!function_exists('klarnaForceReload')) {
function klarnaForceReload() {
$str = <<<EOF
<script type='text/javascript'>
jQuery(document).ready(function (){
jQuery('select[title="billingcountry"]').bind('change', function(){
window.setTimeout('window.location = window.location.href', 500);
});
});
</script>
EOF;
print($str);
}
add_action('wpsc_bottom_of_shopping_cart', 'klarnaForceReload');
}
function klarnaPartPaymentBox($productID = 0) {
global $wp_query;
$activated_gateways = get_option('custom_gateway_options');
if(!in_array('wpsc_merchant_klarna_part', $activated_gateways))
return;
if(!$productID)
return;
if($wp_query->is_single == 1 && get_option('klarna_part_product_view') != 'on')
return;
if($wp_query->is_single != 1 && get_option('klarna_part_product_gallery_view') != 'on')
return;
$price = wpsc_calculate_price($productID);
if($price <= 0)
return;
if(!WPKlarna::getCustomerCountry())
return;
$Klarna = new WPKlarna('part', 'product', $price);
if($Klarna->enabled)
print($Klarna->getPartPaymentBox($price));
}
add_action('wpsc_product_before_description', 'klarnaPartPaymentBox');
add_filter('wpsc_pre_transaction_results', array(&$Klarna, 'getKlarnaInvoiceNumberInfo'));