Last active
August 29, 2015 13:56
-
-
Save james-dibble/8801458 to your computer and use it in GitHub Desktop.
PayPal REST API SDK Payment Serice example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Application.Services | |
| { | |
| using System.Collections.Generic; | |
| using System.Configuration; | |
| using System.Globalization; | |
| using System.Linq; | |
| using System.Web; | |
| using System.Web.Mvc; | |
| using global::PayPal; | |
| using global::PayPal.Api.Payments; | |
| using Application.Model; | |
| public class PaymentService : IPaymentService | |
| { | |
| private OAuthTokenCredential _authToken; | |
| private APIContext _api; | |
| public Payment CreatePayment(Order order, string cancelUrl, string returnUrl) | |
| { | |
| var amount = this.CreateAmount(order); | |
| var payment = new Payment | |
| { | |
| transactions = new List<Transaction> | |
| { | |
| new Transaction | |
| { | |
| amount = amount, | |
| description = "Purchase From Application" | |
| } | |
| }, | |
| intent = "sale", | |
| payer = new Payer { payment_method = "paypal" }, | |
| redirect_urls = new RedirectUrls | |
| { | |
| cancel_url = cancelUrl, | |
| return_url = returnUrl | |
| } | |
| }; | |
| payment = payment.Create(this.Api); | |
| return payment; | |
| } | |
| public Payment ConfirmPayment(string token, string payerId, Order order) | |
| { | |
| var paymentExecution = new PaymentExecution | |
| { | |
| payer_id = payerId | |
| }; | |
| var payment = new Payment { id = token }; | |
| return payment.Execute(this.Api, paymentExecution); | |
| } | |
| private Amount CreateAmount(Order order) | |
| { | |
| var details = new Details | |
| { | |
| subtotal = order.SubTotal.ToString("0.00", CultureInfo.CurrentCulture), | |
| shipping = order.Shipping.ToString("0.00", CultureInfo.CurrentCulture), | |
| tax = order.Tax.ToString("0.00", CultureInfo.CurrentCulture), | |
| fee = order.Fees.ToString("0.00", CultureInfo.CurrentCulture) | |
| }; | |
| var total = order.SubTotal + order.Shipping + order.Tax + order.Fees; | |
| var amount = new Amount | |
| { | |
| currency = "USD", | |
| details = details, | |
| total = total.ToString("0.00", CultureInfo.CurrentCulture) | |
| }; | |
| return amount; | |
| } | |
| private OAuthTokenCredential ApiAccessToken | |
| { | |
| get | |
| { | |
| if (this._authToken != null) | |
| { | |
| return this._authToken; | |
| } | |
| var clientId = ConfigurationManager.AppSettings["clientId"]; | |
| var secretToken = ConfigurationManager.AppSettings["secretToken"]; | |
| var config = new Dictionary<string, string> { { "mode", "sandbox" } }; | |
| this._authToken = new OAuthTokenCredential(clientId, secretToken, config); | |
| return this._authToken; | |
| } | |
| } | |
| private APIContext Api | |
| { | |
| get | |
| { | |
| return this._api ?? (this._api = new APIContext(this.ApiAccessToken.GetAccessToken())); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment