44
55namespace Devscast \Flexpay ;
66
7- use Devscast \Flexpay \Data \Method ;
87use Devscast \Flexpay \Exception \NetworkException ;
8+ use Devscast \Flexpay \Request \MobileRequest ;
9+ use Devscast \Flexpay \Request \Request ;
10+ use Devscast \Flexpay \Request \VposRequest ;
911use Devscast \Flexpay \Response \CheckResponse ;
1012use Devscast \Flexpay \Response \FlexpayResponse ;
1113use Devscast \Flexpay \Response \PaymentResponse ;
14+ use Devscast \Flexpay \Response \VposResponse ;
1215use Symfony \Component \HttpClient \HttpClient ;
1316use Symfony \Component \HttpClient \Retry \GenericRetryStrategy ;
1417use Symfony \Component \HttpClient \RetryableHttpClient ;
2326 *
2427 * @author bernard-ng <bernard@devscast.tech>
2528 */
26- final readonly class Client
29+ final class Client
2730{
2831 private HttpClientInterface $ http ;
2932
3033 private Serializer $ serializer ;
3134
3235 public function __construct (
33- public Credential $ credential ,
34- public Environment $ environment = Environment::SANDBOX
36+ public readonly Credential $ credential ,
37+ public readonly Environment $ environment = Environment::SANDBOX ,
3538 ) {
36- $ this ->serializer = new Serializer (normalizers: [
37- new BackedEnumNormalizer (),
38- new ObjectNormalizer (),
39- ]);
39+ $ this ->serializer = new Serializer (
40+ normalizers: [
41+ new ObjectNormalizer (),
42+ new BackedEnumNormalizer (),
43+ ]
44+ );
4045
4146 $ this ->http = new RetryableHttpClient (
4247 client: HttpClient::create (
@@ -53,33 +58,21 @@ public function __construct(
5358 }
5459
5560 /**
56- * Cette interface permet d’ envoyer une requête de redirection de paiement à FlexPay
61+ * Permet d' envoyer une directement intention de paiement sur le mobile money du client
5762 *
58- * @throws NetworkException quand une erreur réseau survient
59- * @throws \InvalidArgumentException quand le numéro de téléphone n'est pas fourni pour un paiement mobile
63+ * @throws NetworkException
6064 */
61- public function pay ( PaymentEntry $ entry , Method $ method = Method:: MOBILE ): PaymentResponse
65+ public function mobile ( MobileRequest $ request ): PaymentResponse
6266 {
63- /**
64- * Définit ici, pour éviter de préciser manuellement le marchant à chaque fois
65- */
66- $ entry ->setMerchant ($ this ->credential ->merchant );
67-
68- if ($ method === Method::MOBILE && $ entry ->phone === null ) {
69- throw new \InvalidArgumentException ('phone number should be provided for mobile payment ' );
70- }
67+ $ request ->setCredential ($ this ->credential );
7168
7269 try {
7370 /** @var PaymentResponse $response */
7471 $ response = $ this ->getMappedData (
7572 type: PaymentResponse::class,
76- data: $ this ->http ->request (
77- method: 'POST ' ,
78- url: sprintf ('%s/paymentService ' , $ this ->environment ->getBaseUrl ()),
79- options: [
80- 'json ' => $ this ->serializer ->normalize ($ entry ),
81- ]
82- )->toArray ()
73+ data: $ this ->http ->request ('POST ' , $ this ->environment ->getMobilePaymentUrl (), [
74+ 'json ' => $ request ->getPayload (),
75+ ])->toArray ()
8376 );
8477
8578 return $ response ;
@@ -89,14 +82,41 @@ public function pay(PaymentEntry $entry, Method $method = Method::MOBILE): Payme
8982 }
9083
9184 /**
92- * Cette interface permet d'obtenir une réponse de paiement provenant de FlexPay
85+ * Créer une URL unique de paiement via le gateway de Flexpay
86+ * Cela permet d'utiliser différente méthode de paiement
87+ * y compris une carte bancaire (VISA, MASTERCARD, etc.)
88+ *
89+ * @throws NetworkException
9390 */
94- public function handleCallback ( array $ data ): PaymentResponse
91+ public function vpos ( VposRequest $ request ): VposResponse
9592 {
96- /** @var PaymentResponse $payment */
97- $ payment = $ this ->getMappedData (PaymentResponse::class, $ data );
93+ $ request ->setCredential ($ this ->credential );
9894
99- return $ payment ;
95+ try {
96+ dd (
97+ /** @var VposResponse $response */
98+ $ response = $ this ->getMappedData (
99+ type: VposResponse::class,
100+ data: $ this ->http ->request ('POST ' , $ this ->environment ->getVposAskUrl (), [
101+ 'json ' => $ request ->getPayload (),
102+ ])->toArray ()
103+ )
104+ );
105+ } catch (\Throwable $ e ) {
106+ $ this ->createExceptionFromResponse ($ e );
107+ }
108+ }
109+
110+ /**
111+ * @throws NetworkException
112+ */
113+ public function pay (Request $ request ): PaymentResponse |VposResponse
114+ {
115+ return match (true ) {
116+ $ request instanceof MobileRequest => $ this ->mobile ($ request ),
117+ $ request instanceof VposRequest => $ this ->vpos ($ request ),
118+ default => throw new \RuntimeException ('Unsupported request ' )
119+ };
100120 }
101121
102122 /**
@@ -112,10 +132,9 @@ public function check(string $orderNumber): CheckResponse
112132 /** @var CheckResponse $response */
113133 $ response = $ this ->getMappedData (
114134 type: CheckResponse::class,
115- data: $ this ->http ->request (
116- method: 'GET ' ,
117- url: sprintf ('%s/check/%s ' , $ this ->environment ->getBaseUrl (), $ orderNumber )
118- )->toArray ()
135+ data: $ this ->http
136+ ->request ('GET ' , $ this ->environment ->getCheckStatusUrl ($ orderNumber ))
137+ ->toArray ()
119138 );
120139
121140 return $ response ;
@@ -124,6 +143,17 @@ public function check(string $orderNumber): CheckResponse
124143 }
125144 }
126145
146+ /**
147+ * Cette interface permet d'obtenir une réponse de paiement provenant de FlexPay
148+ */
149+ public function handleCallback (array $ data ): PaymentResponse
150+ {
151+ /** @var PaymentResponse $payment */
152+ $ payment = $ this ->getMappedData (PaymentResponse::class, $ data );
153+
154+ return $ payment ;
155+ }
156+
127157 /**
128158 * @psalm-param class-string<FlexpayResponse> $type
129159 */
0 commit comments