Beispiel #1
0
        public static MicrosoftPayProtocolResponse ParseResponse(string jsonDetails)
        {
            string token = GetTokenFromResponse(jsonDetails);

            string[] tokenParts = token.Split('.');
            if (tokenParts.Length < 3)
            {
                throw new MicrosoftPayParseResponseException("Incorrect token format.");
            }

            string tokenHeadersJson = DecodeBase64UrlString(tokenParts[0]);
            string tokenBody        = DecodeBase64UrlString(tokenParts[1]);

            byte[] tokenSignature = DecodeBase64Url(tokenParts[2]);

            MicrosoftPayProtocolResponse result = ParseHeaders(tokenHeadersJson);

            result.Token = tokenBody;

            return(result);
        }
Beispiel #2
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();
        }
Beispiel #3
0
        private static MicrosoftPayProtocolResponse ParsePaymentTokenHeaders(JsonReader jsonReader)
        {
            MicrosoftPayProtocolResponse result       = new MicrosoftPayProtocolResponse();
            MicrosoftPayErrorInfo        errorInfo    = new MicrosoftPayErrorInfo();
            MicrosoftPayStandardInfo     standardInfo = new MicrosoftPayStandardInfo();

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

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

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

                    case "Expiry":
                        result.Expiry = (DateTime)jsonReader.ReadAsDateTime();
                        break;

                    case "TimeStamp":
                        result.TimeStamp = (DateTime)jsonReader.ReadAsDateTime();
                        break;

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

                    case "ErrorCode":
                        errorInfo.ErrorCode = jsonReader.ReadAsString();
                        break;

                    case "ErrorText":
                        errorInfo.ErrorText = jsonReader.ReadAsString();
                        break;

                    case "ErrorSource":
                        errorInfo.ErrorSource = jsonReader.ReadAsString();
                        break;

                    case "EPK":
                        standardInfo.EPK = jsonReader.ReadAsString();
                        break;

                    case "KeyId":
                        standardInfo.KeyId = jsonReader.ReadAsString();
                        break;

                    case "Nonce":
                        standardInfo.Nonce = jsonReader.ReadAsString();
                        break;

                    case "Tag":
                        standardInfo.Tag = jsonReader.ReadAsString();
                        break;

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

                case JsonToken.EndObject:
                    switch (result.Format)
                    {
                    case "Error":
                        result.ErrorInfo = errorInfo;
                        break;

                    case "Standard":
                        result.StandardInfo = standardInfo;
                        break;
                    }

                    return(result);

                case JsonToken.Comment:
                    // Do nothing
                    break;

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

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