Skip to content

Instantly share code, notes, and snippets.

@akmalhazim
Created May 16, 2019 08:42
Show Gist options
  • Save akmalhazim/d55d11f5f29c50256f5737c542ac3591 to your computer and use it in GitHub Desktop.
Save akmalhazim/d55d11f5f29c50256f5737c542ac3591 to your computer and use it in GitHub Desktop.
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