Skip to content

Commit

Permalink
Merge pull request #9 from TibebeJS/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
TibebeJS authored Dec 31, 2020
2 parents bbaba32 + 09aa41b commit 73eeb25
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 19 deletions.
28 changes: 27 additions & 1 deletion checkout/chkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Models included in this package:
package checkout

import (
"fmt"
"reflect"

"github.com/google/go-querystring/query"
)

Expand Down Expand Up @@ -39,7 +42,7 @@ func NewYenePayCheckOut() *YenePayCheckOut {
// Generate Checkout URL for Express Checkout
func (self *YenePayCheckOut) GetCheckoutUrlForExpress(checkoutOptions *CheckoutOption, checkoutItem *CheckoutItem) string {

v, _ := query.Values(checkoutOptions.GetCartFields())
v, _ := query.Values(checkoutOptions.GetExpressFields())

checkoutUrl := self.CheckoutBaseUrlProd + "?" + v.Encode()

Expand All @@ -49,3 +52,26 @@ func (self *YenePayCheckOut) GetCheckoutUrlForExpress(checkoutOptions *CheckoutO

return checkoutUrl
}

// Generate Checkout URL for Cart Checkout
func (self *YenePayCheckOut) GetCheckoutUrlForCart(checkoutOptions *CheckoutOption, checkoutItems []CheckoutItem) string {

v, _ := query.Values(checkoutOptions)

checkoutUrl := self.CheckoutBaseUrlProd + "?" + v.Encode()

if checkoutOptions.UseSandbox {
checkoutUrl = self.CheckoutBaseUrlSandbox + "?" + v.Encode()
}

for i, item := range checkoutItems {
v := reflect.ValueOf(item.GetCartFields())
typeOfS := v.Type()

for j := 0; j < v.NumField(); j++ {
checkoutUrl += fmt.Sprintf("&Items[%d].%s=%v", i, typeOfS.Field(j).Name, v.Field(j).Interface())
}
}

return checkoutUrl
}
16 changes: 16 additions & 0 deletions checkout/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ func (self *CheckoutItem) ToJSON() (string, error) {
}
}

// Return Only Required Fields for Cart Checkout
// I.e. Exclude all Fields that are related with tax, etc..
func (self *CheckoutItem) GetCartFields() interface{} {
return struct {
ItemId string
ItemName string
UnitPrice float64
Quantity int
}{
self.ItemId,
self.ItemName,
self.UnitPrice,
self.Quantity,
}
}

// CheckoutItem Constructor
func NewCheckoutItem(ItemId string, ItemName string, UnitPrice float64, Quantity int, Discount float64, HandlingFee float64, DeliveryFee float64, Tax1 float64, Tax2 float64) *CheckoutItem {
return &CheckoutItem{
Expand Down
28 changes: 28 additions & 0 deletions checkout/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,31 @@ func TestNewCheckoutItem(t *testing.T) {

assert.Exactly(t, expected, item)
}

func TestGetCartFields(t *testing.T) {
actual := NewCheckoutItem(
"2",
"item-name",
10.0,
2,
0.0,
0.0,
0.0,
0.0,
0.0,
).GetCartFields()

expected := struct {
ItemId string
ItemName string
UnitPrice float64
Quantity int
}{
"2",
"item-name",
10.0,
2,
}

assert.Exactly(t, expected, actual)
}
8 changes: 4 additions & 4 deletions checkout/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type CheckoutOption struct {
TotalItemsHandlingFee float64
}

// Return Only Required Fields for Cart Checkout
// Return Only Required Fields for Express Checkout
// I.e. Exclude all Fields that start with 'Total'
func (self *CheckoutOption) GetCartFields() interface{} {
func (self *CheckoutOption) GetExpressFields() interface{} {
return struct {
UseSandbox bool
Process CheckoutType
Expand Down Expand Up @@ -54,8 +54,8 @@ func (self *CheckoutOption) ToJSON(forCart bool) (string, error) {

var fields interface{}

if forCart {
fields = self.GetCartFields()
if !forCart {
fields = self.GetExpressFields()
} else {
fields = self
}
Expand Down
8 changes: 4 additions & 4 deletions checkout/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestNewCheckoutOption(t *testing.T) {
assert.Exactly(t, expected, actual)
}

func TestOptionsToJSONForCart(t *testing.T) {
func TestOptionsToJSONForExpress(t *testing.T) {
actual, _ := NewCheckoutOption(
true,
ExpressCheckout,
Expand All @@ -61,7 +61,7 @@ func TestOptionsToJSONForCart(t *testing.T) {
0.0,
0.0,
0.0,
).ToJSON(true)
).ToJSON(false)

expected := `
{
Expand All @@ -80,7 +80,7 @@ func TestOptionsToJSONForCart(t *testing.T) {
require.JSONEq(t, expected, actual)

}
func TestOptionsToJSON(t *testing.T) {
func TestOptionsToJSONForCart(t *testing.T) {
actual, _ := NewCheckoutOption(
true,
ExpressCheckout,
Expand All @@ -96,7 +96,7 @@ func TestOptionsToJSON(t *testing.T) {
0.0,
0.0,
0.0,
).ToJSON(false)
).ToJSON(true)

expected := `
{
Expand Down
47 changes: 37 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
func main() {
checkoutOption := checkout.NewCheckoutOption(
true,
checkout.ExpressCheckout,
"222",
"sdfsd",
"sdfsd",
"sdfsd",
"sdfsd",
3,
"sdfsdsdfsd",
checkout.CartCheckout,
"0694",
"http://localhost:3000/Home/PaymentSuccessReturnUrl",
"http://localhost:3000/Home/CancelReturnUrl",
"http://localhost:3000/Home/IPNUrl",
"http://localhost:3000/Home/FailureUrl",
2880,
"ab-cd",
2.0,
3.0,
1.0,
0.0,
5.0,
10.0,
)
Expand All @@ -29,7 +29,7 @@ func main() {
"PC",
30.0,
2,
0.2,
0.0,
0.0,
0.0,
0.0,
Expand All @@ -45,4 +45,31 @@ func main() {

fmt.Println("***************************")
fmt.Println(check.GetCheckoutUrlForExpress(checkoutOption, checkoutItem))

fmt.Println("***************************")

fmt.Println(check.GetCheckoutUrlForCart(checkoutOption, []checkout.CheckoutItem{
{
ItemId: "544",
ItemName: "PC",
UnitPrice: 30.0,
Quantity: 2,
Discount: 0.0,
HandlingFee: 0.0,
DeliveryFee: 0.0,
Tax1: 0.0,
Tax2: 0.0,
},
{
ItemId: "541",
ItemName: "PC2",
UnitPrice: 30.0,
Quantity: 2,
Discount: 0.0,
HandlingFee: 0.0,
DeliveryFee: 0.0,
Tax1: 0.0,
Tax2: 0.0,
},
}))
}

0 comments on commit 73eeb25

Please sign in to comment.