Skip to content

Commit 8840dda

Browse files
Merge pull request #489 from Anmol-Chauhan/v2.3.0
Payment method order place
2 parents dbad34a + 45eb72c commit 8840dda

File tree

28 files changed

+250
-161
lines changed

28 files changed

+250
-161
lines changed

Postman_APIs/Collections/GraphQL-API.postman_collection.json

Lines changed: 7 additions & 28 deletions
Large diffs are not rendered by default.

src/Helper/PaymentHelper.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Webkul\GraphQLAPI\Helper;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use Webkul\Paypal\Payment\SmartButton;
7+
use Webkul\Sales\Repositories\OrderRepository;
8+
use Webkul\Sales\Repositories\InvoiceRepository;
9+
10+
class PaymentHelper
11+
{
12+
/**
13+
* Create a new helper instance.
14+
*
15+
* @return void
16+
*/
17+
public function __construct(
18+
protected OrderRepository $orderRepository,
19+
protected InvoiceRepository $invoiceRepository,
20+
protected SmartButton $smartButton,
21+
) {}
22+
23+
public function createInvoice($cart, $paymentDetail, $order)
24+
{
25+
if (
26+
! empty($paymentDetail['error'])
27+
|| $paymentDetail['message'] != "Success"
28+
|| $cart->payment->method != $paymentDetail['payment_method']
29+
) {
30+
return;
31+
}
32+
33+
$paymentMethod = $cart->payment->method;
34+
$paymentIsCompleted = false;
35+
36+
match ($paymentMethod) {
37+
'paypal_standard' => $paymentIsCompleted = $this->isNewTransaction($paymentDetail['txn_id']) && $this->checkPaypalPaymentStatus($paymentDetail['txn_id']),
38+
'paypal_smart_button' => $paymentIsCompleted = $this->isNewTransaction($paymentDetail['orderID']) && $this->checkSmartButtonPaymentStatus($paymentDetail['orderID']),
39+
default => null,
40+
};
41+
42+
if (
43+
$paymentIsCompleted
44+
&& $order->canInvoice()
45+
) {
46+
request()->merge($paymentDetail);
47+
48+
$this->invoiceRepository->create($this->prepareInvoiceData($order));
49+
50+
$this->orderRepository->update(['status' => 'processing'], $order->id);
51+
}
52+
}
53+
54+
/**
55+
* Prepares order's invoice data for creation.
56+
*
57+
* @return array
58+
*/
59+
protected function prepareInvoiceData($order)
60+
{
61+
$invoiceData = ['order_id' => $order->id];
62+
63+
foreach ($order->items as $item) {
64+
$invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;
65+
}
66+
67+
return $invoiceData;
68+
}
69+
70+
protected function isNewTransaction(string $transactionId): bool
71+
{
72+
return ! DB::table('order_transactions')->where('transaction_id', $transactionId)->exists();
73+
}
74+
75+
protected function checkPaypalPaymentStatus($paymentId)
76+
{
77+
$clientId = core()->getConfigData('sales.payment_methods.paypal_standard.client_id');
78+
$secretKey = core()->getConfigData('sales.payment_methods.paypal_standard.client_secret');
79+
$sandbox = core()->getConfigData('sales.payment_methods.paypal_standard.sandbox');
80+
81+
$url = $sandbox
82+
? "https://api-m.sandbox.paypal.com/v1/payments/payment/{$paymentId}"
83+
: "https://api-m.paypal.com/v1/payments/payment/{$paymentId}";
84+
85+
$curl = curl_init($url);
86+
curl_setopt_array($curl, [
87+
CURLOPT_RETURNTRANSFER => true,
88+
CURLOPT_FAILONERROR => true,
89+
CURLOPT_HTTPHEADER => [
90+
'Content-Type: application/json',
91+
'Authorization: Basic ' . base64_encode("{$clientId}:{$secretKey}"),
92+
],
93+
]);
94+
95+
$response = curl_exec($curl);
96+
curl_close($curl);
97+
98+
if ($response) {
99+
$responseData = json_decode($response, true);
100+
return $responseData['state'] ?? '' === 'approved';
101+
}
102+
103+
return false;
104+
}
105+
106+
public function checkSmartButtonPaymentStatus(string $orderId): bool
107+
{
108+
$transactionDetails = json_decode(json_encode($this->smartButton->getOrder($orderId)), true);
109+
110+
return $transactionDetails['statusCode'] ?? 0 === 200;
111+
}
112+
}

src/Mutations/Admin/Catalog/Products/ProductMutation.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ public function validateFormData(int $id, array $data)
287287
'special_price_to' => 'nullable|date|after_or_equal:special_price_from',
288288
'special_price' => ['nullable', new Decimal, 'lt:price'],
289289
]);
290-
291-
foreach ($product->getEditableAttributes() as $attribute) {
290+
291+
foreach ($product->getEditableAttributes() as $attribute) {
292292
if (
293293
$attribute->code == 'sku'
294294
|| $attribute->type == 'boolean'
@@ -321,10 +321,10 @@ public function validateFormData(int $id, array $data)
321321
}
322322

323323
if ($attribute->is_unique) {
324-
array_push($validations, function ($field, $value, $fail) use ($attribute, $id) {
324+
array_push($validations, function ($field, $value, $fail) use ($attribute, $id, $data) {
325325
$column = ProductAttributeValue::$attributeTypeFields[$attribute->type];
326-
327-
if (! $this->productAttributeValueRepository->isValueUnique($id, $attribute->id, $column, request($attribute->code))) {
326+
327+
if (! $this->productAttributeValueRepository->isValueUnique($id, $attribute->id, $column, $data[$attribute->code])) {
328328
$fail('The :attribute has already been taken.');
329329
}
330330
});

src/Mutations/Admin/Customer/CustomerAddressMutation.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function store(mixed $rootValue, array $args, GraphQLContext $context)
6767

6868
return [
6969
'success' => true,
70-
'message' => trans('bagisto_graphql::app.admin.customers.addressess.create-success'),
70+
'message' => trans('bagisto_graphql::app.admin.customers.addresses.create-success'),
7171
'address' => $customerAddress,
7272
];
7373
} catch (\Exception $e) {
@@ -112,7 +112,7 @@ public function update(mixed $rootValue, array $args, GraphQLContext $context)
112112
$customerAddress = $this->customerAddressRepository->find($args['id']);
113113

114114
if (! $customerAddress) {
115-
throw new CustomException(trans('bagisto_graphql::app.admin.customers.addressess.not-found'));
115+
throw new CustomException(trans('bagisto_graphql::app.admin.customers.addresses.not-found'));
116116
}
117117

118118
try {
@@ -124,7 +124,7 @@ public function update(mixed $rootValue, array $args, GraphQLContext $context)
124124

125125
return [
126126
'success' => true,
127-
'message' => trans('bagisto_graphql::app.admin.customers.addressess.update-success'),
127+
'message' => trans('bagisto_graphql::app.admin.customers.addresses.update-success'),
128128
'address' => $customerAddress,
129129
];
130130
} catch (\Exception $e) {
@@ -145,7 +145,11 @@ public function setAsDefaultAddress(mixed $rootValue, array $args, GraphQLContex
145145
]);
146146

147147
if (! $address) {
148-
throw new CustomException(trans('bagisto_graphql::app.admin.customers.addressess.not-found'));
148+
throw new CustomException(trans('bagisto_graphql::app.admin.customers.addresses.not-found'));
149+
}
150+
151+
if ($address->default_address) {
152+
throw new CustomException(trans('bagisto_graphql::app.admin.customers.addresses.already-default'));
149153
}
150154

151155
try {
@@ -154,16 +158,11 @@ public function setAsDefaultAddress(mixed $rootValue, array $args, GraphQLContex
154158
'default_address' => 1,
155159
])->update(['default_address' => 0]);
156160

157-
$address = $this->customerAddressRepository->findOnewhere([
158-
'id' => $args['id'],
159-
'customer_id' => $args['customer_id'],
160-
]);
161-
162161
$address->update(['default_address' => 1]);
163162

164163
return [
165164
'success' => true,
166-
'message' => trans('bagisto_graphql::app.admin.customers.addressess.default-update-success'),
165+
'message' => trans('bagisto_graphql::app.admin.customers.addresses.default-update-success'),
167166
'address' => $address,
168167
];
169168
} catch (\Exception $e) {
@@ -183,7 +182,7 @@ public function delete(mixed $rootValue, array $args, GraphQLContext $context)
183182
$customerAddress = $this->customerAddressRepository->find($args['id']);
184183

185184
if (! $customerAddress) {
186-
throw new CustomException(trans('bagisto_graphql::app.admin.customers.addressess.not-found'));
185+
throw new CustomException(trans('bagisto_graphql::app.admin.customers.addresses.not-found'));
187186
}
188187

189188
try {
@@ -195,7 +194,7 @@ public function delete(mixed $rootValue, array $args, GraphQLContext $context)
195194

196195
return [
197196
'success' => true,
198-
'message' => trans('bagisto_graphql::app.admin.customers.addressess.delete-success'),
197+
'message' => trans('bagisto_graphql::app.admin.customers.addresses.delete-success'),
199198
];
200199

201200
} catch (\Exception $e) {

src/Mutations/Shop/Customer/AddressesMutation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function setDefaultAddress(mixed $rootValue, array $args, GraphQLContext
175175

176176
return [
177177
'success' => true,
178-
'message' => trans('bagisto_graphql::app.admin.customers.addressess.default-update-success'),
178+
'message' => trans('bagisto_graphql::app.admin.customers.addresses.default-update-success'),
179179
'address' => $address,
180180
];
181181
} catch (\Exception $e) {

src/Mutations/Shop/Customer/CheckoutMutation.php

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Webkul\Core\Rules\PostCode;
66
use Webkul\Checkout\Facades\Cart;
7+
use Illuminate\Support\Facades\DB;
78
use Webkul\Core\Rules\PhoneNumber;
89
use Webkul\Payment\Facades\Payment;
910
use App\Http\Controllers\Controller;
1011
use Illuminate\Support\Facades\Auth;
1112
use Webkul\Shipping\Facades\Shipping;
13+
use Webkul\GraphQLAPI\Helper\PaymentHelper;
1214
use Webkul\Sales\Transformers\OrderResource;
1315
use Webkul\Sales\Repositories\OrderRepository;
1416
use Webkul\Sales\Repositories\InvoiceRepository;
@@ -30,7 +32,8 @@ public function __construct(
3032
protected CustomerAddressRepository $customerAddressRepository,
3133
protected OrderRepository $orderRepository,
3234
protected NotificationRepository $notificationRepository,
33-
protected InvoiceRepository $invoiceRepository
35+
protected InvoiceRepository $invoiceRepository,
36+
protected PaymentHelper $paymentHelper
3437
) {
3538
Auth::setDefaultDriver('api');
3639
}
@@ -470,24 +473,16 @@ public function saveOrder(mixed $rootValue, array $args, GraphQLContext $context
470473

471474
$cart = Cart::getCart();
472475

473-
if (
474-
! $args['is_payment_required']
475-
&& $redirectUrl = Payment::getRedirectUrl($cart)
476-
) {
477-
return [
478-
'success' => true,
479-
'redirect_url' => $redirectUrl,
480-
'selected_method' => $cart->payment->method,
481-
];
482-
}
483-
484-
$data = (new OrderResource($cart))->jsonSerialize();
485-
486-
$order = $this->orderRepository->create($data);
476+
$orderData = (new OrderResource($cart))->jsonSerialize();
477+
$order = $this->orderRepository->create($orderData);
487478

488479
if (core()->getConfigData('general.api.pushnotification.private_key')) {
489480
$this->prepareNotificationContent($order);
490481
}
482+
483+
if (! empty($args['is_payment_completed'])) {
484+
$this->paymentHelper->createInvoice($cart, $args, $order);
485+
}
491486

492487
Cart::deActivateCart();
493488

@@ -558,54 +553,4 @@ public function prepareNotificationContent($order)
558553

559554
$this->notificationRepository->sendNotification($data, $notification);
560555
}
561-
562-
/**
563-
* Create charge
564-
*
565-
* @return array
566-
*
567-
* @throws CustomException
568-
*/
569-
public function createCharge()
570-
{
571-
if (! $cart = Cart::getCart()) {
572-
throw new CustomException(trans('bagisto_graphql::app.shop.checkout.something-wrong'));
573-
}
574-
575-
$order = $this->orderRepository->create((new OrderResource($cart))->jsonSerialize());
576-
577-
$order = $this->orderRepository->findOneByField('cart_id', $cart->id);
578-
579-
$this->orderRepository->update(['status' => 'processing'], $order->id);
580-
581-
if ($order->canInvoice()) {
582-
$this->invoiceRepository->create($this->prepareInvoiceData($order));
583-
}
584-
585-
Cart::deActivateCart();
586-
587-
return [
588-
'success' => true,
589-
'order' => $order,
590-
];
591-
}
592-
593-
/**
594-
* Prepares order's invoice data for creation
595-
*
596-
* @param \Webkul\Sales\Contracts\Order $order
597-
* @return array
598-
*/
599-
public function prepareInvoiceData($order)
600-
{
601-
$invoiceData = [
602-
'order_id' => $order->id,
603-
];
604-
605-
foreach ($order->items as $item) {
606-
$invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice;
607-
}
608-
609-
return $invoiceData;
610-
}
611556
}

src/Resources/lang/ar/app.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
'delete-success' => 'تم حذف عنوان العميل بنجاح',
7070
'not-found' => 'تحذير: لم يتم العثور على العنوان.',
7171
'update-success' => 'تم تحديث العنوان بنجاح.',
72+
'already-default' => 'تحذير: هذا العنوان تم تعيينه كافتراضي بالفعل.',
7273
],
7374

7475
'wishlist' => [
@@ -362,12 +363,14 @@
362363
'login-success' => 'تم تسجيل الدخول بنجاح.',
363364
],
364365

365-
'addressess' => [
366+
'addresses' => [
366367
'create-success' => 'تم إنشاء عنوان العميل بنجاح.',
367-
'default-update-success' => 'تم تعيين العنوان كافتراضي',
368-
'delete-success' => 'تم حذف عنوان العميل بنجاح',
368+
'default-update-success' => 'تم تعيين العنوان كافتراضي.',
369+
'delete-success' => 'تم حذف عنوان العميل بنجاح.',
369370
'not-found' => 'تحذير: عنوان العميل غير موجود.',
370371
'update-success' => 'تم تحديث عنوان العميل بنجاح.',
372+
'already-default' => 'تحذير: هذا العنوان تم تعيينه كافتراضي بالفعل.',
373+
],
371374
],
372375

373376
'groups' => [

src/Resources/lang/bn/app.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
'delete-success' => 'ঠিকানা সফলভাবে মোছা হয়েছে',
7070
'not-found' => 'সতর্কবার্তা: ঠিকানা পাওয়া যায়নি।',
7171
'update-success' => 'ঠিকানা সফলভাবে আপডেট হয়েছে।',
72+
'already-default' => 'সতর্কবার্তা: এই ঠিকানাটি ইতিমধ্যেই ডিফল্ট হিসাবে সেট করা হয়েছে।',
7273
],
7374

7475
'wishlist' => [
@@ -362,12 +363,14 @@
362363
'login-success' => 'গ্রাহক সফলভাবে লগইন হয়েছে।',
363364
],
364365

365-
'addressess' => [
366+
'addresses' => [
366367
'create-success' => 'গ্রাহকের ঠিকানা সফলভাবে তৈরি হয়েছে।',
367-
'default-update-success' => 'ঠিকানা ডিফল্ট হিসাবে সেট করা হয়েছে',
368-
'delete-success' => 'গ্রাহকের ঠিকানা সফলভাবে মুছে ফেলা হয়েছে',
368+
'default-update-success' => 'ঠিকানা ডিফল্ট হিসাবে সেট করা হয়েছে',
369+
'delete-success' => 'গ্রাহকের ঠিকানা সফলভাবে মুছে ফেলা হয়েছে',
369370
'not-found' => 'সতর্কবার্তা: গ্রাহকের ঠিকানা পাওয়া যায়নি।',
370371
'update-success' => 'গ্রাহকের ঠিকানা সফলভাবে আপডেট হয়েছে।',
372+
'already-default' => 'সতর্কবার্তা: এই ঠিকানাটি ইতিমধ্যেই ডিফল্ট হিসাবে সেট করা হয়েছে।',
373+
],
371374
],
372375

373376
'groups' => [

0 commit comments

Comments
 (0)