/
Gateway.php
176 lines (148 loc) · 4.01 KB
/
Gateway.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
<?php
/**
* Gateway
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2019 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Gateways\PayPal
*/
namespace Pronamic\WordPress\Pay\Gateways\PayPal;
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
use Pronamic\WordPress\Pay\Core\PaymentMethods;
use Pronamic\WordPress\Pay\Payments\Payment;
/**
* Gateway
*
* @author Remco Tolsma
* @version 1.0.0
* @since 1.0.0
*/
class Gateway extends Core_Gateway {
/**
* Client.
*
* @var Client
*/
private $client;
/**
* Constructs and initializes an PayPal gateway.
*
* @param Config $config Config.
*/
public function __construct( Config $config ) {
parent::__construct( $config );
$this->set_method( self::METHOD_HTTP_REDIRECT );
// Supported features.
$this->supports = array(
'payment_status_request',
);
// Client.
$this->client = new Client( $config );
}
/**
* Get supported payment methods
*
* @see Core_Gateway::get_supported_payment_methods()
* @return array<string>
*/
public function get_supported_payment_methods() {
return array(
PaymentMethods::PAYPAL,
);
}
/**
* Is payment method required to start transaction?
*
* @see Core_Gateway::payment_method_is_required()
* @return true
*/
public function payment_method_is_required() {
return true;
}
/**
* Start.
*
* @param Payment $payment Payment.
* @return void
* @throws \InvalidArgumentException Throws exception if payment ID or currency is empty.
* @see Plugin::start()
*/
public function start( Payment $payment ) {
/**
* HTML Variables for PayPal Payments Standard
*
* @link https://developer.paypal.com/docs/paypal-payments-standard/integration-guide/Appx-websitestandard-htmlvariables/
* @link https://github.com/easydigitaldownloads/easy-digital-downloads/blob/2.9.26/includes/gateways/paypal-standard.php
*/
$url = $this->config->get_webscr_url();
$variables = new Variables();
$variables->set_business( $this->config->get_email() );
$variables->set_cmd( '_cart' );
$variables->set_upload( true );
/**
* Customer.
*/
$customer = $payment->get_customer();
if ( null !== $customer ) {
$variables->set_optional_value( 'email', $customer->get_email() );
$name = $customer->get_name();
if ( null !== $name ) {
$variables->set_optional_value( 'first_name', $name->get_first_name() );
$variables->set_optional_value( 'last_name', $name->get_last_name() );
}
}
/**
* Currency
*/
$currency_code = $payment->get_total_amount()->get_currency()->get_alphabetic_code();
if ( null !== $currency_code ) {
$variables->set_value( 'currency_code', $currency_code );
}
/**
* Items.
*/
$x = 1;
$variables->set_value( 'item_name_' . $x, 'Payment ' . $payment->get_id() );
$variables->set_value( 'amount_' . $x, $payment->get_total_amount()->get_value() );
/**
* Return.
*/
$variables->set_value( 'return', $payment->get_return_url() );
/**
* Notify URL.
*/
$notify_url = \rest_url( Integration::REST_ROUTE_NAMESPACE . '/ipn-listener' );
/**
* Filters the PayPal notify URL.
*
* If you want to debug the PayPal notify URL you can use this filter
* to override the report URL. You could for example use a service like
* https://webhook.site/ to inspect the notify requests from PayPal.
*
* @param string $notify_url PayPal notify URL.
*/
$notify_url = \apply_filters( 'pronamic_pay_paypal_notify_url', $notify_url );
$variables->set_value( 'notify_url', $notify_url );
/**
* Custom.
*
* Pass-through variable for your own tracking purposes, which buyers do not see.
*/
$variables->set_value( 'custom', $payment->get_id() );
/**
* URL.
*/
$url = \add_query_arg( $variables->get_array(), $url );
$payment->set_action_url( $url );
}
/**
* Update status of the specified payment.
*
* @param Payment $payment Payment.
* @return void
*/
public function update_status( Payment $payment ) {
// @todo handle status updates
}
}