44
55use EE_Error ;
66use EE_Line_Item ;
7+ use EE_Payment ;
8+ use EEM_Payment ;
79use EE_Transaction ;
810use EventEspresso \core \domain \services \capabilities \FeatureFlag ;
911use EventEspresso \core \domain \services \capabilities \FeatureFlags ;
1012use EventEspresso \core \domain \services \validation \email \strategies \Basic ;
1113use EventEspresso \core \services \loaders \LoaderFactory ;
14+ use EventEspresso \core \services \payment_methods \gateways \GatewayDataFormatter ;
1215use EventEspresso \core \services \request \sanitizers \RequestSanitizer ;
1316use EventEspresso \PaymentMethods \PayPalCommerce \api \PayPalApi ;
1417use EventEspresso \PaymentMethods \PayPalCommerce \domain \Domain ;
@@ -74,6 +77,9 @@ class CreateOrder extends OrdersApi
7477
7578 private FeatureFlags $ feature ;
7679
80+ private GatewayDataFormatter $ gateway_data_formatter ;
81+
82+ private EE_Payment $ payment ;
7783
7884 /**
7985 * CreateOrder constructor.
@@ -83,13 +89,15 @@ class CreateOrder extends OrdersApi
8389 * @param array $billing_info
8490 * @param FeatureFlags $feature
8591 */
86- public function __construct (PayPalApi $ api , EE_Transaction $ transaction , array $ billing_info , FeatureFlags $ feature )
92+ public function __construct (PayPalApi $ api , EE_Transaction $ transaction , array $ billing_info , FeatureFlags $ feature, GatewayDataFormatter $ gateway_data_formatter )
8793 {
8894 parent ::__construct ($ api );
8995 $ this ->transaction = $ transaction ;
9096 $ this ->feature = $ feature ;
9197 $ this ->currency_code = CurrencyManager::currencyCode ();
9298 $ this ->sanitizeRequestParameters ($ billing_info );
99+ $ this ->gateway_data_formatter = $ gateway_data_formatter ;
100+ $ this ->setPaymentPlaceholder ();
93101 }
94102
95103
@@ -146,10 +154,7 @@ protected function getParameters(): array
146154 $ registrant = $ this ->transaction ->primary_registration ();
147155 $ attendee = $ registrant ->attendee ();
148156 $ event = $ registrant ->event ();
149- $ description = $ event ->name () ?: sprintf (
150- esc_html__ ('Tickets for an event at %1$s ' , 'event_espresso ' ),
151- get_bloginfo ('name ' )
152- );
157+ $ description = $ this ->gateway_data_formatter ->formatOrderDescription ($ this ->payment );
153158 $ parameters = [
154159 'intent ' => 'CAPTURE ' ,
155160 'purchase_units ' => [
@@ -164,16 +169,24 @@ protected function getParameters(): array
164169 ],
165170 ],
166171 ],
167- 'application_context ' => [
168- 'shipping_preference ' => 'NO_SHIPPING ' ,
169- 'user_action ' => 'PAY_NOW ' ,
170- ],
171- 'payer ' => [
172- 'email_address ' => $ attendee ->email (),
173- 'name ' => [
174- 'given_name ' => $ attendee ->fname (),
175- 'surname ' => $ attendee ->lname (),
176-
172+ 'payment_source ' => [
173+ 'paypal ' => [
174+ 'experience_context ' => [
175+ 'user_action ' => 'PAY_NOW '
176+ ],
177+ 'email_address ' => $ attendee ->email (),
178+ 'name ' => [
179+ 'given_name ' => $ attendee ->fname (),
180+ 'surname ' => $ attendee ->lname (),
181+ ],
182+ 'address ' => [
183+ 'address_line_1 ' => $ attendee ->address (),
184+ 'address_line_2 ' => $ attendee ->address2 (),
185+ 'admin_area_2 ' => $ attendee ->city (),
186+ 'admin_area_1 ' => $ attendee ->state_abbrev (),
187+ 'postal_code ' => $ attendee ->zip (),
188+ 'country_code ' => $ attendee ->country_ID (),
189+ ],
177190 ],
178191 ],
179192 ];
@@ -227,11 +240,10 @@ protected function getLineItems(): array
227240 && $ line_item ->quantity () > 0
228241 ) {
229242 $ item_money = CurrencyManager::normalizeValue ($ line_item ->unit_price ());
230- $ li_description = $ line_item ->desc () ?? esc_html__ ('Event Ticket ' , 'event_espresso ' );
231243 $ line_items [] = [
232- 'name ' => substr (wp_strip_all_tags ($ line_item -> name ( )), 0 , 126 ),
244+ 'name ' => substr (wp_strip_all_tags ($ this -> gateway_data_formatter -> formatLineItemName ( $ line_item , $ this -> payment )), 0 , 125 ),
233245 'quantity ' => $ line_item ->quantity (),
234- 'description ' => substr (wp_strip_all_tags ($ li_description ), 0 , 125 ),
246+ 'description ' => substr (wp_strip_all_tags ($ this -> gateway_data_formatter -> formatLineItemDesc ( $ line_item , $ this -> payment ) ), 0 , 125 ),
235247 'unit_amount ' => [
236248 'currency_code ' => $ this ->currency_code ,
237249 'value ' => (string ) $ item_money ,
@@ -402,23 +414,15 @@ protected function getSimplifiedAmountBreakdown(): array
402414 protected function getSimplifiedItems (): array
403415 {
404416 // Simplified single line item.
405- $ line_items = [];
406- $ primary_registrant = $ this ->transaction ->primary_registration ();
407- $ event_obj = $ primary_registrant ->event_obj ();
408- $ name = sprintf (
409- esc_html__ ('Event Registrations from %1$s ' , "event_espresso " ),
410- wp_specialchars_decode (get_bloginfo (), ENT_QUOTES ),
411- );
412- $ description = sprintf (
413- esc_html__ ('Event Registrations from %1$s for %2$s ' , "event_espresso " ),
414- wp_specialchars_decode (get_bloginfo (), ENT_QUOTES ),
415- $ event_obj instanceof EE_Event ? $ event_obj ->name () : esc_html__ ('Event ' , 'event_espresso ' )
416- );
417-
417+ $ line_items = [];
418+ $ primary_registrant = $ this ->transaction ->primary_registration ();
419+ $ event_obj = $ primary_registrant ->event_obj ();
420+ $ name_and_description = $ this ->gateway_data_formatter ->formatOrderDescription ($ this ->payment );
421+
418422 $ line_items [] = [
419- 'name ' => substr (wp_strip_all_tags ($ name ), 0 , 126 ),
423+ 'name ' => substr (wp_strip_all_tags ($ name_and_description ), 0 , 125 ),
420424 'quantity ' => 1 ,
421- 'description ' => substr (wp_strip_all_tags ($ description ), 0 , 2047 ),
425+ 'description ' => substr (wp_strip_all_tags ($ name_and_description ), 0 , 2047 ),
422426 'unit_amount ' => [
423427 'currency_code ' => $ this ->currency_code ,
424428 'value ' => (string ) CurrencyManager::normalizeValue ($ this ->transaction ->remaining () - $ this ->transaction ->tax_total ()),
@@ -427,4 +431,20 @@ protected function getSimplifiedItems(): array
427431 ];
428432 return $ line_items ;
429433 }
434+
435+ /**
436+ * Generates an EE_Payment object but doesn't save it.
437+ */
438+ private function setPaymentPlaceholder (): void
439+ {
440+ $ this ->payment = EE_Payment::new_instance (
441+ [
442+ 'STS_ID ' => EEM_Payment::status_id_pending,
443+ 'TXN_ID ' => $ this ->transaction ->ID (),
444+ 'PMD_ID ' => $ this ->transaction ->payment_method_ID (),
445+ 'PAY_amount ' => 0.00 ,
446+ 'PAY_timestamp ' => time (),
447+ ]
448+ );
449+ }
430450}
0 commit comments