2
2
3
3
namespace Webkul \GraphQLAPI \Helper ;
4
4
5
+ use Illuminate \Support \Facades \DB ;
6
+ use Webkul \Paypal \Payment \SmartButton ;
5
7
use Webkul \Sales \Repositories \OrderRepository ;
6
8
use Webkul \Sales \Repositories \InvoiceRepository ;
7
9
@@ -14,7 +16,8 @@ class PaymentHelper
14
16
*/
15
17
public function __construct (
16
18
protected OrderRepository $ orderRepository ,
17
- protected InvoiceRepository $ invoiceRepository
19
+ protected InvoiceRepository $ invoiceRepository ,
20
+ protected SmartButton $ smartButton ,
18
21
) {}
19
22
20
23
public function createInvoice ($ cart , $ paymentDetail , $ order )
@@ -26,24 +29,25 @@ public function createInvoice($cart, $paymentDetail, $order)
26
29
) {
27
30
return ;
28
31
}
29
-
32
+
30
33
$ paymentMethod = $ cart ->payment ->method ;
31
-
32
34
$ paymentIsCompleted = false ;
33
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
+
34
42
if (
35
- $ paymentMethod == ' paypal_standard '
36
- || $ paymentMethod == ' paypal_smart_button '
43
+ $ paymentIsCompleted
44
+ && $ order -> canInvoice ()
37
45
) {
38
- $ paymentIsCompleted = $ this ->checkPaypalPaymentStatus ($ paymentDetail ['transaction_id ' ]);
39
- }
40
-
41
- if ($ paymentIsCompleted ) {
42
- $ this ->orderRepository ->update (['status ' => 'processing ' ], $ order ->id );
46
+ request ()->merge ($ paymentDetail );
47
+
48
+ $ this ->invoiceRepository ->create ($ this ->prepareInvoiceData ($ order ));
43
49
44
- if ($ order ->canInvoice ()) {
45
- $ invoice = $ this ->invoiceRepository ->create ($ this ->prepareInvoiceData ($ order ));
46
- }
50
+ $ this ->orderRepository ->update (['status ' => 'processing ' ], $ order ->id );
47
51
}
48
52
}
49
53
@@ -63,37 +67,46 @@ protected function prepareInvoiceData($order)
63
67
return $ invoiceData ;
64
68
}
65
69
66
- private function checkPaypalPaymentStatus ($ paymentId )
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 )
67
76
{
68
77
$ clientId = core ()->getConfigData ('sales.payment_methods.paypal_standard.client_id ' );
69
78
$ secretKey = core ()->getConfigData ('sales.payment_methods.paypal_standard.client_secret ' );
79
+ $ sandbox = core ()->getConfigData ('sales.payment_methods.paypal_standard.sandbox ' );
70
80
71
- if (core ()->getConfigData ('sales.payment_methods.paypal_standard.sandbox ' )) {
72
- $ url = "https://api-m.sandbox.paypal.com/v1/payments/payment/ {$ paymentId }" ;
73
- } else {
74
- $ url = "https://api-m.paypal.com/v1/payments/payment/ {$ paymentId }" ;
75
- }
81
+ $ url = $ sandbox
82
+ ? "https://api-m.sandbox.paypal.com/v1/payments/payment/ {$ paymentId }"
83
+ : "https://api-m.paypal.com/v1/payments/payment/ {$ paymentId }" ;
76
84
77
85
$ curl = curl_init ($ url );
78
- curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
79
- curl_setopt ($ curl , CURLOPT_HTTPHEADER , [
80
- 'Content-Type: application/json ' ,
81
- 'Authorization: Basic ' . base64_encode ("{$ clientId }: {$ secretKey }" ),
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
+ ],
82
93
]);
83
94
84
95
$ response = curl_exec ($ curl );
85
-
86
- if (curl_errno ($ curl )) {
87
- throw new \Exception ('Curl error: ' . curl_error ($ curl ));
88
- }
89
96
curl_close ($ curl );
90
97
91
- $ responseData = json_decode ($ response , true );
92
-
93
- if (isset ($ responseData ['state ' ]) && $ responseData ['state ' ] == 'approved ' ) {
94
- return true ;
98
+ if ($ response ) {
99
+ $ responseData = json_decode ($ response , true );
100
+ return $ responseData ['state ' ] ?? '' === 'approved ' ;
95
101
}
96
-
102
+
97
103
return false ;
98
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
+ }
99
112
}
0 commit comments