/// <summary> /// Handle Refund API call /// </summary> /// <param name="context"></param> private void Refund(HttpContext context) { NameValueCollection parameters = context.Request.Params; RefundRequest req = new RefundRequest(new RequestEnvelope("en_US")); // Set optional parameters if(parameters["receiverEmail"].Length > 0) { string[] amt = context.Request.Form.GetValues("receiverAmount"); string[] receiverEmail = context.Request.Form.GetValues("receiverEmail"); string[] phoneCountry = context.Request.Form.GetValues("phoneCountry"); string[] phoneNumber = context.Request.Form.GetValues("phoneNumber"); string[] phoneExtn = context.Request.Form.GetValues("phoneExtn"); string[] primaryReceiver = context.Request.Form.GetValues("primaryReceiver"); string[] invoiceId = context.Request.Form.GetValues("invoiceId"); string[] paymentType = context.Request.Form.GetValues("paymentType"); string[] paymentSubType = context.Request.Form.GetValues("paymentSubType"); List<Receiver> receivers = new List<Receiver>(); for(int i=0; i<amt.Length; i++) { Receiver r = new Receiver(Decimal.Parse(amt[i])); r.email = receiverEmail[i]; r.primary = Boolean.Parse(primaryReceiver[i]); if(invoiceId[i] != "") { r.invoiceId = invoiceId[i]; } if(paymentType[i] != "") { r.paymentType = paymentType[i]; } if(paymentSubType[i] != "") { r.paymentSubType = paymentSubType[i]; } if(phoneCountry[i] != "" && phoneNumber[i] != "") { r.phone = new PhoneNumberType(phoneCountry[i], phoneNumber[i]); if(phoneExtn[i] != "") { r.phone.extension = phoneExtn[i]; } } receivers.Add(r); } req.receiverList = new ReceiverList(receivers); } if(parameters["currencyCode"] != "") { req.currencyCode = parameters["currencyCode"]; } if(parameters["payKey"] != "") { req.payKey = parameters["payKey"]; } if(parameters["transactionId"] != "") { req.transactionId = parameters["transactionId"]; } if(parameters["trackingId"] != "") { req.trackingId = parameters["trackingId"]; } // All set. Fire the request AdaptivePaymentsService service = new AdaptivePaymentsService(); RefundResponse resp = null; try { resp = service.Refund(req); } catch (System.Exception e) { context.Response.Write(e.Message); return; } // Display response values. Dictionary<string, string> keyResponseParams = new Dictionary<string, string>(); string redirectUrl = null; if (!(resp.responseEnvelope.ack == AckCode.FAILURE) && !(resp.responseEnvelope.ack == AckCode.FAILUREWITHWARNING)) { keyResponseParams.Add("Currency code", resp.currencyCode); int idx = 1; foreach (RefundInfo refund in resp.refundInfoList.refundInfo) { keyResponseParams.Add("Refund receiver " + idx, refund.receiver.email); keyResponseParams.Add("Refund amount " + idx, refund.receiver.amount.ToString()); keyResponseParams.Add("Refund status " + idx, refund.refundStatus); } } displayResponse(context, "Refund", keyResponseParams, service.getLastRequest(), service.getLastResponse(), resp.error, redirectUrl); }
// # Refund API Operation // Use the Refund API operation to refund all or part of a payment. You can specify the amount of the refund and identify the accounts to receive the refund by the payment key or tracking ID, and optionally, by transaction ID or the receivers of the original payment. public RefundResponse RefundAPIOperation() { // Create the RefundResponse object RefundResponse responseRefund = new RefundResponse(); try { // # RefundRequest // The code for the language in which errors are returned RequestEnvelope envelopeRequest = new RequestEnvelope(); envelopeRequest.errorLanguage = "en_US"; // RefundRequest which takes, // `Request Envelope` - Information common to each API operation, such // as the language in which an error message is returned. RefundRequest requestRefund = new RefundRequest(envelopeRequest); // You must specify either, // // * `Pay Key` - The pay key that identifies the payment for which you // want to retrieve details. This is the pay key returned in the // PayResponse message. // * `Transaction ID` - The PayPal transaction ID associated with the // payment. The IPN message associated with the payment contains the // transaction ID. // `requestRefund.transactionId = transactionId` // * `Tracking ID` - The tracking ID that was specified for this payment // in the PayRequest message. // `requestRefund.trackingId = trackingId` requestRefund.payKey = "AP-86H50830VE600922B"; // Create the service wrapper object to make the API call AdaptivePaymentsService service = new AdaptivePaymentsService(); // # API call // Invoke the Refund method in service wrapper object responseRefund = service.Refund(requestRefund); if (responseRefund != null) { // Response envelope acknowledgement string acknowledgement = "Refund API Operation - "; acknowledgement += responseRefund.responseEnvelope.ack.ToString(); logger.Info(acknowledgement + "\n"); Console.WriteLine(acknowledgement + "\n"); // # Success values if (responseRefund.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS")) { // List of refunds associated with the payment. IEnumerator <RefundInfo> iterator = responseRefund.refundInfoList.refundInfo.GetEnumerator(); while (iterator.MoveNext()) { // Represents the refund attempt made to a receiver of a // PayRequest. RefundInfo refundInfo = iterator.Current; // Status of the refund. It is one of the following values: // // * REFUNDED - Refund successfully completed // * REFUNDED_PENDING - Refund awaiting transfer of funds; for // example, a refund paid by eCheck. // * NOT_PAID - Payment was never made; therefore, it cannot // be refunded. // * ALREADY_REVERSED_OR_REFUNDED - Request rejected because // the refund was already made, or the payment was reversed // prior to this request. // * NO_API_ACCESS_TO_RECEIVER - Request cannot be completed // because you do not have third-party access from the // receiver to make the refund. // * REFUND_NOT_ALLOWED - Refund is not allowed. // * INSUFFICIENT_BALANCE - Request rejected because the // receiver from which the refund is to be paid does not // have sufficient funds or the funding source cannot be // used to make a refund. // * AMOUNT_EXCEEDS_REFUNDABLE - Request rejected because you // attempted to refund more than the remaining amount of the // payment; call the PaymentDetails API operation to // determine the amount already refunded. // * PREVIOUS_REFUND_PENDING - Request rejected because a // refund is currently pending for this part of the payment // * NOT_PROCESSED - Request rejected because it cannot be // processed at this time // * REFUND_ERROR - Request rejected because of an internal // error // * PREVIOUS_REFUND_ERROR - Request rejected because another // part of this refund caused an internal error. logger.Info("Refund Status : " + refundInfo.refundStatus + "\n"); Console.WriteLine("Refund Status : " + refundInfo.refundStatus + "\n"); } } // # Error Values else { List <ErrorData> errorMessages = responseRefund.error; foreach (ErrorData error in errorMessages) { logger.Debug("API Error Message : " + error.message); Console.WriteLine("API Error Message : " + error.message + "\n"); } } } } // # Exception log catch (System.Exception ex) { // Log the exception message logger.Debug("Error Message : " + ex.Message); Console.WriteLine("Error Message : " + ex.Message); } return(responseRefund); }
/// <summary> /// Refunds the payment. /// </summary> /// <param name="p">The parameters.</param> /// <exception cref="UserException">If refund was not suceessful, it throws an exception for the user</exception> public void RefundPayment(RefundParameters p) { // error language : (Required) RFC 3066 language in which error messages are returned; by default it is en_US, which is the only language currently supported. RefundRequest request = new RefundRequest(new RequestEnvelope("en_US")); request.payKey = p.payKey; request.currencyCode = p.currencyCode; List <Receiver> receivers = new List <Receiver>(); request.receiverList = new ReceiverList(receivers); if (p.receiver1amount != 0) { AddReceiver(p.receiver1amount, p.receiver1email, request.receiverList); } if (p.receiver2amount != 0) { AddReceiver(p.receiver2amount, p.receiver2email, request.receiverList); } AdaptivePaymentsService service = null; RefundResponse response = null; // Configuration map containing signature credentials and other required configuration. // For a full list of configuration parameters refer in wiki page // (https://github.com/paypal/sdk-core-dotnet/wiki/SDK-Configuration-Parameters) Dictionary <string, string> configurationMap = FWUtils.ConfigUtils.GetAppSettings().Paypal.GetAcctAndConfig(); // Creating service wrapper object to make an API call and loading // configuration map for your credentials and endpoint service = new AdaptivePaymentsService(configurationMap); response = service.Refund(request); string ack = response.responseEnvelope.ack.ToString().Trim().ToUpper(); // if no error happened if (!ack.Equals(AckCode.FAILURE.ToString()) && !ack.Equals(AckCode.FAILUREWITHWARNING.ToString())) { StringBuilder sbErrors = new StringBuilder(); bool hasError = false; foreach (RefundInfo refund in response.refundInfoList.refundInfo) { RefundStatusSEnum refundStatus = new RefundStatusSEnum(refund.refundStatus); if (refundStatus != RefundStatusSEnum.REFUNDED && refundStatus != RefundStatusSEnum.REFUNDED_PENDING) { sbErrors.Append(refundStatus.GetUserFriendlyMessage()); hasError = true; if (refundStatus == RefundStatusSEnum.NO_API_ACCESS_TO_RECEIVER) { EmailPersonRefundError(refund.receiver.email, refundStatus); } } } if (hasError) { throw new UserException(sbErrors.ToString()); } } else { throw new UserException(GetPayPalErrorString(response.error)); } }