-
Notifications
You must be signed in to change notification settings - Fork 3
/
gateway-buckaroo-paypal.php
154 lines (136 loc) · 4.82 KB
/
gateway-buckaroo-paypal.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
<?php
require_once __DIR__ . '/library/api/paymentmethods/buckaroopaypal/buckaroopaypal.php';
/**
* @package Buckaroo
*/
class WC_Gateway_Buckaroo_Paypal extends WC_Gateway_Buckaroo {
const PAYMENT_CLASS = BuckarooPayPal::class;
public $sellerprotection;
protected $express_order_id = null;
public function __construct() {
$this->id = 'buckaroo_paypal';
$this->title = 'PayPal';
$this->has_fields = false;
$this->method_title = 'Buckaroo PayPal';
$this->setIcon( '24x24/paypal.gif', 'svg/paypal.svg' );
parent::__construct();
$this->addRefundSupport();
}
/**
* Init class fields from settings
*
* @return void
*/
protected function setProperties() {
parent::setProperties();
$this->sellerprotection = $this->get_option( 'sellerprotection', 'TRUE' );
}
/**
* Can the order be refunded
*
* @param integer $order_id
* @param integer $amount defaults to null
* @param string $reason
* @return callable|string function or error
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
return $this->processDefaultRefund( $order_id, $amount, $reason );
}
private function set_order_contribution( WC_Order $order ) {
$prefix = (string) apply_filters(
'wc_order_attribution_tracking_field_prefix',
'wc_order_attribution_'
);
// Remove leading and trailing underscores.
$prefix = trim( $prefix, '_' );
// Ensure the prefix ends with _, and set the prefix.
$prefix = "_{$prefix}_";
$order->add_meta_data( $prefix . 'source_type', 'typein' );
$order->add_meta_data( $prefix . 'utm_source', '(direct)' );
$order->save();
}
/**
* Process payment
*
* @param integer $order_id
* @return callable fn_buckaroo_process_response()
*/
public function process_payment( $order_id ) {
$order = getWCOrder( $order_id );
/** @var BuckarooPayPal */
$paypal = $this->createDebitRequest( $order );
$order_details = new Buckaroo_Order_Details( $order );
$customVars = array();
// set paypal express
if ( $this->express_order_id !== null ) {
$this->set_order_contribution( $order );
$customVars = array_merge(
$customVars,
array( 'PayPalOrderId' => $this->express_order_id )
);
}
if ( $this->sellerprotection == 'TRUE' ) {
$paypal->sellerprotection = 1;
$address = $order_details->getShippingAddressComponents();
$customVars = array_merge(
$customVars,
array(
'CustomerName' => $order_details->getShipping( 'first_name' ) . ' ' . $order_details->getShipping( 'last_name' ),
'ShippingPostalCode' => $order_details->getShipping( 'postcode' ),
'ShippingCity' => $order_details->getShipping( 'city' ),
'ShippingStreet' => $address['street'],
'ShippingHouse' => $address['house_number'],
'StateOrProvince' => $order_details->getShipping( 'state' ),
'Country' => $order_details->getShipping( 'country' ),
)
);
}
$response = $paypal->Pay( $customVars );
return fn_buckaroo_process_response( $this, $response );
}
/**
* Add fields to the form_fields() array, specific to this page.
*
* @access public
*/
public function init_form_fields() {
parent::init_form_fields();
$this->form_fields['sellerprotection'] = array(
'title' => __( 'Seller Protection', 'wc-buckaroo-bpe-gateway' ),
'type' => 'select',
'description' => __( 'Sends customer address information to PayPal to enable PayPal seller protection.', 'wc-buckaroo-bpe-gateway' ),
'options' => array(
'TRUE' => __( 'Enabled', 'wc-buckaroo-bpe-gateway' ),
'FALSE' => __( 'Disabled', 'wc-buckaroo-bpe-gateway' ),
),
'default' => 'TRUE',
);
$this->form_fields['express_merchant_id'] = array(
'title' => __( 'PayPal express merchant id', 'wc-buckaroo-bpe-gateway' ),
'type' => 'text',
'description' => __( 'PayPal merchant id required for paypal express', 'wc-buckaroo-bpe-gateway' ),
);
$this->form_fields['express'] = array(
'title' => __( 'PayPal express', 'wc-buckaroo-bpe-gateway' ),
'type' => 'multiselect',
'description' => __( 'Enable PayPal express for the following pages.', 'wc-buckaroo-bpe-gateway' ),
'options' => array(
Buckaroo_Paypal_Express::LOCATION_NONE => __( 'None', 'wc-buckaroo-bpe-gateway' ),
Buckaroo_Paypal_Express::LOCATION_PRODUCT => __( 'Product page', 'wc-buckaroo-bpe-gateway' ),
Buckaroo_Paypal_Express::LOCATION_CART => __( 'Cart page', 'wc-buckaroo-bpe-gateway' ),
Buckaroo_Paypal_Express::LOCATION_CHECKOUT => __( 'Checkout page', 'wc-buckaroo-bpe-gateway' ),
),
'default' => 'none',
);
}
/**
* Set paypal express id
*
* @param string $express_order_id
*
* @return void
*/
public function set_express_order_id( $express_order_id ) {
$this->express_order_id = $express_order_id;
}
}