Skip to content

Commit c2c7f64

Browse files
committed
Finish credit card integration
1 parent 6d6437f commit c2c7f64

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

app/orders/handlers.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package orders
33
import (
44
"fmt"
55
"net/http"
6+
"strconv"
67

78
"github.com/gorilla/schema"
89
amazonpay "github.com/qor/amazon-pay-sdk-go"
10+
"github.com/qor/gomerchant"
911
"github.com/qor/qor-example/config"
1012
"github.com/qor/qor-example/models/orders"
1113
"github.com/qor/qor-example/utils"
@@ -62,8 +64,20 @@ func (ctrl Controller) CompleteCreditCard(w http.ResponseWriter, req *http.Reque
6264
req.ParseForm()
6365

6466
order := getCurrentOrder(w, req)
65-
if order.AmazonOrderReferenceID = req.Form.Get("amazon_order_reference_id"); order.AmazonOrderReferenceID != "" {
66-
order.AmazonAddressAccessToken = req.Form.Get("amazon_address_access_token")
67+
68+
expMonth, _ := strconv.Atoi(req.Form.Get("exp_month"))
69+
expYear, _ := strconv.Atoi(req.Form.Get("exp_year"))
70+
71+
creditCard := gomerchant.CreditCard{
72+
Name: req.Form.Get("name"),
73+
Number: req.Form.Get("creditcard"),
74+
CVC: req.Form.Get("cvv"),
75+
ExpYear: uint(expYear),
76+
ExpMonth: uint(expMonth),
77+
}
78+
79+
if creditCard.ValidNumber() {
80+
// TODO integrate with https://github.com/qor/gomerchant to handle those information
6781
tx := utils.GetDB(req)
6882
err := orders.OrderState.Trigger("checkout", order, tx, "")
6983

@@ -72,11 +86,9 @@ func (ctrl Controller) CompleteCreditCard(w http.ResponseWriter, req *http.Reque
7286
http.Redirect(w, req, "/cart/success", http.StatusFound)
7387
return
7488
}
75-
utils.AddFlashMessage(w, req, err.Error(), "error")
76-
} else {
77-
utils.AddFlashMessage(w, req, "Order Reference ID not Found", "error")
7889
}
7990

91+
utils.AddFlashMessage(w, req, "Invalid Credit Card", "error")
8092
http.Redirect(w, req, "/cart", http.StatusFound)
8193
}
8294

app/orders/views/checkout.tmpl

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
<input type="text" name="name" />
2929
</div>
3030
<div class="grid__col is-2">
31-
<label for="">Expiration date</label>
32-
<input type="text" name="exp_date" />
31+
<label for="">Exp Month</label>
32+
<input type="text" name="exp_month" />
33+
</div>
34+
<div class="grid__col is-2">
35+
<label for="">Exp Year</label>
36+
<input type="text" name="exp_year" />
3337
</div>
3438
<div class="grid__col is-1">
3539
<label for="">CVV</label>

models/orders/order.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
type PaymentMethod = string
1414

1515
const (
16-
COD PaymentMethod = "COD"
17-
AmazonPay PaymentMethod = "AmazonPay"
16+
COD PaymentMethod = "COD"
17+
AmazonPay PaymentMethod = "AmazonPay"
18+
CreditCard PaymentMethod = "CreditCard"
1819
)
1920

2021
type Order struct {

0 commit comments

Comments
 (0)