-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-graphql-razorpay.php
More file actions
118 lines (96 loc) · 4.3 KB
/
wp-graphql-razorpay.php
File metadata and controls
118 lines (96 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/*
* Plugin Name: Razorpay for WooCommerce WPGraphQL server
* Description: Razorpay Payment Gateway Integration for WooCommerce
* Author: Vino Crazy
* Author URI: https://vinocrazy.com/
* Domain Path: /languages
* Version: 1.0.0
* Requires PHP: 7.0
* GitHub Plugin URI: https://github.com/vinocrzy/wp-graphql-woocommerce-gold-price
*/
require_once(__DIR__ . '/vendor/autoload.php');
use Razorpay\Api\Api;
$api_key = defined('RAZORPAY_API_KEY') ? RAZORPAY_API_KEY : '';
$api_secret = defined('RAZORPAY_API_SECRET') ? RAZORPAY_API_SECRET : '';
$GLOBALS['api'] = new Api($api_key, $api_secret);
add_action('graphql_register_types', 'razorpay');
function razorpay()
{
register_graphql_mutation('paymentIntent', [
# inputFields expects an array of Fields to be used for inputting values to the mutation
'inputFields' => [
'amount' => [
'type' => 'Float',
'description' => __('Razerpay Amount', 'wp-graphql-razorpay'),
],
'currency' => [
'type' => 'String',
'description' => __('Razerpay Currency', 'wp-graphql-razorpay'),
],
'email' => [
'type' => 'String',
'description' => __('User email', 'wp-graphql-razorpay'),
],
'phone' => [
'type' => 'String',
'description' => __('User phone', 'wp-graphql-razorpay'),
],
],
# outputFields expects an array of fields that can be asked for in response to the mutation
# the resolve function is optional, but can be useful if the mutateAndPayload doesn't return an array
# with the same key(s) as the outputFields
'outputFields' => [
'id' => [
'type' => 'String',
'description' => __('Description of the output field', 'your-textdomain'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['id']) ? $payload['id'] : null;
}
],
'amount' => [
'type' => 'String',
'description' => __('Description of the output field', 'your-textdomain'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['amount']) ? $payload['amount'] : null;
}
],
'currency' => [
'type' => 'String',
'description' => __('Description of the output field', 'your-textdomain'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['currency']) ? $payload['currency'] : null;
}
],
'email' => [
'type' => 'String',
'description' => __('Description of the output field', 'your-textdomain'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['email']) ? $payload['email'] : null;
}
],
'phone' => [
'type' => 'String',
'description' => __('Description of the output field', 'your-textdomain'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['phone']) ? $payload['phone'] : null;
}
],
],
# mutateAndGetPayload expects a function, and the function gets passed the $input, $context, and $info
# the function should return enough info for the outputFields to resolve with
'mutateAndGetPayload' => function ($input, $context, $info) {
// Do any logic here to sanitize the input, check user capabilities, etc
// $Output = null;
// $Output = $input;
$order = $GLOBALS['api']->order->create(array('receipt' => '123', 'amount' => $input['amount'], 'currency' => $input['currency']));
return [
'id' => $order['id'],
'amount' => $order['amount'],
'currency' => $order['currency'],
'email' => $input['email'],
'phone' => $input['phone'],
];
}
]);
}