PluginProbe
PayPlug for WooCommerce (Official) / 1.2.6
PayPlug for WooCommerce (Official) v1.2.6
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) 1.2.6, at src/Gateway/PayplugApi.php

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