Skip to content

Commit fcc85a4

Browse files
committed
feat: add new api for checkout app
1 parent 33449e2 commit fcc85a4

File tree

3 files changed

+377
-4
lines changed

3 files changed

+377
-4
lines changed

Model/Api/Model/Store/Checkout.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Vuefront\Vuefront\Model\Api\Model\Store;
4+
5+
use \Vuefront\Vuefront\Model\Api\System\Engine\Model;
6+
7+
class Checkout extends Model
8+
{
9+
private $_appsFactory;
10+
11+
private $_jsonSerializer;
12+
13+
private $_curl;
14+
15+
public function __construct(
16+
\Vuefront\Vuefront\Model\AppsFactory $appsFactory,
17+
\Magento\Framework\HTTP\Client\Curl $curl,
18+
\Magento\Framework\Serialize\Serializer\Json $jsonSerializer
19+
) {
20+
$this->_appsFactory = $appsFactory;
21+
$this->_curl = $curl;
22+
$this->_jsonSerializer = $jsonSerializer;
23+
}
24+
25+
public function getJwt($codename) {
26+
27+
$collection = $this->_appsFactory->create()->getCollection();
28+
29+
$jwt = '';
30+
31+
foreach ($collection as $key => $value) {
32+
$data = $value->getData();
33+
34+
if($data['codename'] == $codename) {
35+
$jwt = $data['jwt'];
36+
}
37+
}
38+
39+
return $jwt;
40+
}
41+
42+
public function requestCheckout($query, $variables) {
43+
$jwt = $this->getJwt('vuefront-checkout-app');
44+
45+
$requestData = array(
46+
'operationName' => null,
47+
'variables' => $variables,
48+
'query' => $query
49+
);
50+
51+
$headr = array();
52+
53+
$headr[] = 'Content-type: application/json';
54+
$headr[] = 'Authorization: '.$jwt;
55+
56+
$this->_curl->addHeader('Content-type', 'application/json');
57+
$this->_curl->addHeader('Authorization', $jwt);
58+
$this->_curl->post('https://api.checkout.vuefront.com/graphql', $this->_jsonSerializer->serialize($requestData));
59+
60+
$result = $this->_curl->getBody();
61+
62+
$result = $this->_jsonSerializer->unserialize($result);
63+
64+
return $result['data'];
65+
}
66+
}

Model/Api/Resolver/Store/Checkout.php

Lines changed: 308 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,40 @@
22

33
namespace Vuefront\Vuefront\Model\Api\Resolver\Store;
44

5-
use \Vuefront\Vuefront\Model\Api\System\Engine\Resolver;
5+
use Magento\Checkout\Model\Session;
6+
use Magento\Framework\Pricing\Helper\Data;
7+
use Magento\Framework\UrlInterface;
8+
use Magento\Shipping\Model\Config;
9+
use Vuefront\Vuefront\Model\Api\System\Engine\Resolver;
610

711
class Checkout extends Resolver
812
{
13+
private $_currencyHelper;
914
/**
10-
* @var \Magento\Framework\UrlInterface
15+
* @var UrlInterface
1116
*/
1217
private $url;
1318

19+
/**
20+
* @var Config
21+
*/
22+
private $_shippingModelConfig;
23+
24+
/**
25+
* @var Session
26+
*/
27+
protected $checkoutSession;
28+
1429
public function __construct(
15-
\Magento\Framework\UrlInterface $url
30+
Config $shippingModelConfig,
31+
UrlInterface $url,
32+
Session $session,
33+
Data $currencyHelper
1634
) {
35+
$this->_currencyHelper = $currencyHelper;
1736
$this->url = $url;
37+
$this->_shippingModelConfig = $shippingModelConfig;
38+
$this->checkoutSession = $session;
1839
}
1940

2041
public function link()
@@ -23,4 +44,288 @@ public function link()
2344
'link' => $this->url->getUrl('checkout')
2445
];
2546
}
47+
48+
public function paymentMethods()
49+
{
50+
$this->load->model('store/checkout');
51+
52+
$response = $this->model_store_checkout->requestCheckout(
53+
'{
54+
payments {
55+
setting
56+
codename
57+
status
58+
name
59+
}
60+
}',
61+
[]
62+
);
63+
64+
$methods = [];
65+
66+
foreach ($response['payments'] as $key => $value) {
67+
if ($value['status']) {
68+
$methods[] = [
69+
'id' => $value['codename'],
70+
'codename' => $value['codename'],
71+
"name" => $value['name']
72+
];
73+
}
74+
}
75+
76+
return $methods;
77+
}
78+
79+
public function shippingMethods($args)
80+
{
81+
$result = [];
82+
83+
$carriers = $this->_shippingModelConfig->getActiveCarriers();
84+
85+
foreach ($carriers as $shippingCode => $shippingModel) {
86+
$shippingPrice = $this->_currencyHelper->currency($shippingModel->getConfigData('price'), true, false);
87+
$result[] = [
88+
'id' => $shippingModel->getCarrierCode(),
89+
'codename' => $shippingModel->getCarrierCode(),
90+
"name" => $shippingModel->getConfigData('title') . ' - ' . $shippingPrice
91+
];
92+
}
93+
94+
return $result;
95+
}
96+
97+
public function paymentAddress()
98+
{
99+
$fields = [];
100+
101+
$fields[] = [
102+
'type' => 'text',
103+
'name' => 'firstName',
104+
'required' => true
105+
];
106+
$fields[] = [
107+
'type' => 'text',
108+
'name' => 'lastName',
109+
'required' => true
110+
];
111+
112+
$fields[] = [
113+
'type' => 'text',
114+
'name' => 'email',
115+
'required' => true
116+
];
117+
118+
$fields[] = [
119+
'type' => 'text',
120+
'name' => 'company',
121+
'required' => false
122+
];
123+
$fields[] = [
124+
'type' => 'text',
125+
'name' => 'address1',
126+
'required' => true
127+
];
128+
$fields[] = [
129+
'type' => 'text',
130+
'name' => 'address2',
131+
'required' => false
132+
];
133+
$fields[] = [
134+
'type' => 'text',
135+
'name' => 'city',
136+
'required' => true
137+
];
138+
$fields[] = [
139+
'type' => 'text',
140+
'name' => 'postcode',
141+
'required' => true
142+
];
143+
144+
$fields[] = [
145+
'type' => 'country',
146+
'name' => 'country_id',
147+
'required' => true
148+
];
149+
150+
$fields[] = [
151+
'type' => 'zone',
152+
'name' => 'zone_id',
153+
'required' => true
154+
];
155+
156+
$agree = null;
157+
158+
return [
159+
'fields' => $fields,
160+
'agree' => $agree
161+
];
162+
}
163+
164+
public function shippingAddress()
165+
{
166+
$fields = [];
167+
168+
$fields[] = [
169+
'type' => 'text',
170+
'name' => 'firstName',
171+
'required' => true
172+
];
173+
$fields[] = [
174+
'type' => 'text',
175+
'name' => 'lastName',
176+
'required' => true
177+
];
178+
179+
$fields[] = [
180+
'type' => 'text',
181+
'name' => 'company',
182+
'required' => false
183+
];
184+
$fields[] = [
185+
'type' => 'text',
186+
'name' => 'address1',
187+
'required' => true
188+
];
189+
$fields[] = [
190+
'type' => 'text',
191+
'name' => 'address2',
192+
'required' => false
193+
];
194+
$fields[] = [
195+
'type' => 'text',
196+
'name' => 'address3',
197+
'required' => false
198+
];
199+
$fields[] = [
200+
'type' => 'text',
201+
'name' => 'city',
202+
'required' => true
203+
];
204+
$fields[] = [
205+
'type' => 'zone',
206+
'name' => 'zone_id',
207+
'required' => true
208+
];
209+
$fields[] = [
210+
'type' => 'text',
211+
'name' => 'postcode',
212+
'required' => true
213+
];
214+
215+
$fields[] = [
216+
'type' => 'country',
217+
'name' => 'country_id',
218+
'required' => true
219+
];
220+
221+
return $fields;
222+
}
223+
224+
public function createOrder($args)
225+
{
226+
// $this->session->data['shipping_address'] = array();
227+
228+
// foreach ($this->shippingAddress() as $value) {
229+
// $this->session->data['shipping_address'][$value['name']] = '';
230+
// }
231+
232+
// $this->session->data['payment_address'] = array(
233+
// 'custom_field' => array()
234+
// );
235+
236+
// $paymentAddress = $this->paymentAddress();
237+
// foreach ($paymentAddress['fields'] as $value) {
238+
// $this->session->data['payment_address'][$value['name']] = '';
239+
// }
240+
241+
// $this->session->data['payment_method'] = null;
242+
// $this->session->data['shipping_method'] = null;
243+
return ['success' => 'success'];
244+
}
245+
246+
public function updateOrder($args)
247+
{
248+
// foreach ($args['paymentAddress'] as $value) {
249+
// if (strpos($value['name'], "vfCustomField-") !== false) {
250+
// if ($value['value']) {
251+
// $field_name = str_replace("vfCustomField-", "", $value['name']);
252+
// $field_name = explode('-', $field_name);
253+
// if (!isset($this->session->data['payment_address']['custom_field'][$field_name[0]])) {
254+
// $this->session->data['payment_address']['custom_field'][$field_name[0]] = array();
255+
// }
256+
// $this->session->data['payment_address']['custom_field'][$field_name[0]][$field_name[1]] = $value['value'];
257+
// }
258+
// } else {
259+
// if ($value['value']) {
260+
// $this->session->data['payment_address'][$value['name']] = $value['value'];
261+
// }
262+
// }
263+
// }
264+
265+
// foreach ($args['shippingAddress'] as $value) {
266+
// if (strpos($value['name'], "vfCustomField-") !== false) {
267+
// if ($value['value']) {
268+
// $field_name = str_replace("vfCustomField-", "", $value['name']);
269+
// $field_name = explode('-', $field_name);
270+
// if (!isset($this->session->data['shipping_address']['custom_field'][$field_name[0]])) {
271+
// $this->session->data['shipping_address']['custom_field'][$field_name[0]] = array();
272+
// }
273+
// $this->session->data['shipping_address']['custom_field'][$field_name[0]][$field_name[1]] = $value['value'];
274+
// }
275+
// } else {
276+
// if ($value['value']) {
277+
// $this->session->data['shipping_address'][$value['name']] = $value['value'];
278+
// }
279+
// }
280+
// }
281+
282+
// if (!empty($args['shippingMethod'])) {
283+
// $shipping = explode('.', $args['shippingMethod']);
284+
285+
// $this->load->model('extension/shipping/'.$shipping[0]);
286+
287+
// $quote = $this->{'model_extension_shipping_' . $shipping[0]}->getQuote($this->session->data['shipping_address']);
288+
// if ($quote) {
289+
// $this->session->data['shipping_method'] = $quote['quote'][$shipping[1]];
290+
// }
291+
// }
292+
293+
// $this->session->data['payment_method'] = $args['paymentMethod'];
294+
$that = $this;
295+
return [
296+
'paymentMethods' => function ($root, $args) use ($that) {
297+
return $that->load->resolver('store/checkout/paymentMethods', [
298+
'parent' => $root,
299+
'args' => $args
300+
]);
301+
},
302+
'shippingMethods' => function ($root, $args) use ($that) {
303+
return $that->load->resolver('store/checkout/shippingMethods', [
304+
'parent' => $root,
305+
'args' => $args
306+
]);
307+
},
308+
'totals' => function ($root, $args) use ($that) {
309+
return $that->load->resolver('store/checkout/totals', [
310+
'parent' => $root,
311+
'args' => $args
312+
]);
313+
}
314+
];
315+
}
316+
317+
public function confirmOrder()
318+
{
319+
return [
320+
'url' => '1',
321+
'order' => [
322+
'id' => '1'
323+
]
324+
];
325+
}
326+
327+
public function totals()
328+
{
329+
return [];
330+
}
26331
}

0 commit comments

Comments
 (0)