Skip to content

Commit 5a155b4

Browse files
committed
feat: add api for checkout
1 parent 60fe816 commit 5a155b4

File tree

103 files changed

+4040
-2790
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+4040
-2790
lines changed

mapping.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
"addBlogPostReview": "blog/review/add",
3030

3131
"checkoutLink": "store/checkout/link",
32+
"paymentAddress": "store/checkout/paymentAddress",
33+
"shippingAddress": "store/checkout/shippingAddress",
34+
"paymentMethods": "store/checkout/paymentMethods",
35+
"shippingMethods": "store/checkout/shippingMethods",
36+
"createOrder": "store/checkout/createOrder",
3237

3338
"cart": "store/cart/get",
3439
"addToCart": "store/cart/add",
@@ -54,11 +59,11 @@
5459
"productsList": "store/product/getList",
5560
"product": "store/product/get",
5661
"addReview": "store/review/add",
57-
62+
5863
"wishlist": "store/wishlist/getList",
5964
"addToWishlist": "store/wishlist/add",
6065
"removeWishlist": "store/wishlist/remove",
6166

6267
"contactSend": "common/contact/send",
6368
"contact": "common/contact/get"
64-
}
69+
}

model/store/checkout.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
class VFA_ModelStoreCheckout extends VFA_Model {
4+
public function getJwt($codename) {
5+
$setting = get_option('vuefront-apps');
6+
7+
$result = false;
8+
9+
foreach ($setting as $key => $value) {
10+
if($value['codename'] == $codename) {
11+
$result = $value['jwt'];
12+
}
13+
}
14+
15+
return $result;
16+
}
17+
public function requestCheckout($query, $variables) {
18+
$jwt = $this->getJwt('vuefront-checkout-app');
19+
20+
$ch = curl_init();
21+
22+
$requestData = array(
23+
'operationName' => null,
24+
'variables' => $variables,
25+
'query' => $query
26+
);
27+
28+
$headr = array();
29+
30+
$headr[] = 'Content-type: application/json';
31+
$headr[] = 'Authorization: '.$jwt;
32+
33+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
34+
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
35+
curl_setopt($ch, CURLOPT_POST,true);
36+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestData, JSON_FORCE_OBJECT) );
37+
// curl_setopt($ch, CURLOPT_URL, 'http://localhost:3005/graphql');
38+
curl_setopt($ch, CURLOPT_URL, 'https://api.checkout.vuefront.com/graphql');
39+
40+
$result = curl_exec($ch);
41+
42+
$result = json_decode($result, true);
43+
44+
return $result['data'];
45+
}
46+
}

plugin.php

+61
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ function VFA_simulate_as_not_rest( $is_rest_api_request ) {
2525

2626
add_action( 'admin_menu', 'VFA_add_plugin_page' );
2727
add_action( 'admin_enqueue_scripts', 'VFA_vuefront_admin_styles' );
28+
add_action( 'wp_ajax_vf_apps', 'VFA_vuefront_admin_action_apps' );
29+
add_action( 'wp_ajax_vf_apps_create', 'VFA_vuefront_admin_action_apps_create' );
30+
add_action( 'wp_ajax_vf_apps_remove', 'VFA_vuefront_admin_action_apps_remove' );
2831
add_action( 'wp_ajax_vf_register', 'VFA_vuefront_admin_action_register' );
2932
add_action( 'wp_ajax_vf_turn_on', 'VFA_vuefront_admin_action_turn_on' );
3033
add_action( 'wp_ajax_vf_update', 'VFA_vuefront_admin_action_update' );
@@ -89,6 +92,49 @@ function VFA_vuefront_admin_action_vf_information() {
8992
wp_die();
9093
}
9194

95+
function VFA_vuefront_admin_action_apps_create() {
96+
$setting = get_option('vuefront-apps');
97+
98+
$d = new DateTime();
99+
100+
$setting[] = array(
101+
'codename' => $_POST['codename'],
102+
'jwt' => $_POST['jwt'],
103+
'dateAdded' => $d->format('Y-m-d\TH:i:s.u')
104+
);
105+
106+
107+
update_option('vuefront-apps', $setting);
108+
109+
echo json_encode(
110+
array('success' => 'success')
111+
);
112+
113+
wp_die();
114+
}
115+
116+
function VFA_vuefront_admin_action_apps() {
117+
118+
echo json_encode(
119+
get_option('vuefront-apps')
120+
);
121+
122+
wp_die();
123+
}
124+
125+
function VFA_vuefront_admin_action_apps_remove() {
126+
$setting = get_option('vuefront-apps');
127+
unset($setting[$_POST['key']]);
128+
update_option('vuefront-apps', $setting);
129+
130+
echo json_encode(
131+
array('success' => 'success')
132+
);
133+
134+
wp_die();
135+
}
136+
137+
92138
function VFA_vuefront_admin_action_turn_off() {
93139
if ( strpos( $_SERVER["SERVER_SOFTWARE"], "Apache" ) !== false ) {
94140
if ( file_exists( __DIR__ . '/.htaccess.txt' ) ) {
@@ -308,6 +354,16 @@ function VFA_RestApi( WP_REST_Request $request ) {
308354
return $output;
309355
}
310356

357+
function VFA_Callback(WP_REST_Request $request) {
358+
$registry = VFA_Start();
359+
360+
$registry->set( 'request', $request );
361+
362+
$output = $registry->get( 'load' )->resolver( 'store/checkout/callback' );
363+
364+
return $output;
365+
}
366+
311367
add_action( 'determine_current_user', function ( $user ) {
312368
$registry = VFA_Start();
313369

@@ -319,6 +375,11 @@ function VFA_RestApi( WP_REST_Request $request ) {
319375
'methods' => 'POST',
320376
'callback' => 'VFA_RestApi',
321377
) );
378+
379+
register_rest_route( 'vuefront/v1', '/callback', array(
380+
'methods' => 'POST',
381+
'callback' => 'VFA_Callback',
382+
) );
322383
} );
323384

324385
add_action( 'wp', function () {

0 commit comments

Comments
 (0)