示例#1
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();
        }
示例#2
0
        private static BasicCardResponse ParseBasicCardResponse(JsonReader jsonReader)
        {
            BasicCardResponse result = new BasicCardResponse();

            while (jsonReader.Read())
            {
                switch (jsonReader.TokenType)
                {
                case JsonToken.PropertyName:
                    string propertyName = (string)jsonReader.Value;
                    switch (propertyName)
                    {
                    case "cardholderName":
                        result.CardholderName = jsonReader.ReadAsString();
                        break;

                    case "cardNumber":
                        result.CardNumber = jsonReader.ReadAsString();
                        break;

                    case "expiryMonth":
                        result.ExpiryMonth = jsonReader.ReadAsString();
                        break;

                    case "expiryYear":
                        result.ExpiryYear = jsonReader.ReadAsString();
                        break;

                    case "cardSecurityCode":
                        result.CardSecurityCode = jsonReader.ReadAsString();
                        break;

                    case "billingAddress":
                        jsonReader.Read();

                        if (jsonReader.TokenType != JsonToken.StartObject)
                        {
                            throw new BasicCardParseResponseException("Property 'billingAddress' must have value that is an object type.");
                        }

                        result.BillingAddress = ParsePaymentAddress(jsonReader);
                        break;

                    default:
                        // Ignore extra data.
                        jsonReader.Read(); // goto value
                        jsonReader.Skip(); // skip value
                        break;
                    }
                    break;

                case JsonToken.EndObject:
                    return(result);

                case JsonToken.Comment:
                    // Do nothing
                    break;

                default:
                    throw new BasicCardParseResponseException("Incorrect JSON format.");
                }
            }

            throw new BasicCardParseResponseException("Unexpected end of file.");
        }