Beispiel #1
0
        private async void UpdateUserStatus(PaymentRequestSubmitResult result)
        {
            if (result.Status == PaymentRequestStatus.Succeeded)
            {
                // Simulate payment processing wait
                await Task.Delay(TimeSpan.FromSeconds(2));

                // Report that we successfully charged their card using on the payment token we were given
                await result.Response.CompleteAsync(PaymentRequestCompletionStatus.Succeeded);
            }
            else
            {
                await new MessageDialog("Payment failed, status: " + result.Status).ShowAsync();
            }
        }
Beispiel #2
0
        private async void UpdateUserStatus(PaymentRequestSubmitResult res)
        {
            if (res.Status == PaymentRequestStatus.Succeeded)
            {
                // Sleep just to pretend like we're processing the payment
                Thread.Sleep(2000);

                // Report that we successfully charged their card using on the payment token we were given
                await res.Response.CompleteAsync(PaymentRequestCompletionStatus.Succeeded);
            }
            else
            {
                MessageBox.Show("Payment failed, status: " + res.Status);
            }
        }
        private async Task UpdateUI(PaymentRequestSubmitResult result)
        {
            if (result.Status == PaymentRequestStatus.Succeeded)
            {
                // If payment request is successful, Microsoft returns a json token
                //   containing Microsoft Wallet data to submit to merchant's payment processor
                Debug.WriteLine("Method ID: " + result.Response.PaymentToken.PaymentMethodId);
                Debug.WriteLine("Payment JSON Token: " + (result.Response.PaymentToken.JsonDetails ?? ""));

                // Simulate payment processing by merchant's payment processor
                // Note: actual time will vary quite a bit by payment processor
                await Task.Delay(TimeSpan.FromSeconds(2));

                // Report that we successfully charged their card using the payment token we were given
                await result.Response.CompleteAsync(PaymentRequestCompletionStatus.Succeeded);

                await new MessageDialog("Payment succeeded!").ShowAsync();
                Save_Click(this, null);
            }
            else
            {
                await new MessageDialog("Payment failed, status: " + result.Status).ShowAsync();
            }
        }
Beispiel #4
0
        public async Task PurchaseProductAsync(Product product)
        {
            if (product != null)
            {
                PaymentMediator mediator = new PaymentMediator();
                ((IInitializeWithWindow)(object)mediator).Initialize(this.handle);

                PaymentDetails paymentDetails = new PaymentDetails(
                    new PaymentItem(
                        "Total",
                        new PaymentCurrencyAmount(
                            product.Price.ToString(CultureInfo.CurrentCulture),
                            "GBP")),
                    new List <PaymentItem>()
                {
                    new PaymentItem(
                        product.Name,
                        new PaymentCurrencyAmount(
                            product.Price.ToString(
                                CultureInfo.CurrentCulture),
                            "GBP"))
                })
                {
                    ShippingOptions =
                        GetShippingOptions()
                };
                List <PaymentMethodData> methods =
                    new List <PaymentMethodData> {
                    new PaymentMethodData(new List <string>()
                    {
                        "basic-card"
                    })
                };
                PaymentMerchantInfo merchantInfo = new PaymentMerchantInfo(new Uri("http://www.myenterprise.co.uk"));
                PaymentOptions      options      = new PaymentOptions()
                {
                    RequestShipping         = true,
                    ShippingType            = PaymentShippingType.Delivery,
                    RequestPayerEmail       = PaymentOptionPresence.Optional,
                    RequestPayerName        = PaymentOptionPresence.Required,
                    RequestPayerPhoneNumber = PaymentOptionPresence.None
                };

                PaymentRequest paymentRequest = new PaymentRequest(paymentDetails, methods, merchantInfo, options);

                PaymentRequestSubmitResult res = await mediator.SubmitPaymentRequestAsync(paymentRequest);

                if (res.Status == PaymentRequestStatus.Succeeded)
                {
                    await res.Response.CompleteAsync(PaymentRequestCompletionStatus.Succeeded);

                    NotificationService.Current.Show(
                        "Payment Successful",
                        $"The payment for '{product}' was successful!",
                        null,
                        null);
                }
                else
                {
                    NotificationService.Current.Show(
                        "Payment Failed",
                        $"A problem occurred while paying for '{product}'!",
                        null,
                        null);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Starts a checkout operation and runs it to completion.
        /// </summary>
        public static async Task CheckoutAsync(ShoppingCart shoppingCart)
        {
            Uri merchantUri = new Uri("http://www.contoso.com");
            PaymentMerchantInfo merchantInfo = new PaymentMerchantInfo(merchantUri);

            PaymentOptions options = new PaymentOptions();

            options.RequestShipping         = true;
            options.ShippingType            = PaymentShippingType.Delivery;
            options.RequestPayerEmail       = PaymentOptionPresence.Optional;
            options.RequestPayerName        = PaymentOptionPresence.Required;
            options.RequestPayerPhoneNumber = PaymentOptionPresence.Optional;

            PaymentRequest request = new PaymentRequest(
                CreatePaymentDetails(shoppingCart),
                SupportedPaymentMethods,
                merchantInfo,
                options);

            IAsyncOperation <PaymentRequestSubmitResult> submitOperation = null;

            Action abortSubmitFunc = () =>
            {
                submitOperation.Cancel();
            };

            PaymentRequestChangedHandler changedHandler = (sender, args) =>
            {
                PaymentRequestChangedEventHandler(abortSubmitFunc, args, shoppingCart);
            };

            PaymentMediator mediator = new PaymentMediator();

            submitOperation = mediator.SubmitPaymentRequestAsync(request, changedHandler);

            PaymentRequestSubmitResult result = await submitOperation;

            if (result.Status != PaymentRequestStatus.Succeeded)
            {
                string message = result.Status == PaymentRequestStatus.Canceled ?
                                 "The payment was canceled." :
                                 "The payment failed.";

                MessageDialog failedMessageDialog = new MessageDialog(message);
                await failedMessageDialog.ShowAsync();

                return;
            }

            PaymentResponse response = result.Response;
            PaymentToken    token    = response.PaymentToken;

            bool paymentSucceeded = false;

            switch (token.PaymentMethodId)
            {
            case BasicCardPaymentProtocol.PaymentMethodId:
                BasicCardResponse basicCardResponse = BasicCardPaymentProtocol.ParseResponse(token.JsonDetails);

                //
                // TODO: Use data inside 'basicCardResponse' to collect payment.
                //

                paymentSucceeded = true;
                break;

            case MicrosoftPayProtocol.PaymentMethodId:
                MicrosoftPayProtocolResponse microsoftPayResponse = MicrosoftPayProtocol.ParseResponse(token.JsonDetails);

                switch (microsoftPayResponse.Format)
                {
                case "Stripe":
                    //
                    // TODO: Use data inside 'microsoftPayResponse.Token' to collect payment.
                    //

                    paymentSucceeded = true;
                    break;

                case "Error":
                    paymentSucceeded = false;
                    break;

                case "Invalid":
                    throw new Exception("The payment request was invalid.");

                default:
                    throw new Exception("Unsupported payment gateway.");
                }
                break;

            default:
                await result.Response.CompleteAsync(PaymentRequestCompletionStatus.Unknown);

                throw new Exception("Unsupported payment method ID.");
            }

            await result.Response.CompleteAsync(paymentSucceeded?PaymentRequestCompletionStatus.Succeeded : PaymentRequestCompletionStatus.Failed);

            string completionMessage = paymentSucceeded ?
                                       "The payment succeeded." :
                                       "Unable to process the payment.";

            MessageDialog messageDialog = new MessageDialog(completionMessage);
            await messageDialog.ShowAsync();
        }