Skip to content
This repository was archived by the owner on Oct 4, 2019. It is now read-only.

Commit 26aba44

Browse files
committed
Payment Gateways: Add a method_supports property to each gateway response
Fixes #17
1 parent ef55f96 commit 26aba44

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed

api/class-wc-rest-dev-payment-gateways-controller.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,163 @@ class WC_REST_Dev_Payment_Gateways_Controller extends WC_REST_Payment_Gateways_C
2626
*/
2727
protected $namespace = 'wc/v3';
2828

29+
/**
30+
* Prepare a payment gateway for response.
31+
*
32+
* @param WC_Payment_Gateway $gateway Payment gateway object.
33+
* @param WP_REST_Request $request Request object.
34+
* @return WP_REST_Response $response Response data.
35+
*/
36+
public function prepare_item_for_response( $gateway, $request ) {
37+
$order = (array) get_option( 'woocommerce_gateway_order' );
38+
$item = array(
39+
'id' => $gateway->id,
40+
'title' => $gateway->title,
41+
'description' => $gateway->description,
42+
'order' => isset( $order[ $gateway->id ] ) ? $order[ $gateway->id ] : '',
43+
'enabled' => ( 'yes' === $gateway->enabled ),
44+
'method_title' => $gateway->get_method_title(),
45+
'method_description' => $gateway->get_method_description(),
46+
'method_supports' => $gateway->supports,
47+
'settings' => $this->get_settings( $gateway ),
48+
);
49+
50+
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
51+
$data = $this->add_additional_fields_to_object( $item, $request );
52+
$data = $this->filter_response_by_context( $data, $context );
53+
54+
$response = rest_ensure_response( $data );
55+
$response->add_links( $this->prepare_links( $gateway, $request ) );
56+
57+
/**
58+
* Filter payment gateway objects returned from the REST API.
59+
*
60+
* @param WP_REST_Response $response The response object.
61+
* @param WC_Payment_Gateway $gateway Payment gateway object.
62+
* @param WP_REST_Request $request Request object.
63+
*/
64+
return apply_filters( 'woocommerce_rest_prepare_payment_gateway', $response, $gateway, $request );
65+
}
66+
67+
/**
68+
* Get the payment gateway schema, conforming to JSON Schema.
69+
*
70+
* @return array
71+
*/
72+
public function get_item_schema() {
73+
$schema = array(
74+
'$schema' => 'http://json-schema.org/draft-04/schema#',
75+
'title' => 'payment_gateway',
76+
'type' => 'object',
77+
'properties' => array(
78+
'id' => array(
79+
'description' => __( 'Payment gateway ID.', 'woocommerce' ),
80+
'type' => 'string',
81+
'context' => array( 'view', 'edit' ),
82+
'readonly' => true,
83+
),
84+
'title' => array(
85+
'description' => __( 'Payment gateway title on checkout.', 'woocommerce' ),
86+
'type' => 'string',
87+
'context' => array( 'view', 'edit' ),
88+
),
89+
'description' => array(
90+
'description' => __( 'Payment gateway description on checkout.', 'woocommerce' ),
91+
'type' => 'string',
92+
'context' => array( 'view', 'edit' ),
93+
),
94+
'order' => array(
95+
'description' => __( 'Payment gateway sort order.', 'woocommerce' ),
96+
'type' => 'integer',
97+
'context' => array( 'view', 'edit' ),
98+
'arg_options' => array(
99+
'sanitize_callback' => 'absint',
100+
),
101+
),
102+
'enabled' => array(
103+
'description' => __( 'Payment gateway enabled status.', 'woocommerce' ),
104+
'type' => 'boolean',
105+
'context' => array( 'view', 'edit' ),
106+
),
107+
'method_title' => array(
108+
'description' => __( 'Payment gateway method title.', 'woocommerce' ),
109+
'type' => 'string',
110+
'context' => array( 'view', 'edit' ),
111+
'readonly' => true,
112+
),
113+
'method_description' => array(
114+
'description' => __( 'Payment gateway method description.', 'woocommerce' ),
115+
'type' => 'string',
116+
'context' => array( 'view', 'edit' ),
117+
'readonly' => true,
118+
),
119+
'method_supports' => array(
120+
'description' => __( 'Supported features for this payment gateway.', 'woocommerce' ),
121+
'type' => 'array',
122+
'context' => array( 'view', 'edit' ),
123+
'readonly' => true,
124+
'items' => array(
125+
'type' => 'string',
126+
),
127+
),
128+
'settings' => array(
129+
'description' => __( 'Payment gateway settings.', 'woocommerce' ),
130+
'type' => 'object',
131+
'context' => array( 'view', 'edit' ),
132+
'properties' => array(
133+
'id' => array(
134+
'description' => __( 'A unique identifier for the setting.', 'woocommerce' ),
135+
'type' => 'string',
136+
'context' => array( 'view', 'edit' ),
137+
'readonly' => true,
138+
),
139+
'label' => array(
140+
'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ),
141+
'type' => 'string',
142+
'context' => array( 'view', 'edit' ),
143+
'readonly' => true,
144+
),
145+
'description' => array(
146+
'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ),
147+
'type' => 'string',
148+
'context' => array( 'view', 'edit' ),
149+
'readonly' => true,
150+
),
151+
'type' => array(
152+
'description' => __( 'Type of setting.', 'woocommerce' ),
153+
'type' => 'string',
154+
'context' => array( 'view', 'edit' ),
155+
'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox' ),
156+
'readonly' => true,
157+
),
158+
'value' => array(
159+
'description' => __( 'Setting value.', 'woocommerce' ),
160+
'type' => 'string',
161+
'context' => array( 'view', 'edit' ),
162+
),
163+
'default' => array(
164+
'description' => __( 'Default value for the setting.', 'woocommerce' ),
165+
'type' => 'string',
166+
'context' => array( 'view', 'edit' ),
167+
'readonly' => true,
168+
),
169+
'tip' => array(
170+
'description' => __( 'Additional help text shown to the user about the setting.', 'woocommerce' ),
171+
'type' => 'string',
172+
'context' => array( 'view', 'edit' ),
173+
'readonly' => true,
174+
),
175+
'placeholder' => array(
176+
'description' => __( 'Placeholder text to be displayed in text inputs.', 'woocommerce' ),
177+
'type' => 'string',
178+
'context' => array( 'view', 'edit' ),
179+
'readonly' => true,
180+
),
181+
),
182+
),
183+
),
184+
);
185+
186+
return $this->add_additional_fields_schema( $schema );
187+
}
29188
}

tests/unit-tests/payment-gateways.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function test_get_payment_gateways() {
5050
'enabled' => true,
5151
'method_title' => 'Check payments',
5252
'method_description' => 'Allows check payments. Why would you take checks in this day and age? Well you probably would not, but it does allow you to make test purchases for testing order emails and the success pages.',
53+
'method_supports' => array( 'products' ),
5354
'settings' => array_diff_key( $this->get_settings( 'WC_Gateway_Cheque' ), array( 'enabled' => false, 'description' => false ) ),
5455
'_links' => array(
5556
'self' => array(
@@ -97,6 +98,7 @@ public function test_get_payment_gateway() {
9798
'enabled' => true,
9899
'method_title' => 'PayPal',
99100
'method_description' => 'PayPal Standard sends customers to PayPal to enter their payment information. PayPal IPN requires fsockopen/cURL support to update order statuses after payment. Check the <a href="http://example.org/wp-admin/admin.php?page=wc-status">system status</a> page for more details.',
101+
'method_supports' => array( 'products', 'refunds' ),
100102
'settings' => array_diff_key( $this->get_settings( 'WC_Gateway_Paypal' ), array( 'enabled' => false, 'description' => false ) ),
101103
), $paypal );
102104
}
@@ -252,14 +254,15 @@ public function test_payment_gateway_schema() {
252254
$data = $response->get_data();
253255
$properties = $data['schema']['properties'];
254256

255-
$this->assertEquals( 8, count( $properties ) );
257+
$this->assertEquals( 9, count( $properties ) );
256258
$this->assertArrayHasKey( 'id', $properties );
257259
$this->assertArrayHasKey( 'title', $properties );
258260
$this->assertArrayHasKey( 'description', $properties );
259261
$this->assertArrayHasKey( 'order', $properties );
260262
$this->assertArrayHasKey( 'enabled', $properties );
261263
$this->assertArrayHasKey( 'method_title', $properties );
262264
$this->assertArrayHasKey( 'method_description', $properties );
265+
$this->assertArrayHasKey( 'method_supports', $properties );
263266
$this->assertArrayHasKey( 'settings', $properties );
264267
}
265268

0 commit comments

Comments
 (0)