/// <summary>
 /// Creates a <see cref="IapPurchaseDocument" /> from a <see cref="IapPurchaseContract" />.
 /// </summary>
 /// <returns>The <see cref="IapPurchaseDocument" />.</returns>
 public static IapPurchaseDocument CreateFromContract(IapPurchaseContract contract)
 {
     return new IapPurchaseDocument
     {
         Id = contract.IapPurchaseId,
         UserId = contract.UserId,
         ProductId = contract.ProductId,
         PurchaseDatetime = new DateDocument
         {
             Date = contract.PurchaseDatetime
         },
         ExpirationDatetime = new DateDocument
         {
             Date = contract.ExpirationDatetime
         },
         GoldIncrement = contract.GoldIncrement
     };
 }
        /// <summary>
        /// Validates the xml signature.
        /// </summary>
        /// <param name="receipt">Receipt to validate.</param>
        /// <returns>A validated or null IapPurchaseContract object.</returns>
        public IapPurchaseContract ValidateXmlSignature(XmlDocument receipt)
        {
            if (receipt == null)
            {
                throw new IapValidationException(IapValidationError.NoReceipt, "ValidateXmlSignature: no receipt");
            }

            var iapPurchaseResult = new IapPurchaseContract();

            // For actual receipt validation we strongly suggest you follow the example
            // https://code.msdn.microsoft.com/windowsapps/In-app-purchase-receipt-c3e0bce4
            var isValid = true;

            if (isValid)
            {
                DateTime datetime;

                var productReceipt = receipt.GetElementsByTagName("ProductReceipt");

                if (DateTime.TryParse(productReceipt[0].Attributes["PurchaseDate"].Value, out datetime))
                {
                    iapPurchaseResult.PurchaseDatetime = datetime;
                }

                if (DateTime.TryParse(productReceipt[0].Attributes["ExpirationDate"].Value, out datetime))
                {
                    iapPurchaseResult.ExpirationDatetime = datetime;
                }

                iapPurchaseResult.IapPurchaseId = productReceipt[0].Attributes["Id"].Value;
                iapPurchaseResult.ProductId = productReceipt[0].Attributes["ProductId"].Value;
            }
            else
            {
                throw new IapValidationException(IapValidationError.BadSignature,
                    "ValidateXmlSignature: Bad receipt, signature not valid");
            }

            return iapPurchaseResult;
        }
 /// <summary>
 /// Inserts receipt and adds gold to user.
 /// </summary>
 /// <param name="validatedIapReciept">Validated receipt values.</param>
 /// <returns>User object containing new gold balance.</returns>
 public Task<UserContract> InsertIapPurchase(IapPurchaseContract validatedIapReciept)
 {
     return _repository.InsertIapPurchase(validatedIapReciept);
 }
        /// <summary>
        /// Inserts receipt and adds gold to user.
        /// </summary>
        /// <param name="validatedIapReciept">Validated receipt values.</param>
        /// <returns>User object containing new gold balance.</returns>
        public async Task<UserContract> InsertIapPurchase(IapPurchaseContract validatedIapReciept)
        {
            var document = _documentClient.CreateDocumentQuery<IapPurchaseDocument>(DocumentCollectionUri)
                .Where(d => d.DocumentType == IapPurchaseDocument.DocumentTypeIdentifier)
                .Where(d => d.DocumentVersion == _currentDocumentVersion)
                .Where(i => i.Id == validatedIapReciept.IapPurchaseId)
                .AsEnumerable().FirstOrDefault();

            if (document != null)
            {
                throw new DataLayerException(DataLayerError.DuplicateKeyInsert,
                    $"Iap Purchase with Id={validatedIapReciept.IapPurchaseId} already exists");
            }

            if (validatedIapReciept.GoldIncrement > 0)
            {
                // Handle gold balance changes and create transaction record
                await
                    ExecuteGoldTransactionSproc(validatedIapReciept.UserId, SystemUserId,
                        validatedIapReciept.GoldIncrement,
                        GoldTransactionType.IapGoldTransaction);
            }

            var iapPurchaseDocument = IapPurchaseDocument.CreateFromContract(validatedIapReciept);
            await _documentClient.CreateDocumentAsync(DocumentCollectionUri, iapPurchaseDocument);

            return await GetUser(validatedIapReciept.UserId);
        }
 public Task<UserContract> InsertIapPurchase(IapPurchaseContract validatedIapReciept)
 {
     throw new NotImplementedException();
 }