11using System . Collections . Generic ;
22using System . Linq ;
3+ using System . Web ;
34using System . Web . Mvc ;
45using SmartStore . PayPal . Models ;
56using SmartStore . PayPal . Services ;
@@ -14,9 +15,11 @@ namespace SmartStore.PayPal.Controllers
1415{
1516 public class PayPalPlusController : PayPalControllerBase < PayPalPlusPaymentSettings >
1617 {
18+ private readonly HttpContextBase _httpContext ;
1719 private readonly IPayPalService _payPalService ;
1820
1921 public PayPalPlusController (
22+ HttpContextBase httpContext ,
2023 IPaymentService paymentService ,
2124 IOrderService orderService ,
2225 IOrderProcessingService orderProcessingService ,
@@ -26,6 +29,7 @@ public PayPalPlusController(
2629 orderService ,
2730 orderProcessingService )
2831 {
32+ _httpContext = httpContext ;
2933 _payPalService = payPalService ;
3034 }
3135
@@ -105,10 +109,12 @@ public ActionResult UpsertExperienceProfile(string profileId)
105109 var settings = Services . Settings . LoadSetting < PayPalPlusPaymentSettings > ( storeScope ) ;
106110
107111 var store = Services . StoreService . GetStoreById ( storeScope == 0 ? Services . StoreContext . CurrentStore . Id : storeScope ) ;
112+ var session = new PayPalSessionData ( ) ;
108113
109- var result = _payPalService . UpsertCheckoutExperience ( settings , store , profileId ) ;
110- if ( result != null )
114+ var result = _payPalService . EnsureAccessToken ( session , settings ) ;
115+ if ( result . Success )
111116 {
117+ result = _payPalService . UpsertCheckoutExperience ( settings , session , store , profileId ) ;
112118 if ( result . Success && result . Id . HasValue ( ) )
113119 {
114120 settings . ExperienceProfileId = result . Id ;
@@ -120,6 +126,10 @@ public ActionResult UpsertExperienceProfile(string profileId)
120126 NotifyError ( result . ErrorMessage ) ;
121127 }
122128 }
129+ else
130+ {
131+ NotifyError ( result . ErrorMessage ) ;
132+ }
123133
124134 return RedirectToAction ( "ConfigureProvider" , "Plugin" , new { area = "admin" , systemName = PayPalPlusProvider . SystemName } ) ;
125135 }
@@ -131,15 +141,67 @@ public ActionResult PaymentInfo()
131141
132142 public ActionResult PaymentWall ( )
133143 {
144+ var store = Services . StoreContext . CurrentStore ;
145+ var customer = Services . WorkContext . CurrentCustomer ;
146+ var settings = Services . Settings . LoadSetting < PayPalPlusPaymentSettings > ( store . Id ) ;
147+
134148 var model = new PayPalPlusCheckoutModel ( ) ;
149+ var state = _httpContext . GetCheckoutState ( ) ;
150+
151+ if ( ! state . CustomProperties . ContainsKey ( PayPalPlusProvider . SystemName ) )
152+ state . CustomProperties . Add ( PayPalPlusProvider . SystemName , new PayPalSessionData ( ) ) ;
153+
154+ var session = state . CustomProperties [ PayPalPlusProvider . SystemName ] as PayPalSessionData ;
155+
156+ model . UseSandbox = settings . UseSandbox ;
157+ model . HasPaymentFee = ( settings . AdditionalFee > decimal . Zero ) ;
158+ model . LanguageCulture = ( Services . WorkContext . WorkingLanguage . LanguageCulture ?? "de_DE" ) . Replace ( "-" , "_" ) ;
159+
160+ if ( customer . BillingAddress != null && customer . BillingAddress . Country != null )
161+ {
162+ model . BillingAddressCountryCode = customer . BillingAddress . Country . TwoLetterIsoCode ;
163+ }
164+
165+ var protocol = ( store . SslEnabled ? "https" : "http" ) ;
166+ var returnUrl = Url . Action ( "CheckoutReturn" , "PayPalPlus" , new { area = Plugin . SystemName } , protocol ) ;
167+ var cancelUrl = Url . Action ( "CheckoutCancel" , "PayPalPlus" , new { area = Plugin . SystemName } , protocol ) ;
168+
169+
170+ var result = _payPalService . EnsureAccessToken ( session , settings ) ;
171+ if ( result . Success )
172+ {
173+ result = _payPalService . UpsertPayment ( settings , session , null , returnUrl , cancelUrl ) ;
174+ if ( result . Success && result . Json != null )
175+ {
176+ foreach ( var link in result . Json . links )
177+ {
178+ if ( ( ( string ) link . rel ) . IsCaseInsensitiveEqual ( "approval_url" ) )
179+ {
180+ model . ApprovalUrl = link . href ;
181+ break ;
182+ }
183+ }
184+ }
185+ else if ( result . ErrorMessage . HasValue ( ) )
186+ {
187+ ModelState . AddModelError ( "" , result . ErrorMessage ) ;
188+ }
189+ }
190+ else if ( result . ErrorMessage . HasValue ( ) )
191+ {
192+ ModelState . AddModelError ( "" , result . ErrorMessage ) ;
193+ }
135194
136195 return View ( model ) ;
137196 }
138197
139- [ HttpPost ]
140- public ActionResult PaymentWall ( FormCollection form )
198+ public ActionResult CheckoutReturn ( )
141199 {
200+ return RedirectToAction ( "Confirm" , "Checkout" , new { area = "" } ) ;
201+ }
142202
203+ public ActionResult CheckoutCancel ( )
204+ {
143205 return RedirectToAction ( "Confirm" , "Checkout" , new { area = "" } ) ;
144206 }
145207 }
0 commit comments