From 71d770de47a7f653e775ef6de7451b74204f489f Mon Sep 17 00:00:00 2001 From: Pererinha Date: Tue, 31 Mar 2015 17:15:46 -0300 Subject: [PATCH] partial #5191 --- .../default/cockpit/api/refund.php | 40 ++++++++++---- .../default/cockpit2/api/test/sandbox.php | 6 --- include/library/Crunchbutton/Order.php | 23 ++++++++ .../default/cockpit/orders/content.phtml | 7 ++- www/assets/js/admin.js | 52 +++++++++++++++++++ 5 files changed, 111 insertions(+), 17 deletions(-) diff --git a/include/controllers/default/cockpit/api/refund.php b/include/controllers/default/cockpit/api/refund.php index 44b4d3893..6dbb2731c 100644 --- a/include/controllers/default/cockpit/api/refund.php +++ b/include/controllers/default/cockpit/api/refund.php @@ -7,16 +7,36 @@ class Controller_api_refund extends Crunchbutton_Controller_RestAccount { header('HTTP/1.1 401 Unauthorized'); exit; } - $order = Order::uuid( c::getPagePiece( 2 ) ); - if( $order->id_order ){ - $status = $order->refund(); - if( $status ){ - echo json_encode(['status' => 'success']); - } else { - echo json_encode(['status' => 'false', 'errors' => $status->errors]); - } - } else { - echo json_encode(['status' => 'false', 'errors' => 'invalid order id' ]); + + switch ( c::getPagePiece( 2 ) ) { + case 'check': + $order = Order::uuid( c::getPagePiece( 3 ) ); + if( $order->id_order ){ + $order = $order->get( 0 ); + if( $order->checkIfOrderWasRefunded() ){ + echo json_encode(['status' => 'success']); + } else { + echo json_encode(['status' => 'false']); + } + } else { + echo json_encode(['status' => 'false', 'errors' => 'invalid order id' ]); + } + return; + break; + + default: + $order = Order::uuid( c::getPagePiece( 2 ) ); + if( $order->id_order ){ + $status = $order->refund(); + if( $status ){ + echo json_encode(['status' => 'success']); + } else { + echo json_encode(['status' => 'false', 'errors' => $status->errors]); + } + } else { + echo json_encode(['status' => 'false', 'errors' => 'invalid order id' ]); + } + break; } } } \ No newline at end of file diff --git a/include/controllers/default/cockpit2/api/test/sandbox.php b/include/controllers/default/cockpit2/api/test/sandbox.php index dfcde868c..84bc69cc9 100644 --- a/include/controllers/default/cockpit2/api/test/sandbox.php +++ b/include/controllers/default/cockpit2/api/test/sandbox.php @@ -3,12 +3,6 @@ class Controller_Api_Test_Sandbox extends Cana_Controller { public function init(){ - $order = Order::o( $_GET[ 'id_order' ] ); - if( $order->id_order ){ - echo '
';var_dump( $order->refund() );exit();;
-		} else {
-			echo '
';var_dump( 'error' );exit();
-		}
 
 	}
 }
\ No newline at end of file
diff --git a/include/library/Crunchbutton/Order.php b/include/library/Crunchbutton/Order.php
index 3d97dc200..4b3832197 100644
--- a/include/library/Crunchbutton/Order.php
+++ b/include/library/Crunchbutton/Order.php
@@ -2998,6 +2998,29 @@ class Crunchbutton_Order extends Crunchbutton_Order_Trackchange {
 		return $out;
 	}
 
+	public function checkIfOrderWasRefunded( $force = false ){
+		if( $this->refunded && !$force ){
+			return true;
+		}
+		if( $this->txn ){
+			$env = ( $this->env == 'live' ) ? 'live' : 'dev';
+			$api_key = c::config()->balanced->{$env}->secret;
+			Balanced\Settings::$api_key = $api_key;
+			$url = '/debits/' . $this->txn . '/refunds';
+			$refund = json_decode( json_encode( ( object ) Balanced\Refund::get( $url ) ) );
+			if( $refund && $refund->status && $refund->status == 'succeeded' ){
+				$this->refunded = 1;
+				$this->save();
+				return true;
+			}
+			else {
+				return false;
+			}
+		} else {
+			return false;
+		}
+	}
+
 	public function save() {
 		$new = $this->id_order ? false : true;
 
diff --git a/include/views/default/cockpit/orders/content.phtml b/include/views/default/cockpit/orders/content.phtml
index d54fc7ef7..4ac0bbaf5 100644
--- a/include/views/default/cockpit/orders/content.phtml
+++ b/include/views/default/cockpit/orders/content.phtml
@@ -59,8 +59,13 @@ $permissionSupportView = c::admin()->permission()->check(['global','support-all'
 							refunded) : ?>
 								REFUNDED
 							
-								REFUND
+								REFUND
 								
+
+								
+
+ Check if it was refunded + diff --git a/www/assets/js/admin.js b/www/assets/js/admin.js index be218cff0..9df128c1d 100644 --- a/www/assets/js/admin.js +++ b/www/assets/js/admin.js @@ -886,6 +886,57 @@ App.orders = { location.href = '/orders/content?' + jQuery.param(params); }, createEvents: function(){ + + $(document).on('click', '.check-refunded', function() { + + var el = $(this); + + $( '.refunded-' + el.attr('data-uuid') ).show(); + + el.html(' Checking '); + + var fail = function( result ){ + console.log( result.responseText); + el.html('REFUND'); + var er = result.errors ? "\n\n" + result.errors : 'See the console.log!'; + alert('Refunding fail! ' + er); + } + $.ajax({ + url: '/api/refund/check/' + el.attr('data-uuid'), + success: function( result ){ + console.log('result',result); + try { + if( result.status && result.status == 'success' ){ + $( '.was-refunded-' + el.attr('data-uuid') ).html( 'REFUNDED' ); + el.hide(); + var do_not_reimburse_driver = $( '.do_not_reimburse_driver-' + el.attr('data-uuid') ); + do_not_reimburse_driver.show(); + var do_not_reimburse_driver_value = ( do_not_reimburse_driver.attr( 'data-value' ) == 1 ? 0 : 1 ); + do_not_reimburse_driver.attr( 'data-value', do_not_reimburse_driver_value ); + if( do_not_reimburse_driver_value ){ + do_not_reimburse_driver.find( 'span' ).html( '' ); + } else { + do_not_reimburse_driver.find( 'span' ).html( '' ); + } + + } else if( result.status && result.status == 'false' ){ + alert( 'Not refunded yet!' ) + el.html('Check if it was refunded'); + } else { + fail( result ); + el.html('Check if it was refunded'); + } + } + catch (err) { + fail( result ); + } + }, + error: function( result ){ + fail( result ); + } + }) + }); + $(document).on('click', '.refund', function() { var el = $(this); @@ -927,6 +978,7 @@ App.orders = { try { if( result.status && result.status == 'success' ){ el.html('REFUNDED'); + $( '.check-was-refunded-' + el.attr('data-uuid') ).hide(); } else { fail( result ); }