Esempio n. 1
0
        /// <summary>
        ///     This is the entry point for the consumer, that will purchase and consume a single unit of the first price, of the
        ///     first service of the first device found.
        /// </summary>
        /// <param name="service">The WPWithin service endpoint.</param>
        /// <returns>Indication of the success of the operation.</returns>
        public CommandResult MakePurchase(WPWithinService service)
        {
            service.SetupDevice("my-device", "an example consumer device");

            ServiceMessage firstDevice = DiscoverDevices(service)?.FirstOrDefault();

            if (firstDevice == null)
            {
                _error.WriteLine("No devices discovered.  Is a producer running on your network?");
                return(CommandResult.NonCriticalError);
            }
            else
            {
                _output.WriteLine("Discovered device: {0}", firstDevice);
            }

            // Configure our WPWithinService as a consumer, using a dummy payment card.
            ConnectToDevice(service, firstDevice);

            // Get the first service offered by the device.
            ServiceDetails firstService = GetAvailableServices(service)?.FirstOrDefault();

            if (firstService == null)
            {
                _error.WriteLine("Couldn't find any services offered by {0}", firstDevice);
                return(CommandResult.NonCriticalError);
            }
            else
            {
                _output.WriteLine("Found first service {0}", firstService);
            }

            // Get the first first offered by the first service on the first device.
            Price firstPrice = GetServicePrices(service, firstService.ServiceId)?.FirstOrDefault();

            if (firstPrice == null)
            {
                return(CommandResult.NonCriticalError);
            }

            TotalPriceResponse priceResponse = GetServicePriceQuote(service, firstService.ServiceId, 1,
                                                                    firstPrice.Id);

            if (priceResponse == null)
            {
                return(CommandResult.CriticalError);
            }

            PurchaseService(service, firstService.ServiceId, priceResponse);

            return(CommandResult.Success);
        }
Esempio n. 2
0
 public static ThriftTotalPriceResponse Create(TotalPriceResponse request)
 {
     return(new ThriftTotalPriceResponse
     {
         ServerId = request.ServerId,
         UnitsToSupply = request.UnitsToSupply,
         PriceId = request.PriceId,
         ClientId = request.ClientId,
         MerchantClientKey = request.MerchantClientKey,
         PaymentReferenceId = request.PaymentReferenceId,
         TotalPrice = request.TotalPrice
     });
 }
        private TotalPriceResponse GetServicePriceQuote(WPWithinService service, int serviceId, int numberOfUnits,
                                                        int priceId)
        {
            TotalPriceResponse tpr = service.SelectService(serviceId, numberOfUnits, priceId);

            if (tpr != null)
            {
                _output.WriteLine("Received price quote:");
                _output.WriteLine("Merchant client key: {0}", tpr.MerchantClientKey);
                _output.WriteLine("Payment reference id: {0}", tpr.PaymentReferenceId);
                _output.WriteLine("Units to supply: {0}", tpr.UnitsToSupply);
                _output.WriteLine("Total price: {0}", tpr.TotalPrice);
            }
            else
            {
                _output.WriteLine("No Total Price Response received");
            }
            return(tpr);
        }
        public CommandResult MakePurchase(WPWithinService service)
        {
            service.SetupDevice("my-device", "an example consumer device");

            ServiceMessage firstDevice = DiscoverDevices(service)?.FirstOrDefault();

            if (firstDevice == null)
            {
                return(CommandResult.NonCriticalError);
            }

            connectToDevice(service, firstDevice);

            ServiceDetails firstService = GetAvailableServices(service)?.FirstOrDefault();

            if (firstService == null)
            {
                return(CommandResult.NonCriticalError);
            }

            Price firstPrice = GetServicePrices(service, firstService.ServiceId.Value)?.FirstOrDefault();

            if (firstPrice == null)
            {
                return(CommandResult.NonCriticalError);
            }

            TotalPriceResponse priceResponse = GetServicePriceQuote(service, firstService.ServiceId.Value, 1,
                                                                    firstPrice.Id.Value);

            if (priceResponse == null)
            {
                return(CommandResult.CriticalError);
            }

            PurchaseService(service, firstService.ServiceId.Value, priceResponse);

            return(CommandResult.Success);
        }
        private PaymentResponse PurchaseService(WPWithinService service, int serviceId, TotalPriceResponse pReq)
        {
            PaymentResponse pResp = service.MakePayment(pReq);

            if (pResp != null)
            {
                _output.WriteLine("Payment response: ");
                _output.WriteLine("Client UUID: {0}", pResp.ClientUuid);
                _output.WriteLine("Client ServiceId: {0}", pResp.ServerId);
                _output.WriteLine("Total paid: {0}", pResp.TotalPaid);
                _output.WriteLine("ServiceDeliveryToken.issued: {0}", pResp.ServiceDeliveryToken.Issued);
                _output.WriteLine("ServiceDeliveryToken.expiry: {0}", pResp.ServiceDeliveryToken.Expiry);
                _output.WriteLine("ServiceDeliveryToken.key: {0}", pResp.ServiceDeliveryToken.Key);
                _output.WriteLine("ServiceDeliveryToken.signature: [{0}]", ToReadableString(pResp.ServiceDeliveryToken.Signature));
                _output.WriteLine("ServiceDeliveryToken.refundOnExpiry: {0}", pResp.ServiceDeliveryToken.RefundOnExpiry);

                BeginServiceDelivery(service, serviceId, pResp.ServiceDeliveryToken, 1);
            }
            else
            {
                _error.WriteLine("Result of MakePayment call is null");
            }

            return(pResp);
        }