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

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