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

210 lines 5.0 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 $response = $this->do_request('\Payplug\OneySimulation::getSimulations', [[
126 "amount" => (int) $price * 100,
127 "country" => $country['country'],
128 "operations" => $oney_fees
129 ]]);
130 PayplugWoocommerceHelper::oney_simulation_values($oney_fees, $response);
131 return $response;
132 }
133
134 /**
135 * Invoke PayPlug API. If it fail it switch to the other mode and retry the same request.
136 *
137 * @param callable $callback
138 * @param array $params
139 *
140 * @return object
141 * @throws \Payplug\Exception\ConfigurationException
142 */
143 protected function do_request_with_fallback( $callback, $params = [] ) {
144 try {
145 $response = $this->do_request( $callback, $params );
146 } catch ( NotFoundException $e ) {
147 try {
148 $this->switch_mode();
149 $response = $this->do_request( $callback, $params );
150 $this->restore_mode();
151 } catch ( \Exception $e ) {
152 $this->restore_mode();
153 throw $e;
154 }
155 }
156
157 return $response;
158 }
159
160 /**
161 * Invoke PayPlug API.
162 *
163 * @param callable $callback
164 * @param array $params
165 *
166 * @return object
167 */
168 protected function do_request( $callback, $params = [] ) {
169
170 if ( ! is_array( $params ) ) {
171 $params = [ $params ];
172 }
173
174 return call_user_func_array( $callback, $params );
175 }
176
177 /**
178 * Reconfigure PayPlug client with new secret keys.
179 *
180 * @throws \Payplug\Exception\ConfigurationException
181 */
182 protected function switch_mode() {
183 $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test';
184 $new_key = $this->gateway->get_api_key( $switched_mode );
185 if ( empty( $new_key ) ) {
186 throw new \Exception( sprintf(
187 'No secret key available for the %s mode', $switched_mode
188 ) );
189 }
190
191 Payplug::setSecretKey( $new_key );
192 }
193
194 /**
195 * Restore PayPlug client secret keys for the current mode.
196 *
197 * @throws \Payplug\Exception\ConfigurationException
198 */
199 protected function restore_mode() {
200 $key = $this->gateway->get_api_key( $this->gateway->get_current_mode() );
201 if ( empty( $key ) ) {
202 throw new \Exception( sprintf(
203 'No secret key available for the %s mode', $this->gateway->get_current_mode()
204 ) );
205 }
206
207 Payplug::setSecretKey( $key );
208 }
209 }
210