Skip to content

Commit

Permalink
Merge pull request #32 from tighten/alk/refund
Browse files Browse the repository at this point in the history
Add Refund from Charge Detail Page
  • Loading branch information
faxblaster authored Aug 17, 2021
2 parents a32c6fa + 8a4a096 commit 3960165
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 19 deletions.
66 changes: 64 additions & 2 deletions dist/js/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -2866,14 +2866,49 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//



/* harmony default export */ __webpack_exports__["default"] = ({
props: ['chargeId'],

components: {
'charge-detail-card': __WEBPACK_IMPORTED_MODULE_0__components_ChargeDetailCard_vue___default.a
},
data: function data() {
return {
charge: undefined,
deleting: false
};
},

methods: {
refund: function refund(chargeId) {
var _this = this;

this.deleting = true;

Nova.request().post('/nova-vendor/nova-stripe/stripe/charges/' + this.chargeId + '/refund').then(function (response) {
Nova.success('Charge Successfully Refunded!');
_this.$refs.detail.getCharge();
});

this.deleting = false;
}
}
});

Expand Down Expand Up @@ -2996,6 +3031,7 @@ Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
Nova.request().get('/nova-vendor/nova-stripe/stripe/charges/' + this.chargeId).then(function (response) {
_this.charge = response.data.charge;
_this.initialLoading = false;
_this.$emit('charge-loaded', response.data.charge);
});
},
formatMoney: function formatMoney(amount, currency) {
Expand Down Expand Up @@ -3106,7 +3142,33 @@ var render = function() {
[
_c("heading", { staticClass: "mb-6" }, [_vm._v("Charge Details")]),
_vm._v(" "),
_c("charge-detail-card", { attrs: { "charge-id": _vm.chargeId } })
_c("div", { staticClass: "flex flex-row-reverse mb-3" }, [
_vm.charge && !_vm.charge.refunded
? _c(
"button",
{
staticClass: "btn-primary px-4 py-2 rounded",
attrs: { disabled: _vm.deleting },
on: {
click: function($event) {
return _vm.refund(_vm.charge.id)
}
}
},
[_vm._v("\n Refund\n ")]
)
: _vm._e()
]),
_vm._v(" "),
_c("charge-detail-card", {
ref: "detail",
attrs: { "charge-id": _vm.chargeId },
on: {
"charge-loaded": function($event) {
_vm.charge = $event
}
}
})
],
1
)
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
"dependencies": {
"currency.js": "^1.2.1",
"vue": "^2.5.0"
},
"prettier": {
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 4,
"printWidth": 80
}
}
1 change: 1 addition & 0 deletions resources/js/components/ChargeDetailCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default {
.then((response) => {
this.charge = response.data.charge
this.initialLoading = false
this.$emit('charge-loaded', response.data.charge);
})
},
Expand Down
54 changes: 46 additions & 8 deletions resources/js/views/Detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,56 @@
<div>
<heading class="mb-6">Charge Details</heading>

<charge-detail-card :charge-id="chargeId"></charge-detail-card>
<div class="flex flex-row-reverse mb-3">
<button
v-if="charge && !charge.refunded"
class="btn-primary px-4 py-2 rounded"
@click="refund(charge.id)"
:disabled="deleting"
>
Refund
</button>
</div>

<charge-detail-card
ref="detail"
:charge-id="chargeId"
@charge-loaded="charge = $event"
/>
</div>
</template>

<script>
import ChargeDetailCard from '../components/ChargeDetailCard.vue'
import ChargeDetailCard from '../components/ChargeDetailCard.vue';
export default {
props: ['chargeId'],
components: {
'charge-detail-card': ChargeDetailCard,
},
data() {
return {
charge: undefined,
deleting: false,
};
},
methods: {
refund(chargeId) {
this.deleting = true;
export default {
props: ['chargeId'],
Nova.request()
.post(
'/nova-vendor/nova-stripe/stripe/charges/' +
this.chargeId +
'/refund'
)
.then((response) => {
Nova.success('Charge Successfully Refunded!');
this.$refs.detail.getCharge();
});
components: {
'charge-detail-card': ChargeDetailCard,
}
}
this.deleting = false;
},
},
};
</script>
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@

Route::get('/stripe/charges', StripeChargesController::class . '@index');
Route::get('/stripe/charges/{id}', StripeChargesController::class . '@show');
Route::post('/stripe/charges/{id}/refund', StripeChargesController::class . '@refund');
Route::get('/stripe/balance', StripeBalanceController::class . '@index');
19 changes: 19 additions & 0 deletions src/Clients/StripeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Stripe\Balance;
use Stripe\Charge;
use Stripe\Refund;

class StripeClient
{
Expand Down Expand Up @@ -52,4 +53,22 @@ public function getBalance()

}
}

public function refundCharge($chargeId)
{
try {
return Refund::create(['charge' => $chargeId], ['api_key' => $this->apiKey]);
} catch (Exception $e) {

}
}

public function createCharge(array $params)
{
try {
return Charge::create($params, ['api_key' => $this->apiKey]);
} catch (Exception $e) {

}
}
}
6 changes: 6 additions & 0 deletions src/Http/StripeChargesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tighten\NovaStripe\Http;

use Illuminate\Routing\Controller;
use Stripe\Charge;
use Tighten\NovaStripe\Clients\StripeClient;

class StripeChargesController extends Controller
Expand All @@ -20,4 +21,9 @@ public function show($id)
{
return response()->json(['charge' => (new StripeClient)->getCharge($id)]);
}

public function refund($id)
{
return response()->json((new StripeClient)->refundCharge($id));
}
}
41 changes: 32 additions & 9 deletions tests/StripeChargeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
namespace Tighten\NovaStripe\Tests;

use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Config;
use Stripe\Charge;
use Stripe\StripeClient;
use Tighten\NovaStripe\Clients\StripeClient;

class StripeChargeControllerTest extends TestCase
{
Expand All @@ -17,11 +15,10 @@ class StripeChargeControllerTest extends TestCase
public function setUp(): void
{
parent::setUp();
$this->stripe = new StripeClient(Config::get('services.stripe.secret'));
$this->stripe = new StripeClient;

$this->charge = $this->stripe->charges->all()->count()
? $this->stripe->charges->all(['limit' => 1])->first()
: $this->stripe->charges->create([
$this->charge = $this->findSuccessfulNonRefundedCharge()
?: $this->stripe->createCharge([
'amount' => $this->faker->numberBetween(50, 1000),
'currency' => 'usd',
'source' => 'tok_mastercard',
Expand Down Expand Up @@ -54,7 +51,7 @@ public function it_returns_a_list_of_charges()
public function it_returns_charge_details()
{
$response = $this->get('nova-vendor/nova-stripe/stripe/charges/' . $this->charge->id);
$stripeCharge = Charge::retrieve(['id' => $this->charge->id, 'expand' => ['balance_transaction']], ['api_key' => Config::get('services.stripe.secret')]);
$stripeCharge = $this->stripe->getCharge($this->charge->id);

$response->assertJsonFragment([
'id' => $stripeCharge->id,
Expand Down Expand Up @@ -86,12 +83,38 @@ public function it_returns_charge_details()
/** @test */
public function it_shows_the_current_balance()
{
$balance = $this->stripe->balance->retrieve();
$balance = $this->stripe->getBalance();

$this->get('nova-vendor/nova-stripe/stripe/balance')
->assertJsonFragment([
'available' => $balance->available,
'pending' => $balance->pending,
]);
}

/** @test */
public function it_can_refund_a_charge_successfully()
{
$this->post('nova-vendor/nova-stripe/stripe/charges/' . $this->charge->id . '/refund')
->assertSuccessful()
->assertJsonFragment([
'charge' => $this->charge->id,
'status' => 'succeeded',
]);

$this->assertTrue($this->stripe->getCharge($this->charge->id)->refunded);
}

public function findSuccessfulNonRefundedCharge()
{
$charges = $this->stripe->listCharges();

foreach ($charges as $charge) {
if (! $charge->refunded && $charge->status === 'succeeded') {
return $charge;
}
}

return null;
}
}

0 comments on commit 3960165

Please sign in to comment.