Skip to content

Commit dac2547

Browse files
authored
Merge pull request #118 from kidunot89/feature/payment-gateways
PaymentGateway type.
2 parents cd2fa59 + 7d5740c commit dac2547

File tree

12 files changed

+348
-6
lines changed

12 files changed

+348
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ composer.lock
3030
c3.php
3131
.log/
3232
php-coveralls.phar
33+
codeception.yml

bin/install-wp-tests.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,6 @@ configure_wordpress() {
188188
setup_woocommerce() {
189189
echo "Installing & Activating WooCommerce"
190190
wp plugin install woocommerce --activate
191-
echo "Installing & Activating WordPress Importer"
192-
wp plugin install wordpress-importer
193-
wp plugin activate wordpress-importer
194-
echo "Import sample products"
195-
wp import $WP_CORE_DIR/wp-content/plugins/woocommerce/sample-data/sample_products.xml --authors=skip --path=$WP_CORE_DIR
196191
}
197192

198193
setup_wpgraphql() {

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ services:
1414
WP_DOMAIN: 'localhost:8091'
1515
DB_HOST: app_db
1616
INCLUDE_WPGRAPHIQL: 1
17+
IMPORT_WC_PRODUCTS: 1
1718
ports:
1819
- '8091:80'
1920
networks:

includes/class-type-registry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static function graphql_register_types() {
6767
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Shipping_Method_Type::register();
6868
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Cart_Type::register();
6969
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Variation_Attribute_Type::register();
70+
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Payment_Gateway_Type::register();
7071

7172
// Object fields.
7273
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Category_Type::register_fields();
@@ -85,6 +86,7 @@ public static function graphql_register_types() {
8586
\WPGraphQL\Extensions\WooCommerce\Connection\Tax_Rates::register_connections();
8687
\WPGraphQL\Extensions\WooCommerce\Connection\Shipping_Methods::register_connections();
8788
\WPGraphQL\Extensions\WooCommerce\Connection\Cart_Items::register_connections();
89+
\WPGraphQL\Extensions\WooCommerce\Connection\Payment_Gateways::register_connections();
8890

8991
// Mutations.
9092
\WPGraphQL\Extensions\WooCommerce\Mutation\Customer_Register::register_mutation();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Connection - PaymentGateways
4+
*
5+
* Registers connections to PaymentGateway
6+
*
7+
* @package WPGraphQL\Extensions\WooCommerce\Connection
8+
*/
9+
10+
namespace WPGraphQL\Extensions\WooCommerce\Connection;
11+
12+
use WPGraphQL\Extensions\WooCommerce\Data\Factory;
13+
14+
/**
15+
* Class - PaymentGateways
16+
*/
17+
class Payment_Gateways {
18+
/**
19+
* Registers the various connections from other Types to Customer
20+
*/
21+
public static function register_connections() {
22+
// From RootQuery.
23+
register_graphql_connection( self::get_connection_config() );
24+
}
25+
26+
/**
27+
* Given an array of $args, this returns the connection config, merging the provided args
28+
* with the defaults
29+
*
30+
* @access public
31+
* @param array $args - Connection configuration.
32+
*
33+
* @return array
34+
*/
35+
public static function get_connection_config( $args = [] ) {
36+
$defaults = array(
37+
'fromType' => 'RootQuery',
38+
'toType' => 'PaymentGateway',
39+
'fromFieldName' => 'paymentGateways',
40+
'connectionArgs' => self::get_connection_args(),
41+
'resolve' => function ( $source, $args, $context, $info ) {
42+
return Factory::resolve_payment_gateway_connection( $source, $args, $context, $info );
43+
},
44+
);
45+
return array_merge( $defaults, $args );
46+
}
47+
48+
/**
49+
* Returns array of where args
50+
*
51+
* @return array
52+
*/
53+
public static function get_connection_args() {
54+
return array(
55+
'all' => array(
56+
'type' => 'Boolean',
57+
'description' => __( 'Include disabled payment gateways?', 'wp-graphql-woocommerce' ),
58+
),
59+
);
60+
}
61+
}

includes/data/class-factory.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use WPGraphQL\Extensions\WooCommerce\Data\Connection\Tax_Rate_Connection_Resolver;
2727
use WPGraphQL\Extensions\WooCommerce\Data\Connection\Shipping_Method_Connection_Resolver;
2828
use WPGraphQL\Extensions\WooCommerce\Data\Connection\Cart_Item_Connection_Resolver;
29+
use WPGraphQL\Extensions\WooCommerce\Data\Connection\Payment_Gateway_Connection_Resolver;
2930
use WPGraphQL\Extensions\WooCommerce\Model\Order_Item;
3031
use WPGraphQL\Extensions\WooCommerce\Model\Tax_Rate;
3132
use WPGraphQL\Extensions\WooCommerce\Model\Shipping_Method;
@@ -385,4 +386,20 @@ public static function resolve_cart_item_connection( $source, array $args, AppCo
385386
$resolver = new Cart_Item_Connection_Resolver();
386387
return $resolver->resolve( $source, $args, $context, $info );
387388
}
389+
390+
/**
391+
* Resolves PaymentGateway connections
392+
*
393+
* @param mixed $source - Data resolver for connection source.
394+
* @param array $args - Connection arguments.
395+
* @param AppContext $context - AppContext object.
396+
* @param ResolveInfo $info - ResolveInfo object.
397+
*
398+
* @return array
399+
* @access public
400+
*/
401+
public static function resolve_payment_gateway_connection( $source, array $args, AppContext $context, ResolveInfo $info ) {
402+
$resolver = new Payment_Gateway_Connection_Resolver();
403+
return $resolver->resolve( $source, $args, $context, $info );
404+
}
388405
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* ConnectionResolver - Payment_Gateway_Connection_Resolver
4+
*
5+
* Resolves connections to PaymentGateway
6+
*
7+
* @package WPGraphQL\Extensions\WooCommerce\Data\Connection
8+
* @since 0.2.1
9+
*/
10+
11+
namespace WPGraphQL\Extensions\WooCommerce\Data\Connection;
12+
13+
use GraphQL\Error\UserError;
14+
use GraphQL\Type\Definition\ResolveInfo;
15+
use GraphQLRelay\Relay;
16+
use WPGraphQL\AppContext;
17+
18+
/**
19+
* Class Payment_Gateway_Connection_Resolver
20+
*/
21+
class Payment_Gateway_Connection_Resolver {
22+
/**
23+
* Creates connection
24+
*
25+
* @param mixed $source - Connection source Model instance.
26+
* @param array $args - Connection arguments.
27+
* @param AppContext $context - AppContext object.
28+
* @param ResolveInfo $info - ResolveInfo object.
29+
*
30+
* @throws UserError User not authorized.
31+
* @return array
32+
*/
33+
public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
34+
if ( ( ! empty( $args['where']['all'] ) ) && true === $args['where']['all'] ) {
35+
if ( ! current_user_can( 'edit_theme_options' ) ) {
36+
throw new UserError( __( 'Not authorized to view these settings', 'wp-graphql-woocommerce' ) );
37+
}
38+
$gateways = \WC()->payment_gateways()->payment_gateways();
39+
} else {
40+
$gateways = \WC()->payment_gateways()->get_available_payment_gateways();
41+
}
42+
43+
$connection = Relay::connectionFromArray( array_values( $gateways ), $args );
44+
$nodes = array();
45+
if ( ! empty( $connection['edges'] ) && is_array( $connection['edges'] ) ) {
46+
foreach ( $connection['edges'] as $edge ) {
47+
$nodes[] = ! empty( $edge['node'] ) ? $edge['node'] : null;
48+
}
49+
}
50+
$connection['nodes'] = ! empty( $nodes ) ? $nodes : null;
51+
return ! empty( $gateways ) ? $connection : null;
52+
}
53+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* WPObject Type - Payment_Gateway_Type
4+
*
5+
* Registers PaymentGateway WPObject type.
6+
*
7+
* @package \WPGraphQL\Extensions\WooCommerce\Type\WPObject
8+
* @since 0.2.1
9+
*/
10+
11+
namespace WPGraphQL\Extensions\WooCommerce\Type\WPObject;
12+
13+
/**
14+
* Class Payment_Gateway_Type
15+
*/
16+
class Payment_Gateway_Type {
17+
/**
18+
* Registers type
19+
*/
20+
public static function register() {
21+
register_graphql_object_type(
22+
'PaymentGateway',
23+
array(
24+
'description' => __( 'A payment gateway object', 'wp-graphql-woocommerce' ),
25+
'fields' => array(
26+
'id' => array(
27+
'type' => array( 'non_null' => 'String' ),
28+
'description' => __( 'gateway\'s title', 'wp-graphql-woocommerce' ),
29+
'resolve' => function( $source ) {
30+
return ! empty( $source->id ) ? $source->id : null;
31+
},
32+
),
33+
'title' => array(
34+
'type' => 'String',
35+
'description' => __( 'gateway\'s title', 'wp-graphql-woocommerce' ),
36+
'resolve' => function( $source ) {
37+
return ! empty( $source->title ) ? $source->title : null;
38+
},
39+
),
40+
'description' => array(
41+
'type' => 'String',
42+
'description' => __( 'gateway\'s description', 'wp-graphql-woocommerce' ),
43+
'resolve' => function( $source ) {
44+
return ! empty( $source->description ) ? $source->description : null;
45+
},
46+
),
47+
'icon' => array(
48+
'type' => 'String',
49+
'description' => __( 'gateway\'s icon', 'wp-graphql-woocommerce' ),
50+
'resolve' => function( $source ) {
51+
return ! empty( $source->icon ) ? $source->icon : null;
52+
},
53+
),
54+
),
55+
)
56+
);
57+
}
58+
}

tests/wpunit/CheckoutMutationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function setUp() {
3030
update_option(
3131
'woocommerce_bacs_settings',
3232
array(
33-
'enabled' => 'yes',
33+
'enabled' => 'yes',
3434
'title' => 'Direct bank transfer',
3535
'description' => 'Make your payment directly into our bank account. Please use your Order ID as the payment reference. Your order will not be shipped until the funds have cleared in our account.',
3636
'instructions' => 'Instructions that will be added to the thank you page and emails.',

0 commit comments

Comments
 (0)