public void BdkEncoded() { string encodedId = "o-uP6gAAAAAAAAEA"; var bdkId = new BdkId(encodedId); Console.WriteLine("AccountId:{0} SubRefId{1}", bdkId.AccountId, bdkId.SubRefId); }
/// <summary> /// Compare to another BdkId and returns an integer. /// </summary> /// <param name="bid2"></param> /// <returns></returns> public int CompareTo(BdkId bid2) { if (bid2 == null) { return(-3); } if (this.accountId < bid2.accountId) { return(-1); } if (this.accountId > bid2.accountId) { return(+1); } if (this.idType < bid2.idType) { return(-2); } if (this.idType > bid2.idType) { return(+2); } int diff = 0; switch (this.idType) { case BdkIdType.AccountId: case BdkIdType.ServiceInstanceId: case BdkIdType.SubscriptionId: diff = (this.subRefId - bid2.subRefId) + (this.serviceInstanceId - bid2.serviceInstanceId); break; case BdkIdType.AddressId: diff = (this.addressId - bid2.addressId); break; case BdkIdType.PaymentId: diff = (this.payInstId - bid2.payInstId); break; case BdkIdType.ItemInstanceId: diff = (this.itemRefId - bid2.itemRefId); break; case BdkIdType.BillingReferenceId: diff = (this.billingRefId - bid2.billingRefId); break; case BdkIdType.GlobalPaymentMethodId: diff = (this.globalPaymentMethodId - bid2.globalPaymentMethodId); break; default: throw new ArgumentException("Invalid ID Type found in the BdkId object"); } return(diff); }
/// <summary> /// Format a given encoded account id string to be the format like: /// 12345-12345-12345-12345. /// </summary> /// <param name="encodedId"></param> /// <returns></returns> public static string FormatAccountId(string encodedId) { if (encodedId == null || encodedId.Length == 0) { return(""); } BdkId bid = new BdkId(encodedId); return(bid.GetFormattedAccountId()); }
/// <summary> /// IComparable.CompareTo() function to compare /// against another object of same type. /// </summary> /// <param name="obj"></param> /// <returns></returns> public int CompareTo(object obj) { BdkId bid2 = obj as BdkId; if (bid2 != null) { return(this.CompareTo(bid2)); } throw new ArgumentException("Invalid object passed in for comparison"); }
public static bool IsBdkId(string inputString) { try { BdkId bdkId = new BdkId(inputString); return(true); } catch { return(false); } }
private static string ConstructEncodedId(BdkId bid) { string encodedId; // convert values into a byte array byte[] rawData = new Byte[12]; ulong acctId = (ulong)bid.accountId; int i = 0; for (i = 0; i < 8; i++) { rawData[i] = (byte)(acctId & 0xFF); acctId >>= 8; } // TODO: Handle endian-ness later on switch (bid.idType) { case BdkIdType.AccountId: case BdkIdType.ServiceInstanceId: case BdkIdType.SubscriptionId: if (bid.idType == BdkIdType.AccountId) { bid.serviceInstanceId = 0; bid.subRefId = 0; } else if (bid.idType == BdkIdType.SubscriptionId) { bid.serviceInstanceId = 0; } rawData[i + 3] = (byte)((bid.subRefId >> 8) & 0xFF); rawData[i + 2] = (byte)(bid.subRefId & 0xFF); rawData[i + 1] = (byte)((bid.serviceInstanceId >> 8) & 0xFF); rawData[i + 0] = (byte)(bid.serviceInstanceId & 0xFF); break; case BdkIdType.PaymentId: // indicator values bit 31-29 has 100 rawData[i + 3] = 0x4 << 5; rawData[i + 2] = 0; rawData[i + 1] = (byte)((bid.payInstId >> 8) & 0xFF); rawData[i + 0] = (byte)(bid.payInstId & 0xFF); break; case BdkIdType.AddressId: // indicator values bit 31-29 has 101 rawData[i + 3] = 0x5 << 5; rawData[i + 2] = 0; rawData[i + 1] = (byte)((bid.addressId >> 8) & 0xFF); rawData[i + 0] = (byte)(bid.addressId & 0xFF); break; case BdkIdType.ItemInstanceId: if (0 != (bid.itemRefId & 0xF0000000)) { throw new ArgumentException("Item instance too big."); } // indicator values bit 31-28 has 1101 rawData[i + 3] = (byte)((0xD << 4) | ((bid.itemRefId >> 24) & 0x0F)); rawData[i + 2] = (byte)((bid.itemRefId >> 16) & 0xFF); rawData[i + 1] = (byte)((bid.itemRefId >> 8) & 0xFF); rawData[i + 0] = (byte)(bid.itemRefId & 0xFF); break; case BdkIdType.Period: // billing period have only 6 digits if (bid.billingPeriod.ToString().Length > 6) { throw new ArgumentException("Billing Period cannot have more than 6 digits."); } rawData[i + 3] = 0xC << 4; // billing period is less than 2^24 (=16777216) so the highest byte doesn't contain it rawData[i + 2] = (byte)((bid.billingPeriod >> 16) & 0xFF); rawData[i + 1] = (byte)((bid.billingPeriod >> 8) & 0xFF); rawData[i + 0] = (byte)(bid.billingPeriod & 0xFF); break; case BdkIdType.BillingReferenceId: if (0 != (bid.billingRefId & 0xFE000000)) { throw new ArgumentException("Billing reference too big."); } rawData[i + 3] = (byte)(0xE0 | ((bid.billingRefId >> 24) & 0x1)); rawData[i + 2] = (byte)((bid.billingRefId >> 16) & 0xFF); rawData[i + 1] = (byte)((bid.billingRefId >> 8) & 0xFF); rawData[i + 0] = (byte)(bid.billingRefId & 0xFF); break; case BdkIdType.GlobalPaymentMethodId: // indicator values bit 31-28 has 1111 rawData[i + 3] = 0xF << 4; rawData[i + 2] = 0; //16bit is big enough for Global payment method id rawData[i + 1] = (byte)((bid.globalPaymentMethodId >> 8) & 0xFF); rawData[i + 0] = (byte)(bid.globalPaymentMethodId & 0xFF); break; default: throw new ArgumentException("Invalid BdkId object supplied for encoding."); } // switch // do base64 encoding encodedId = Convert.ToBase64String(rawData, 0, rawData.Length); // do URL escape string temp = encodedId; encodedId = temp.Replace('/', '-'); return(encodedId); }
private static void DecodeIdFromString(string encodedId, BdkId bid) { if (encodedId == null) { throw new ArgumentException("Invalid object passed in for decoding"); } encodedId = encodedId.Trim(); if (encodedId.Length != 16) { throw new ArgumentException("Encoded object id length must be 16"); } // undo URL escape string temp = encodedId; encodedId = temp.Replace('-', '/'); // do base64 decoding of the encoded id byte[] decodedValue = Convert.FromBase64String(encodedId); // extract first 64-bits as the account id // TODO: Clean up this conversion process and make it Endian safe! bid.accountId = 0; for (int i = 7; i >= 0; i--) { bid.accountId = (bid.accountId << 8) + decodedValue[i]; } // extract next 32-bits as per the encoding scheme uint val = 0; for (int i = 3; i >= 0; i--) { val = (val << 8) + decodedValue[i + 8]; } // now do bit-manipulation checks to assign exact values. switch ((val >> 29) & 0x7) { case 0: case 1: case 2: case 3: // high order bit is NOT SET => // the value is just a plain Account ID with sub details bid.subRefId = (short)((val >> 16) & 0x7FFF); bid.serviceInstanceId = (short)(val & 0xFFFF); if (bid.subRefId == 0) { if (bid.serviceInstanceId == 0) { bid.idType = BdkIdType.AccountId; } else { throw new ArgumentException( "Invalid ObjectId. Sub ref ID is 0 but Service Instance ID is not"); } } else if (bid.serviceInstanceId == 0) { bid.idType = BdkIdType.SubscriptionId; } else { bid.idType = BdkIdType.ServiceInstanceId; } break; case 4: // the value is just plain account id with payInst Id bid.payInstId = (short)(val & 0xFFFF); bid.idType = BdkIdType.PaymentId; break; case 5: // value = accountId + address Id bid.addressId = (short)(val & 0xFFFF); bid.idType = BdkIdType.AddressId; break; case 6: // looks like an item instnace id //make sure that the bit is 28 is 1 //0 is Period if (1 == ((val >> 28) & 0x1)) { bid.itemRefId = (int)(val & 0x0FFFFFFF); // get 28 bits bid.idType = BdkIdType.ItemInstanceId; } else { bid.idType = BdkIdType.Period; } break; case 7: if (0 == ((val >> 28) & 0x1)) { // Bits 27 thru 25 are 0 for future extention // Bits 24 thru 0 are billing reference id (2^25 possible billing ref ids per account) bid.billingRefId = (int)(val & 0x1FFFFFF); bid.idType = BdkIdType.BillingReferenceId; } else if (1 == ((val >> 28) & 0x1)) { //1111 == 0xF, globalPaymentMethodId, only the low 16 bit are used for this value bid.globalPaymentMethodId = (int)(val & 0xFFFF); bid.idType = BdkIdType.GlobalPaymentMethodId; } else { bid.idType = BdkIdType.InvalidId; throw new ArgumentException("Invalid value supplied for encoded Billing object ID: " + encodedId); } break; default: break; } // switch }