cancel subscription
This commit is contained in:
@ -164,6 +164,8 @@ func (app *application) CreateCustomerAndSubscribeToPlan(w http.ResponseWriter,
|
||||
ExpiryMonth: data.ExpiryMonth,
|
||||
ExpiryYear: data.ExpiryYear,
|
||||
TransactionStatusID: 2,
|
||||
PaymentIntent: subscription.ID,
|
||||
PaymentMethod: data.PaymentMethod,
|
||||
}
|
||||
txnID, err := app.SaveTransaction(txn)
|
||||
if err != nil {
|
||||
@ -587,3 +589,50 @@ func (app *application) RefundCharge(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (app *application) CancelSubscription(w http.ResponseWriter, r *http.Request) {
|
||||
var subToCancel struct {
|
||||
ID int `json:"id"`
|
||||
PaymentIntent string `json:"pi"`
|
||||
Currency string `json:"currency"`
|
||||
}
|
||||
|
||||
err := app.readJSON(w, r, &subToCancel)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// validate
|
||||
|
||||
card := cards.Card{
|
||||
Secret: app.config.stripe.secret,
|
||||
Key: app.config.stripe.key,
|
||||
Currency: subToCancel.Currency,
|
||||
}
|
||||
|
||||
err = card.CancelSubscription(subToCancel.PaymentIntent)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// update status in DB
|
||||
err = app.DB.UpdateOrderStatus(subToCancel.ID, 3)
|
||||
if err != nil {
|
||||
app.badRequest(
|
||||
w,
|
||||
r,
|
||||
errors.New("the subscription was refunded, but the database could not be updated"),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var resp jsonResponse
|
||||
resp.OK = true
|
||||
resp.Message = "Subscription canceled"
|
||||
|
||||
app.writeJSON(w, http.StatusOK, resp)
|
||||
}
|
||||
|
@ -53,12 +53,9 @@ func (app *application) writeJSON(
|
||||
}
|
||||
|
||||
func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err error) error {
|
||||
var payload struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
var payload jsonResponse
|
||||
|
||||
payload.Error = true
|
||||
payload.OK = false
|
||||
payload.Message = err.Error()
|
||||
|
||||
out, err := json.MarshalIndent(payload, "", "\t")
|
||||
@ -73,11 +70,8 @@ func (app *application) badRequest(w http.ResponseWriter, r *http.Request, err e
|
||||
}
|
||||
|
||||
func (app *application) invalidCredentials(w http.ResponseWriter) error {
|
||||
var payload struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
payload.Error = true
|
||||
var payload jsonResponse
|
||||
payload.OK = false
|
||||
payload.Message = "invalid authentication credentials"
|
||||
|
||||
err := app.writeJSON(w, http.StatusUnauthorized, payload)
|
||||
|
@ -36,6 +36,7 @@ func (app *application) routes() http.Handler {
|
||||
mux.Post("/all-subscriptions", app.AllSubscriptions)
|
||||
mux.Post("/get-sale/{id}", app.GetSale)
|
||||
mux.Post("/refund", app.RefundCharge)
|
||||
mux.Post("/cancel-subscription", app.CancelSubscription)
|
||||
})
|
||||
mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
|
||||
mux.Post("/api/reset-password", app.ResetPassword)
|
||||
|
@ -384,9 +384,16 @@ func (app *application) ShowSale(w http.ResponseWriter, r *http.Request) {
|
||||
stringMap := make(map[string]string)
|
||||
stringMap["title"] = "Sale"
|
||||
stringMap["cancel"] = "/admin/all-sales"
|
||||
stringMap["refund-url"] = "/api/admin/refund"
|
||||
stringMap["refund-btn"] = "Refund Order"
|
||||
stringMap["refund-badge"] = "Refunded"
|
||||
|
||||
intMap := make(map[string]int)
|
||||
intMap["isRefund"] = 1
|
||||
|
||||
if err := app.renderTemplate(w, r, "sale", &templateData{
|
||||
StringMap: stringMap,
|
||||
IntMap: intMap,
|
||||
}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
@ -396,9 +403,16 @@ func (app *application) ShowSubscriptions(w http.ResponseWriter, r *http.Request
|
||||
stringMap := make(map[string]string)
|
||||
stringMap["title"] = "Subscriptions"
|
||||
stringMap["cancel"] = "/admin/all-subscriptions"
|
||||
stringMap["refund-url"] = "/api/admin/cancel-subscription"
|
||||
stringMap["refund-btn"] = "Cancel Subscription"
|
||||
stringMap["refund-badge"] = "Cancelled"
|
||||
|
||||
intMap := make(map[string]int)
|
||||
intMap["is-refund"] = 0
|
||||
|
||||
if err := app.renderTemplate(w, r, "sale", &templateData{
|
||||
StringMap: stringMap,
|
||||
IntMap: intMap,
|
||||
}); err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ All Subscriptions
|
||||
<th>Customer</th>
|
||||
<th>Product</th>
|
||||
<th>Amount</th>
|
||||
<th>Status</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
@ -4,7 +4,7 @@
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
<h2 class="mt-5">Sale</h2>
|
||||
<span id="refunded" class="badge bg-danger d-none">Refunded</span>
|
||||
<span id="refunded" class="badge bg-danger d-none">{{index .StringMap "refund-badge"}}</span>
|
||||
<span id="charged" class="badge bg-success d-none">Charged</span>
|
||||
<hr>
|
||||
<div class="alert alert-danger text-center d-none" id="messages"></div>
|
||||
@ -22,7 +22,7 @@
|
||||
</div>
|
||||
<hr>
|
||||
<a href='{{ index .StringMap "cancel" }}' class="btn btn-info">Cancel</a>
|
||||
<a id="refund-btn" href="#!" class="btn btn-warning d-none">Refund Order</a>
|
||||
<a id="refund-btn" href="#!" class="btn btn-warning d-none">{{index .StringMap "refund-btn"}}</a>
|
||||
<input type="hidden" id="pi" value="">
|
||||
<input type="hidden" id="charge-amount" value="">
|
||||
<input type="hidden" id="currency" value="">
|
||||
@ -32,8 +32,10 @@
|
||||
<script type="module">
|
||||
import {showInfo, refund} from "/static/js/sale.js"
|
||||
showInfo({{.API}});
|
||||
|
||||
const api = {{.API}} + {{index .StringMap "refund-url"}}
|
||||
document.getElementById("refund-btn").addEventListener("click", function(event) {
|
||||
refund({{.API}});
|
||||
refund(api, {{index .IntMap "is-refund"}});
|
||||
});
|
||||
</script>
|
||||
{{ end }}
|
||||
|
Reference in New Issue
Block a user