11using System ;
22using System . Collections . Generic ;
3+ using System . Web ;
34using System . Web . Routing ;
45using SmartStore . Core . Configuration ;
56using SmartStore . Core . Domain . Orders ;
@@ -22,6 +23,7 @@ protected PayPalRestApiProviderBase()
2223 }
2324
2425 public ILogger Logger { get ; set ; }
26+ public HttpContextBase HttpContext { get ; set ; }
2527 public ICommonServices Services { get ; set ; }
2628 public IOrderService OrderService { get ; set ; }
2729 public IOrderTotalCalculationService OrderTotalCalculationService { get ; set ; }
@@ -32,6 +34,11 @@ protected string GetControllerName()
3234 return GetControllerType ( ) . Name . EmptyNull ( ) . Replace ( "Controller" , "" ) ;
3335 }
3436
37+ public static string CheckoutCompletedKey
38+ {
39+ get { return "PayPalCheckoutCompleted" ; }
40+ }
41+
3542 public override bool SupportCapture
3643 {
3744 get { return true ; }
@@ -67,7 +74,93 @@ public override decimal GetAdditionalHandlingFee(IList<OrganizedShoppingCartItem
6774 return result ;
6875 }
6976
70- public override CapturePaymentResult Capture ( CapturePaymentRequest capturePaymentRequest )
77+ public override ProcessPaymentResult ProcessPayment ( ProcessPaymentRequest processPaymentRequest )
78+ {
79+ var result = new ProcessPaymentResult
80+ {
81+ NewPaymentStatus = PaymentStatus . Pending
82+ } ;
83+
84+ HttpContext . Session . SafeRemove ( CheckoutCompletedKey ) ;
85+
86+ var settings = Services . Settings . LoadSetting < TSetting > ( processPaymentRequest . StoreId ) ;
87+ var session = HttpContext . GetPayPalSessionData ( ) ;
88+
89+ processPaymentRequest . OrderGuid = session . OrderGuid ;
90+
91+ var apiResult = PayPalService . ExecutePayment ( settings , session ) ;
92+
93+ if ( apiResult . Success && apiResult . Json != null )
94+ {
95+ var state = ( string ) apiResult . Json . state ;
96+
97+ if ( ! state . IsCaseInsensitiveEqual ( "failed" ) )
98+ {
99+ // intent: "sale" for immediate payment, "authorize" for pre-authorized payments and "order" for an order.
100+ // info required cause API has different endpoints for different intents.
101+ result . AuthorizationTransactionCode = ( string ) apiResult . Json . intent ;
102+
103+ var sale = apiResult . Json . transactions [ 0 ] . related_resources [ 0 ] . sale ;
104+
105+ if ( sale != null )
106+ {
107+ state = ( string ) sale . state ;
108+
109+ result . AuthorizationTransactionResult = state ;
110+ result . AuthorizationTransactionId = ( string ) sale . id ;
111+
112+ result . NewPaymentStatus = PaymentStatus . Authorized ;
113+
114+ if ( state . IsCaseInsensitiveEqual ( "completed" ) || state . IsCaseInsensitiveEqual ( "processed" ) )
115+ {
116+ result . CaptureTransactionResult = state ;
117+ result . CaptureTransactionId = ( string ) sale . id ;
118+
119+ result . NewPaymentStatus = PaymentStatus . Paid ;
120+ }
121+ else if ( state . IsCaseInsensitiveEqual ( "pending" ) )
122+ {
123+ var reasonCode = ( string ) sale . reason_code ;
124+ if ( reasonCode . IsCaseInsensitiveEqual ( "ECHECK" ) )
125+ {
126+ result . NewPaymentStatus = PaymentStatus . Pending ;
127+ }
128+ }
129+
130+ session . PaymentInstruction = PayPalService . ParsePaymentInstruction ( apiResult . Json . payment_instruction ) as PayPalPaymentInstruction ;
131+ }
132+ }
133+ else
134+ {
135+ var failureReason = ( string ) apiResult . Json . failure_reason ;
136+
137+ result . Errors . Add ( T ( "Plugins.SmartStore.PayPal.PaymentExecuteFailed" ) . Text . Grow ( failureReason , " " ) ) ;
138+ }
139+ }
140+ else
141+ {
142+ result . Errors . Add ( apiResult . ErrorMessage ) ;
143+ }
144+
145+ return result ;
146+ }
147+
148+ public override void PostProcessPayment ( PostProcessPaymentRequest postProcessPaymentRequest )
149+ {
150+ if ( postProcessPaymentRequest . Order . PaymentStatus == PaymentStatus . Paid )
151+ return ;
152+
153+ var instruction = PayPalService . CreatePaymentInstruction ( HttpContext . GetPayPalSessionData ( ) . PaymentInstruction ) ;
154+
155+ if ( instruction . HasValue ( ) )
156+ {
157+ HttpContext . Session [ CheckoutCompletedKey ] = instruction ;
158+
159+ OrderService . AddOrderNote ( postProcessPaymentRequest . Order , instruction , true ) ;
160+ }
161+ }
162+
163+ public override CapturePaymentResult Capture ( CapturePaymentRequest capturePaymentRequest )
71164 {
72165 var result = new CapturePaymentResult
73166 {
0 commit comments