@@ -905,54 +905,61 @@ func (n *OpenBazaarNode) CalcOrderID(order *pb.Order) (string, error) {
905
905
func (n * OpenBazaarNode ) CalculateOrderTotal (contract * pb.RicardianContract ) (* big.Int , error ) {
906
906
var (
907
907
total = big .NewInt (0 )
908
- physicalGoods = make (map [string ]* pb .Listing )
908
+ physicalGoods = make (map [string ]* repo .Listing )
909
909
toHundredths = func (f float32 ) * big.Float {
910
910
return new (big.Float ).Mul (big .NewFloat (float64 (f )), big .NewFloat (0.01 ))
911
911
}
912
+ v5Order , err = repo .ToV5Order (contract .BuyerOrder , n .LookupCurrency )
912
913
)
914
+ if err != nil {
915
+ return big .NewInt (0 ), fmt .Errorf ("normalizing buyer order: %s" , err .Error ())
916
+ }
913
917
914
- for _ , item := range contract . BuyerOrder .Items {
918
+ for _ , item := range v5Order .Items {
915
919
var itemOriginAmt * repo.CurrencyValue
916
920
l , err := ParseContractForListing (item .ListingHash , contract )
917
921
if err != nil {
918
922
return big .NewInt (0 ), fmt .Errorf ("listing not found in contract for item %s" , item .ListingHash )
919
923
}
920
924
921
- // keep track of physical listings for shipping caluclation
922
- if l .Metadata .ContractType == pb .Listing_Metadata_PHYSICAL_GOOD {
923
- physicalGoods [item .ListingHash ] = l
924
- }
925
-
926
925
rl , err := repo .NewListingFromProtobuf (l )
927
926
if err != nil {
928
927
return big .NewInt (0 ), err
929
928
}
930
929
930
+ nrl , err := rl .Normalize ()
931
+ if err != nil {
932
+ return big .NewInt (0 ), fmt .Errorf ("normalize legacy listing: %s" , err .Error ())
933
+ }
934
+
935
+ // keep track of physical listings for shipping caluclation
936
+ if nrl .GetContractType () == pb .Listing_Metadata_PHYSICAL_GOOD .String () {
937
+ physicalGoods [item .ListingHash ] = nrl
938
+ }
939
+
931
940
// calculate base amount
932
- if l .Metadata .ContractType == pb .Listing_Metadata_CRYPTOCURRENCY &&
933
- l .Metadata .Format == pb .Listing_Metadata_MARKET_PRICE {
934
- var originDef = repo .NewUnknownCryptoDefinition (l .Metadata .CryptoCurrencyCode , 0 )
935
- itemOriginAmt = repo .NewCurrencyValueFromBigInt (GetOrderQuantity (l , item ), originDef )
936
-
937
- if l .Metadata .PriceModifier != 0 {
938
- itemOriginAmt = itemOriginAmt .AddBigFloatProduct (toHundredths (l .Metadata .PriceModifier ))
939
- } else if l .Item .PriceModifier != 0 {
940
- itemOriginAmt = itemOriginAmt .AddBigFloatProduct (toHundredths (l .Item .PriceModifier ))
941
+ if nrl .GetContractType () == pb .Listing_Metadata_CRYPTOCURRENCY .String () &&
942
+ nrl .GetFormat () == pb .Listing_Metadata_MARKET_PRICE .String () {
943
+ var originDef = repo .NewUnknownCryptoDefinition (nrl .GetCryptoCurrencyCode (), 0 )
944
+ itemOriginAmt = repo .NewCurrencyValueFromBigInt (GetOrderQuantity (nrl .GetProtobuf (), item ), originDef )
945
+
946
+ if priceModifier := nrl .GetPriceModifier (); priceModifier != 0 {
947
+ itemOriginAmt = itemOriginAmt .AddBigFloatProduct (toHundredths (priceModifier ))
941
948
}
942
949
} else {
943
- oAmt , err := repo . NewCurrencyValueFromProtobuf ( l . Item . BigPrice , l . Item . PriceCurrency )
950
+ oAmt , err := nrl . GetPrice ( )
944
951
if err != nil {
945
952
return big .NewInt (0 ), err
946
953
}
947
954
itemOriginAmt = oAmt
948
955
}
949
956
950
957
// apply surcharges
951
- selectedSku , err := GetSelectedSku (l , item .Options )
958
+ selectedSku , err := GetSelectedSku (nrl . GetProtobuf () , item .Options )
952
959
if err != nil {
953
960
return big .NewInt (0 ), err
954
961
}
955
- skus , err := rl .GetSkus ()
962
+ skus , err := nrl .GetSkus ()
956
963
if err != nil {
957
964
return big .NewInt (0 ), err
958
965
}
@@ -973,7 +980,7 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (*b
973
980
if err != nil {
974
981
return big .NewInt (0 ), err
975
982
}
976
- for _ , vendorCoupon := range l .Coupons {
983
+ for _ , vendorCoupon := range nrl . GetProtobuf () .Coupons {
977
984
if id .B58String () == vendorCoupon .GetHash () {
978
985
if disc , ok := new (big.Int ).SetString (vendorCoupon .BigPriceDiscount , 10 ); ok && disc .Cmp (big .NewInt (0 )) > 0 {
979
986
// apply fixed discount
@@ -987,7 +994,7 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (*b
987
994
}
988
995
989
996
// apply taxes
990
- for _ , tax := range l .Taxes {
997
+ for _ , tax := range nrl . GetProtobuf () .Taxes {
991
998
for _ , taxRegion := range tax .TaxRegions {
992
999
if contract .BuyerOrder .Shipping .Country == taxRegion {
993
1000
itemOriginAmt = itemOriginAmt .AddBigFloatProduct (toHundredths (tax .Percentage ))
@@ -997,9 +1004,9 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (*b
997
1004
}
998
1005
999
1006
// apply requested quantity
1000
- if ! (l . Metadata . ContractType == pb .Listing_Metadata_CRYPTOCURRENCY &&
1001
- l . Metadata . Format == pb .Listing_Metadata_MARKET_PRICE ) {
1002
- if itemQuantity := GetOrderQuantity (l , item ); itemQuantity .Cmp (big .NewInt (0 )) > 0 {
1007
+ if ! (nrl . GetContractType () == pb .Listing_Metadata_CRYPTOCURRENCY . String () &&
1008
+ nrl . GetFormat () == pb .Listing_Metadata_MARKET_PRICE . String () ) {
1009
+ if itemQuantity := GetOrderQuantity (nrl . GetProtobuf () , item ); itemQuantity .Cmp (big .NewInt (0 )) > 0 {
1003
1010
itemOriginAmt = itemOriginAmt .MulBigInt (itemQuantity )
1004
1011
} else {
1005
1012
log .Debugf ("missing quantity for order, assuming quantity 1" )
@@ -1012,7 +1019,7 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (*b
1012
1019
return big .NewInt (0 ), fmt .Errorf ("preparing reserve currency converter: %s" , err .Error ())
1013
1020
}
1014
1021
1015
- finalItemAmount , err := itemOriginAmt .ConvertUsingProtobufDef (contract . BuyerOrder .Payment .AmountCurrency , cc )
1022
+ finalItemAmount , _ , err := itemOriginAmt .ConvertUsingProtobufDef (v5Order .Payment .AmountCurrency , cc )
1016
1023
if err != nil {
1017
1024
return big .NewInt (0 ), err
1018
1025
}
@@ -1030,7 +1037,7 @@ func (n *OpenBazaarNode) CalculateOrderTotal(contract *pb.RicardianContract) (*b
1030
1037
return total , nil
1031
1038
}
1032
1039
1033
- func (n * OpenBazaarNode ) calculateShippingTotalForListings (contract * pb.RicardianContract , listings map [string ]* pb .Listing ) (* big.Int , error ) {
1040
+ func (n * OpenBazaarNode ) calculateShippingTotalForListings (contract * pb.RicardianContract , listings map [string ]* repo .Listing ) (* big.Int , error ) {
1034
1041
type itemShipping struct {
1035
1042
primary * big.Int
1036
1043
secondary * big.Int
@@ -1039,20 +1046,24 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
1039
1046
version uint32
1040
1047
}
1041
1048
var (
1049
+ v5Order , err = repo .ToV5Order (contract .BuyerOrder , n .LookupCurrency )
1042
1050
is []itemShipping
1043
1051
shippingTotal * big.Int
1044
1052
)
1053
+ if err != nil {
1054
+ return big .NewInt (0 ), fmt .Errorf ("normalizing buyer order: %s" , err .Error ())
1055
+ }
1045
1056
1046
1057
// First loop through to validate and filter out non-physical items
1047
- for _ , item := range contract . BuyerOrder .Items {
1048
- listing , ok := listings [item .ListingHash ]
1058
+ for _ , item := range v5Order .Items {
1059
+ rl , ok := listings [item .ListingHash ]
1049
1060
if ! ok {
1050
1061
continue
1051
1062
}
1052
1063
1053
1064
// Check selected option exists
1054
1065
shippingOptions := make (map [string ]* pb.Listing_ShippingOption )
1055
- for _ , so := range listing .ShippingOptions {
1066
+ for _ , so := range rl . GetProtobuf () .ShippingOptions {
1056
1067
shippingOptions [strings .ToLower (so .Name )] = so
1057
1068
}
1058
1069
option , ok := shippingOptions [strings .ToLower (item .ShippingOption .Name )]
@@ -1069,7 +1080,7 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
1069
1080
for _ , country := range option .Regions {
1070
1081
regions [country ] = true
1071
1082
}
1072
- _ , shipsToMe := regions [contract . BuyerOrder .Shipping .Country ]
1083
+ _ , shipsToMe := regions [v5Order .Shipping .Country ]
1073
1084
_ , shipsToAll := regions [pb .CountryCode_ALL ]
1074
1085
if ! shipsToMe && ! shipsToAll {
1075
1086
return big .NewInt (0 ), errors .New ("listing does ship to selected country" )
@@ -1089,22 +1100,22 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
1089
1100
if ! ok {
1090
1101
return big .NewInt (0 ), errors .New ("shipping service not found in listing" )
1091
1102
}
1092
- servicePrice , err := repo .NewCurrencyValueFromProtobuf (service .BigPrice , listing .Item .PriceCurrency )
1103
+ servicePrice , err := repo .NewCurrencyValueFromProtobuf (service .BigPrice , rl . GetProtobuf () .Item .PriceCurrency )
1093
1104
if err != nil {
1094
1105
return big .NewInt (0 ), fmt .Errorf ("parsing service price (%v): %s" , service .Name , err .Error ())
1095
1106
}
1096
- convertedShippingPrice , err := servicePrice .ConvertUsingProtobufDef (contract . BuyerOrder .Payment .AmountCurrency , cc )
1107
+ convertedShippingPrice , _ , err := servicePrice .ConvertUsingProtobufDef (v5Order .Payment .AmountCurrency , cc )
1097
1108
if err != nil {
1098
1109
return big .NewInt (0 ), fmt .Errorf ("converting service price (%s): %s" , service .Name , err .Error ())
1099
1110
}
1100
1111
1101
- auxServicePrice , err := repo .NewCurrencyValueFromProtobuf (service .BigAdditionalItemPrice , listing .Item .PriceCurrency )
1112
+ auxServicePrice , err := repo .NewCurrencyValueFromProtobuf (service .BigAdditionalItemPrice , rl . GetProtobuf () .Item .PriceCurrency )
1102
1113
if err != nil {
1103
1114
return big .NewInt (0 ), fmt .Errorf ("parsing aux service price (%v): %s" , service .Name , err .Error ())
1104
1115
}
1105
1116
var convertedAuxPrice = repo .NewCurrencyValueFromBigInt (big .NewInt (0 ), convertedShippingPrice .Currency )
1106
1117
if auxServicePrice .IsPositive () {
1107
- finalAux , err := auxServicePrice .ConvertUsingProtobufDef (contract . BuyerOrder .Payment .AmountCurrency , cc )
1118
+ finalAux , _ , err := auxServicePrice .ConvertUsingProtobufDef (v5Order .Payment .AmountCurrency , cc )
1108
1119
if err != nil {
1109
1120
return big .NewInt (0 ), fmt .Errorf ("converting aux service price (%s): %s" , service .Name , err .Error ())
1110
1121
}
@@ -1113,19 +1124,19 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
1113
1124
1114
1125
// Calculate tax percentage
1115
1126
var shippingTaxPercentage float32
1116
- for _ , tax := range listing .Taxes {
1127
+ for _ , tax := range rl . GetProtobuf () .Taxes {
1117
1128
regions := make (map [pb.CountryCode ]bool )
1118
1129
for _ , taxRegion := range tax .TaxRegions {
1119
1130
regions [taxRegion ] = true
1120
1131
}
1121
- _ , ok := regions [contract . BuyerOrder .Shipping .Country ]
1132
+ _ , ok := regions [v5Order .Shipping .Country ]
1122
1133
if ok && tax .TaxShipping {
1123
1134
shippingTaxPercentage = tax .Percentage / 100
1124
1135
}
1125
1136
}
1126
1137
1127
1138
var qty uint64
1128
- if q := quantityForItem (listing . Metadata . Version , item ); q .IsUint64 () {
1139
+ if q := quantityForItem (rl . GetVersion () , item ); q .IsUint64 () {
1129
1140
qty = q .Uint64 ()
1130
1141
} else {
1131
1142
orderID , _ := n .CalcOrderID (contract .BuyerOrder )
@@ -1136,7 +1147,7 @@ func (n *OpenBazaarNode) calculateShippingTotalForListings(contract *pb.Ricardia
1136
1147
secondary : convertedAuxPrice .AmountBigInt (),
1137
1148
quantity : qty ,
1138
1149
shippingTaxPercentage : shippingTaxPercentage ,
1139
- version : listing . Metadata . Version ,
1150
+ version : rl . GetVersion () ,
1140
1151
})
1141
1152
}
1142
1153
0 commit comments