Created
May 16, 2019 08:42
-
-
Save akmalhazim/d55d11f5f29c50256f5737c542ac3591 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function removePaymentMethod( PaymentMethod $paymentMethod, User $user ) { | |
$token = $paymentMethod->token; | |
$BTPaymentMethod = $this->gateway->paymentMethod()->find($token); | |
$isDefault = $BTPaymentMethod->isDefault(); | |
$result = $this->gateway->paymentMethod()->delete($token); | |
// dd($result); | |
if ( $result->success ) { | |
// run some executions. | |
$paymentMethod->delete(); | |
// assign new default card. | |
$paymentMethodNew = $user->paymentMethods()->latest()->first(); | |
if( $isDefault ) { | |
if ( $paymentMethodNew ) { | |
$result = $this->gateway->customer()->update($user->braintree_id, [ | |
'defaultPaymentMethodToken' => $paymentMethodNew->token | |
]); | |
$paymentMethodNew->update([ | |
'default' => true | |
]); | |
} | |
} | |
} else { | |
// notify admin. | |
abort(500); | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment