PluginProbe
PayPlug for WooCommerce (Official) / 2.9.0
PayPlug for WooCommerce (Official) v2.9.0
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Gateway / PayplugApi.php

PayplugApi.php in PayPlug for WooCommerce (Official) 2.9.0, at src/Gateway/PayplugApi.php

232 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Payplug\PayplugWoocommerce\Gateway;
4
5 // Exit if accessed directly
6 if (!defined('ABSPATH')) {
7 exit;
8 }
9
10 use Payplug\Exception\BadRequestException;
11 use Payplug\Exception\NotFoundException;
12 use Payplug\Exception\PayplugServerException;
13 use Payplug\Exception\UnauthorizedException;
14 use Payplug\Exception\ForbiddenException;
15 use Payplug\Payplug;
16 use Payplug\Core\HttpClient;
17 use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper;
18
19 /**
20 * Handle calls to PayPlug PHP client.
21 *
22 * @package Payplug\PayplugWoocommerce\Gateway
23 */
24 class PayplugApi {
25
26 /**
27 * @var PayplugGateway
28 */
29 protected $gateway;
30
31 /**
32 * PayplugApi constructor.
33 *
34 * @param PayplugGateway $gateway
35 */
36 public function __construct( $gateway ) {
37 $this->gateway = $gateway;
38 }
39
40 /**
41 * Configure PayPlug client.
42 */
43 public function init() {
44 $current_mode = $this->gateway->get_current_mode();
45 $key = $this->gateway->get_api_key( $current_mode );
46
47 Payplug::init(array(
48 'secretKey' => $key,
49 'apiVersion' => "2019-08-06",
50 ));
51 HttpClient::setDefaultUserAgentProduct(
52 'PayPlug-WooCommerce',
53 PAYPLUG_GATEWAY_VERSION,
54 sprintf( 'WooCommerce/%s', WC()->version )
55 );
56 }
57
58 /**
59 * Retrieve payment data from PayPlug API.
60 *
61 * @param string $transaction_id
62 *
63 * @return null|\Payplug\Resource\Payment
64 * @throws \Payplug\Exception\ConfigurationException
65 */
66 public function payment_retrieve( $transaction_id ) {
67 return $this->do_request_with_fallback( '\Payplug\Payment::retrieve', $transaction_id );
68 }
69
70 /**
71 * Create a payment.
72 *
73 * @param array $data
74 *
75 * @return null|\Payplug\Resource\Payment
76 */
77 public function payment_create( $data ) {
78 return $this->do_request( '\Payplug\Payment::create', [ $data ] );
79 }
80
81 /**
82 * Retrieve all refunds associated with a payment.
83 *
84 * @param string $transaction_id
85 *
86 * @return \Payplug\Resource\Refund[]
87 * @throws \Payplug\Exception\ConfigurationException
88 */
89 public function refund_list( $transaction_id ) {
90 return $this->do_request_with_fallback( '\Payplug\Refund::listRefunds', $transaction_id );
91 }
92
93 /**
94 * Retrieve refund data from PayPlug API.
95 *
96 * @param string $transaction_id
97 * @param string $refund_id
98 *
99 * @return \Payplug\Resource\Refund
100 * @throws \Payplug\Exception\ConfigurationException
101 */
102 public function refund_retrieve( $transaction_id, $refund_id ) {
103 return $this->do_request_with_fallback( '\Payplug\Refund::retrieve', [ $transaction_id, $refund_id ] );
104 }
105
106 /**
107 * Create a refund.
108 *
109 * @param string $transaction_id
110 * @param array $data
111 *
112 * @return null|\Payplug\Resource\Refund
113 * @throws \Payplug\Exception\ConfigurationException
114 * @author Clément Boirie
115 */
116 public function refund_create( $transaction_id, $data ) {
117 return $this->do_request_with_fallback( '\Payplug\Refund::create', [ $transaction_id, $data ] );
118 }
119
120 /**
121 * Simulate a oney payment
122 *
123 * @return array
124 */
125 public function simulate_oney_payment($price, $oney_type = 'with_fees')
126 {
127 $country = PayplugWoocommerceHelper::get_payplug_merchant_country();
128 $oney_fees = ["x3_" . $oney_type, "x4_" . $oney_type];
129
130 try {
131 try {
132 try {
133 try {
134 $response = $this->do_request('\Payplug\OneySimulation::getSimulations', [[
135 "amount" => intval(floatval($price) * 100),
136 "country" => $country,
137 "operations" => $oney_fees
138 ]]);
139 PayplugWoocommerceHelper::oney_simulation_values($oney_fees, $response);
140 } catch (PayplugServerException $e) {
141 $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
142 }
143 } catch (BadRequestException $e) {
144 $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
145 }
146 } catch (UnauthorizedException $e) {
147 $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
148 }
149 } catch (ForbiddenException $e) {
150 $response = __('Your payment schedule simulation is temporarily unavailable. You will find this information at the payment stage.', 'payplug');
151 }
152
153 return $response;
154 }
155
156 /**
157 * Invoke PayPlug API. If it fail it switch to the other mode and retry the same request.
158 *
159 * @param callable $callback
160 * @param array $params
161 *
162 * @return object
163 * @throws \Payplug\Exception\ConfigurationException
164 */
165 protected function do_request_with_fallback( $callback, $params = [] ) {
166 try {
167 $response = $this->do_request( $callback, $params );
168 } catch ( NotFoundException $e ) {
169 try {
170 $this->switch_mode();
171 $response = $this->do_request( $callback, $params );
172 $this->restore_mode();
173 } catch ( \Exception $e ) {
174 $this->restore_mode();
175 throw $e;
176 }
177 }
178
179 return $response;
180 }
181
182 /**
183 * Invoke PayPlug API.
184 *
185 * @param callable $callback
186 * @param array $params
187 *
188 * @return object
189 */
190 protected function do_request( $callback, $params = [] ) {
191
192 if ( ! is_array( $params ) ) {
193 $params = [ $params ];
194 }
195
196 return call_user_func_array( $callback, $params );
197 }
198
199 /**
200 * Reconfigure PayPlug client with new secret keys.
201 *
202 * @throws \Payplug\Exception\ConfigurationException
203 */
204 protected function switch_mode() {
205 $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test';
206 $new_key = $this->gateway->get_api_key( $switched_mode );
207 if ( empty( $new_key ) ) {
208 throw new \Exception( sprintf(
209 'No secret key available for the %s mode', $switched_mode
210 ) );
211 }
212
213 Payplug::setSecretKey( $new_key );
214 }
215
216 /**
217 * Restore PayPlug client secret keys for the current mode.
218 *
219 * @throws \Payplug\Exception\ConfigurationException
220 */
221 protected function restore_mode() {
222 $key = $this->gateway->get_api_key( $this->gateway->get_current_mode() );
223 if ( empty( $key ) ) {
224 throw new \Exception( sprintf(
225 'No secret key available for the %s mode', $this->gateway->get_current_mode()
226 ) );
227 }
228
229 Payplug::setSecretKey( $key );
230 }
231 }
232