@@ -106,15 +106,23 @@ public function setOrder(\Magento\Sales\Model\Order $order)
106106 public function cancel ($ breadTransactionId , $ amount = 0 , $ lineItems = [])
107107 {
108108 $ this ->logger ->info ('Call API Cancel method. Bread trxId: ' . $ breadTransactionId );
109+
110+ $ apiVersion = $ this ->helper ->getApiVersion ();
111+ $ payment = $ this ->order ->getPayment ();
112+ $ paymentApiVersion = $ payment ->getData ('bread_api_version ' );
113+ $ this ->logger ->info ('Payment API version: ' . $ paymentApiVersion );
114+ if (!is_null ($ paymentApiVersion ) && in_array ($ paymentApiVersion , ['classic ' ,'bread_2 ' ])) {
115+ $ apiVersion = strtolower ($ paymentApiVersion );
116+ }
117+
109118 /* Check if already canceled in bread */
110- $ transaction = $ this ->getInfo ($ breadTransactionId );
119+ $ transaction = $ this ->getInfo ($ breadTransactionId, $ apiVersion );
111120 if (strtoupper ($ transaction ['status ' ]) === self ::STATUS_CANCELED || strtoupper ($ transaction ['status ' ]) === 'CANCELLED ' ) {
112121 $ this ->logger ->info ('Transaction is already canceled in Bread. Bread trxId: ' . $ breadTransactionId );
113122 return $ transaction ;
114123 }
115124
116- $ apiVersion = $ this ->helper ->getApiVersion ();
117-
125+
118126 if ($ apiVersion === 'bread_2 ' ) {
119127 $ this ->logger ->info ('Bread platform transaction. Bread trxId: ' . $ breadTransactionId );
120128 $ currency = $ transaction ['totalAmount ' ]['currency ' ];
@@ -178,6 +186,12 @@ public function authorize($breadTransactionId, $amount, $merchantOrderId = null)
178186 $ validateAmount = $ this ->getInfo ($ breadTransactionId );
179187 $ this ->logger ->info ('Trx: ' . json_encode ($ validateAmount ));
180188 $ apiVersion = $ this ->helper ->getApiVersion ();
189+
190+ $ payment = $ this ->order ->getPayment ();
191+ $ paymentApiVersion = $ payment ->getData ('bread_api_version ' );
192+ if (!is_null ($ paymentApiVersion ) && in_array ($ paymentApiVersion , ['classic ' ,'bread_2 ' ])) {
193+ $ apiVersion = strtolower ($ paymentApiVersion );
194+ }
181195
182196 // set transaction id so it can be fetched for split payment cancel
183197 $ this ->setBreadTransactionId ($ breadTransactionId );
@@ -229,10 +243,11 @@ public function authorize($breadTransactionId, $amount, $merchantOrderId = null)
229243 'orderId ' => $ merchantOrderId
230244 ]);
231245 $ data = '{"externalID":" ' . $ merchantOrderId . '","metadata":{"externalMerchantData":"externalInfo"}} ' ;
246+
232247 $ updateMerchantOrderIdResult = $ this ->call (
233248 $ this ->getTransactionInfoUrl ($ breadTransactionId ),
234249 $ data ,
235- \Zend_Http_Client::PUT ,
250+ \Zend_Http_Client::PATCH ,
236251 false
237252 );
238253 $ this ->logger ->info ('Response: ' . json_encode ($ updateMerchantOrderIdResult ) . 'Bread trxId: ' . $ breadTransactionId );
@@ -311,6 +326,13 @@ public function settle($breadTransactionId, $amount = null, $currency = null)
311326 {
312327 $ this ->logger ->info ('Call API settle method. Bread trxId: ' . $ breadTransactionId );
313328 $ apiVersion = $ this ->helper ->getApiVersion ();
329+
330+ $ payment = $ this ->order ->getPayment ();
331+ $ paymentApiVersion = $ payment ->getData ('bread_api_version ' );
332+ if (!is_null ($ paymentApiVersion ) && in_array ($ paymentApiVersion , ['classic ' ,'bread_2 ' ])) {
333+ $ apiVersion = strtolower ($ paymentApiVersion );
334+ }
335+
314336 if ($ apiVersion === 'bread_2 ' ) {
315337 $ validateAmount = $ this ->getInfo ($ breadTransactionId );
316338 $ currency = trim ($ validateAmount ['totalAmount ' ]['currency ' ]);
@@ -358,7 +380,14 @@ public function settle($breadTransactionId, $amount = null, $currency = null)
358380 public function refund ($ breadTransactionId , $ amount = 0 , $ lineItems = [], $ currency = null )
359381 {
360382 $ this ->logger ->info ('Call API refund method. Bread trxId: ' . $ breadTransactionId );
361- $ apiVersion = $ this ->helper ->getApiVersion ();
383+ $ apiVersion = $ this ->helper ->getApiVersion ();
384+
385+ $ payment = $ this ->order ->getPayment ();
386+ $ paymentApiVersion = $ payment ->getData ('bread_api_version ' );
387+ if (!is_null ($ paymentApiVersion ) && in_array ($ paymentApiVersion , ['classic ' ,'bread_2 ' ])) {
388+ $ apiVersion = strtolower ($ paymentApiVersion );
389+ }
390+
362391 if ($ apiVersion === 'bread_2 ' ) {
363392 $ validateAmount = $ this ->getInfo ($ breadTransactionId );
364393 $ currency = trim ($ validateAmount ['totalAmount ' ]['currency ' ]);
@@ -397,11 +426,11 @@ public function refund($breadTransactionId, $amount = 0, $lineItems = [], $curre
397426 * @return mixed
398427 * @throws \Exception
399428 */
400- public function getInfo ($ breadTransactionId )
429+ public function getInfo ($ breadTransactionId, $ apiVersion = null )
401430 {
402431 $ this ->logger ->info ('Call API getInfo method. Bread trxId: ' . $ breadTransactionId );
403432 return $ this ->call (
404- $ this ->getTransactionInfoUrl ($ breadTransactionId ),
433+ $ this ->getTransactionInfoUrl ($ breadTransactionId, $ apiVersion ),
405434 [],
406435 \Zend_Http_Client::GET
407436 );
@@ -433,7 +462,7 @@ public function submitCartData($data)
433462 */
434463 public function getAsLowAs ($ data )
435464 {
436- $ baseUrl = $ this ->helper ->getTransactionApiUrl ($ this ->getStoreId ());
465+ $ baseUrl = $ this ->helper ->getTransactionApiUrl (' classic ' , $ this ->getStoreId ());
437466 $ asLowAsUrl = join ('/ ' , [ trim ($ baseUrl , '/ ' ), 'aslowas ' ]);
438467 return $ this ->call (
439468 $ asLowAsUrl ,
@@ -455,11 +484,21 @@ public function getAsLowAs($data)
455484 */
456485 protected function call ($ url , $ data , $ method = \Zend_Http_Client::POST , $ jsonEncode = true )
457486 {
487+ $ this ->logger ->info ('We are at call ' );
458488 $ storeId = $ this ->getStoreId ();
459- $ username = $ this ->helper ->getApiPublicKey ($ storeId );
460- $ password = $ this ->helper ->getApiSecretKey ($ storeId );
461489 $ apiVersion = $ this ->helper ->getApiVersion ();
462490
491+ if (!is_null ($ this ->order )) {
492+ $ payment = $ this ->order ->getPayment ();
493+ $ paymentApiVersion = $ payment ->getData ('bread_api_version ' );
494+ if (!is_null ($ paymentApiVersion ) && in_array ($ paymentApiVersion , ['classic ' , 'bread_2 ' ])) {
495+ $ apiVersion = strtolower ($ paymentApiVersion );
496+ }
497+ }
498+ $ this ->logger ->info ("API Version :: " . $ apiVersion );
499+ $ username = $ this ->helper ->getApiPublicKey ($ apiVersion , $ storeId );
500+ $ password = $ this ->helper ->getApiSecretKey ($ apiVersion , $ storeId );
501+
463502 if ($ apiVersion === 'bread_2 ' ) {
464503 try {
465504 $ authToken = $ this ->helper ->getAuthToken ();
@@ -639,7 +678,7 @@ protected function callBread($url, $authToken, $data, $method = \Zend_Http_Clien
639678 curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ data );
640679 }
641680
642- if ($ method == \Zend_Http_Client::PUT ) {
681+ if ($ method == \Zend_Http_Client::PUT || $ method == \Zend_Http_Client:: PATCH ) {
643682 $ authorization = "Authorization: Bearer " . $ authToken ;
644683 curl_setopt ($ curl , CURLOPT_HTTPHEADER , [
645684 'Content-Type: application/json ' ,
@@ -764,7 +803,7 @@ public function sendEmail($cartId, $email, $name)
764803 public function setShippingDetails ($ transactionId , $ trackingNumber , $ carrierName )
765804 {
766805 $ apiVersion = $ this ->helper ->getApiVersion ();
767- $ baseUrl = $ this ->helper ->getTransactionApiUrl ($ this ->getStoreId ());
806+ $ baseUrl = $ this ->helper ->getTransactionApiUrl (null , $ this ->getStoreId ());
768807
769808 if ($ apiVersion === 'bread_2 ' ) {
770809
@@ -797,10 +836,13 @@ public function setShippingDetails($transactionId, $trackingNumber, $carrierName
797836 * @param $transactionId
798837 * @return string
799838 */
800- protected function getTransactionInfoUrl ($ transactionId )
801- {
839+ protected function getTransactionInfoUrl ($ transactionId, $ breadApiVersion = null )
840+ {
802841 $ apiVersion = $ this ->helper ->getApiVersion ();
803- $ baseUrl = $ this ->helper ->getTransactionApiUrl ($ this ->getStoreId ());
842+ if (!is_null ($ breadApiVersion )) {
843+ $ apiVersion = $ breadApiVersion ;
844+ }
845+ $ baseUrl = $ this ->helper ->getTransactionApiUrl ($ apiVersion , $ this ->getStoreId ());
804846 if ($ apiVersion === 'bread_2 ' ) {
805847 return join ('/ ' , [trim ($ baseUrl , '/ ' ), 'transaction ' , trim ($ transactionId , '/ ' )]);
806848 } else {
@@ -816,7 +858,7 @@ protected function getTransactionInfoUrl($transactionId)
816858 */
817859 protected function getUpdateTransactionUrl ($ transactionId )
818860 {
819- $ baseUrl = $ this ->helper ->getTransactionApiUrl ($ this ->getStoreId ());
861+ $ baseUrl = $ this ->helper ->getTransactionApiUrl (' classic ' , $ this ->getStoreId ());
820862 return join (
821863 '/ ' ,
822864 [ trim ($ baseUrl , '/ ' ), 'transactions/actions ' , trim ($ transactionId , '/ ' ) ]
@@ -830,7 +872,8 @@ protected function getUpdateTransactionUrl($transactionId)
830872 * @return string
831873 */
832874 protected function getAuthTokenUrl () {
833- $ baseUrl = $ this ->helper ->getTransactionApiUrl ($ this ->getStoreId ());
875+ $ baseUrl = $ this ->helper ->getTransactionApiUrl ('bread_2 ' , $ this ->getStoreId ());
876+ //return join('/', [trim($baseUrl, '/'), 'auth/service/authorize']);
834877 return join ('/ ' , [trim ($ baseUrl , '/ ' ), 'auth/sa/authenticate ' ]);
835878 }
836879
@@ -851,6 +894,7 @@ protected function generateAuthToken($url, $apiKey, $apiSecret) {
851894 $ curl = curl_init ($ url );
852895 try {
853896 curl_setopt ($ curl , CURLOPT_HEADER , 0 );
897+ curl_setopt ($ curl , CURLOPT_USERPWD , $ apiKey . ': ' . $ apiSecret );
854898 curl_setopt ($ curl , CURLOPT_TIMEOUT , 30 );
855899 curl_setopt ($ curl , CURLOPT_POST , 1 );
856900 curl_setopt ($ curl , CURLOPT_HTTPHEADER , [
@@ -948,7 +992,7 @@ protected function getStoreId()
948992 * @param type $action
949993 */
950994 protected function getUpdateTransactionUrlPlatform ($ transactionId , $ action ) {
951- $ baseUrl = $ this ->helper ->getTransactionApiUrl ($ this ->getStoreId ());
995+ $ baseUrl = $ this ->helper ->getTransactionApiUrl (' bread_2 ' , $ this ->getStoreId ());
952996 $ url = join ('/ ' , [trim ($ baseUrl , '/ ' ), 'transaction ' , $ transactionId , $ action ]);
953997 return $ url ;
954998 }
0 commit comments