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

174 lines 4.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
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::setSecretKey( $key );
43 HttpClient::addDefaultUserAgentProduct(
44 'PayPlug-WooCommerce',
45 PAYPLUG_GATEWAY_VERSION,
46 sprintf( 'WooCommerce/%s', WC()->version )
47 );
48 }
49
50 /**
51 * Retrieve payment data from PayPlug API.
52 *
53 * @param string $transaction_id
54 *
55 * @return null|\Payplug\Resource\Payment
56 * @throws \Payplug\Exception\ConfigurationException
57 */
58 public function payment_retrieve( $transaction_id ) {
59 return $this->do_request_with_fallback( '\Payplug\Payment::retrieve', $transaction_id );
60 }
61
62 /**
63 * Create a payment.
64 *
65 * @param array $data
66 *
67 * @return null|\Payplug\Resource\Payment
68 */
69 public function payment_create( $data ) {
70 return $this->do_request( '\Payplug\Payment::create', [ $data ] );
71 }
72
73 /**
74 * Retrieve all refunds associated with a payment.
75 *
76 * @param string $transaction_id
77 *
78 * @return \Payplug\Resource\Refund[]
79 * @throws \Payplug\Exception\ConfigurationException
80 */
81 public function refund_list( $transaction_id ) {
82 return $this->do_request_with_fallback( '\Payplug\Refund::listRefunds', $transaction_id );
83 }
84
85 /**
86 * Create a refund.
87 *
88 * @param string $transaction_id
89 * @param array $data
90 *
91 * @return null|\Payplug\Resource\Refund
92 * @throws \Payplug\Exception\ConfigurationException
93 * @author Clément Boirie
94 */
95 public function refund_create( $transaction_id, $data ) {
96 return $this->do_request_with_fallback( '\Payplug\Refund::create', [ $transaction_id, $data ] );
97 }
98
99 /**
100 * Invoke PayPlug API. If it fail it switch to the other mode and retry the same request.
101 *
102 * @param callable $callback
103 * @param array $params
104 *
105 * @return object
106 * @throws \Payplug\Exception\ConfigurationException
107 */
108 protected function do_request_with_fallback( $callback, $params = [] ) {
109 try {
110 $response = $this->do_request( $callback, $params );
111 } catch ( NotFoundException $e ) {
112 try {
113 $this->switch_mode();
114 $response = $this->do_request( $callback, $params );
115 $this->restore_mode();
116 } catch ( \Exception $e ) {
117 $this->restore_mode();
118 throw $e;
119 }
120 }
121
122 return $response;
123 }
124
125 /**
126 * Invoke PayPlug API.
127 *
128 * @param callable $callback
129 * @param array $params
130 *
131 * @return object
132 */
133 protected function do_request( $callback, $params = [] ) {
134
135 if ( ! is_array( $params ) ) {
136 $params = [ $params ];
137 }
138
139 return call_user_func_array( $callback, $params );
140 }
141
142 /**
143 * Reconfigure PayPlug client with new secret keys.
144 *
145 * @throws \Payplug\Exception\ConfigurationException
146 */
147 protected function switch_mode() {
148 $switched_mode = 'test' === $this->gateway->get_current_mode() ? 'live' : 'test';
149 $new_key = $this->gateway->get_api_key( $switched_mode );
150 if ( empty( $new_key ) ) {
151 throw new \Exception( sprintf(
152 'No secret key available for the %s mode', $switched_mode
153 ) );
154 }
155
156 Payplug::setSecretKey( $new_key );
157 }
158
159 /**
160 * Restore PayPlug client secret keys for the current mode.
161 *
162 * @throws \Payplug\Exception\ConfigurationException
163 */
164 protected function restore_mode() {
165 $key = $this->gateway->get_api_key( $this->gateway->get_current_mode() );
166 if ( empty( $key ) ) {
167 throw new \Exception( sprintf(
168 'No secret key available for the %s mode', $this->gateway->get_current_mode()
169 ) );
170 }
171
172 Payplug::setSecretKey( $key );
173 }
174 }