diff --git a/modules/ppcp-wc-gateway/resources/js/fraudnet.js b/modules/ppcp-wc-gateway/resources/js/fraudnet.js new file mode 100644 index 000000000..11a74b1c5 --- /dev/null +++ b/modules/ppcp-wc-gateway/resources/js/fraudnet.js @@ -0,0 +1,8 @@ +document.addEventListener('DOMContentLoaded', () => { + const script = document.createElement('script'); + script.setAttribute('src', 'https://c.paypal.com/da/r/fb.js'); + + console.log(script) + + document.body.append(script); +}); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 22bb3715f..ca52026d1 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -29,6 +29,8 @@ use WooCommerce\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use WooCommerce\PayPalCommerce\WcGateway\FundingSource\FundingSourceRenderer; use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; +use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\OrderEndpoint; +use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\TransactionUrlProvider; use WooCommerce\PayPalCommerce\WcGateway\Helper\DCCProductStatus; use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; @@ -2118,4 +2120,19 @@ return array( $container->get( 'wcgateway.settings' ) ); }, + 'wcgateway.pay-upon-invoice-order-endpoint' => static function (ContainerInterface $container): OrderEndpoint { + return new OrderEndpoint( + $container->get( 'api.host' ), + $container->get( 'api.bearer' ), + $container->get( 'api.factory.order' ), + $container->get( 'woocommerce.logger.woocommerce' ) + ); + }, + 'wcgateway.pay-upon-invoice-gateway' => static function (ContainerInterface $container): PayUponInvoiceGateway { + return new PayUponInvoiceGateway( + $container->get( 'wcgateway.pay-upon-invoice-order-endpoint' ), + $container->get( 'api.factory.purchase-unit' ), + $container->get( 'woocommerce.logger.woocommerce' ) + ); + }, ); diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php new file mode 100644 index 000000000..6ef341b6b --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php @@ -0,0 +1,120 @@ +host = $host; + $this->bearer = $bearer; + $this->order_factory = $order_factory; + $this->logger = $logger; + } + + /** + * Creates an order. + * + * @param PurchaseUnit[] $items The purchase unit items for the order. + * @return Order + */ + public function create( array $items ): Order { + $data = array( + 'intent' => 'CAPTURE', + 'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL', + 'purchase_units' => array_map( + static function ( PurchaseUnit $item ): array { + return $item->to_array(); + }, + $items + ), + 'payment_source' => array ( + 'pay_upon_invoice' => array( + 'name' => array( + 'given_name' => 'John', + 'surname' => 'Doe', + ), + 'email' => 'buyer@example.com', + 'birth_date' => '1990-01-01', + 'phone' => array( + 'national_number' => '6912345678', + 'country_code' => '49', + ), + 'billing_address' => array( + 'address_line_1' => 'Schönhauser Allee 84', + 'admin_area_2' => 'Berlin', + 'postal_code' => '10439', + 'country_code' => 'DE', + ), + 'experience_context' => array( + 'locale' => 'en-DE', + 'brand_name' => 'EXAMPLE INC', + 'logo_url' => 'https://example.com/logoUrl.svg', + 'customer_service_instructions' => array( + 'Customer service phone is +49 6912345678.', + ), + ), + ), + ), + ); + + $bearer = $this->bearer->bearer(); + $url = trailingslashit( $this->host ) . 'v2/checkout/orders'; + $args = array( + 'method' => 'POST', + 'headers' => array( + 'Authorization' => 'Bearer ' . $bearer->token(), + 'Content-Type' => 'application/json', + 'Prefer' => 'return=representation', + 'PayPal-Client-Metadata-Id' => 'd4e0d7b9-4f75-43f9-9437-d8a57c901585', + 'PayPal-Request-Id' => uniqid( 'ppcp-', true ), + ), + 'body' => wp_json_encode( $data ), + ); + + $response = $this->request( $url, $args ); + if ( is_wp_error( $response ) ) { + throw new RuntimeException($response->get_error_message()); + } + + $json = json_decode( $response['body'] ); + $status_code = (int) wp_remote_retrieve_response_code( $response ); + if ( 201 !== $status_code ) { + throw new PayPalApiException($json, $status_code); + } + + return $this->order_factory->from_paypal_response( $json ); + } +} diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php new file mode 100644 index 000000000..a81f7f535 --- /dev/null +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoiceGateway.php @@ -0,0 +1,96 @@ +id = self::ID; + + $this->method_title = __('Pay Upon Invoice', 'woocommerce-paypal-payments'); + $this->method_description = __('Once you place an order, pay within 30 days. Our payment partner Ratepay will send you payment instructions.', 'woocommerce-paypal-payments'); + $this->title = $this->method_title; + $this->description = $this->method_description; + + $this->init_form_fields(); + $this->init_settings(); + + add_action( + 'woocommerce_update_options_payment_gateways_' . $this->id, + array( + $this, + 'process_admin_options', + ) + ); + + $this->order_endpoint = $order_endpoint; + $this->purchase_unit_factory = $purchase_unit_factory; + $this->logger = $logger; + } + + /** + * Initialize the form fields. + */ + public function init_form_fields() { + $this->form_fields = array( + 'enabled' => array( + 'title' => __( 'Enable/Disable', 'woocommerce-paypal-payments' ), + 'type' => 'checkbox', + 'label' => __( 'Pay upon Invoice', 'woocommerce-paypal-payments' ), + 'default' => 'yes' + ), + ); + } + + public function process_payment($order_id) + { + $wc_order = new WC_Order( $order_id ); + $wc_order->update_status('on-hold', __('Awaiting Pay Upon Invoice payment', 'woocommerce-paypal-payments')); + + $purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order ); + + try { + $this->order_endpoint->create(array( $purchase_unit )); + } catch (RuntimeException $exception) { + $error = $exception->getMessage(); + $this->logger->error($error); + + // TODO display error in the screen + } + } +} diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 8ec2f9965..854da583d 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -205,6 +205,9 @@ class WCGatewayModule implements ModuleInterface { if ( $dcc_applies->for_country_currency() ) { $methods[] = $container->get( 'wcgateway.credit-card-gateway' ); } + + $methods[] = $container->get('wcgateway.pay-upon-invoice-gateway'); + return (array) $methods; } ); @@ -268,6 +271,25 @@ class WCGatewayModule implements ModuleInterface { return $disabler->handler( (array) $methods ); } ); + + add_action('wp_footer', function () { ?> + + get('wcgateway.url'); + wp_enqueue_script( + 'ppcp-fraudnet', + trailingslashit($gateway_module_url) . 'assets/js/fraudnet.js', + array(), + 1 + ); + }); } /** diff --git a/modules/ppcp-wc-gateway/webpack.config.js b/modules/ppcp-wc-gateway/webpack.config.js index c9c6c2b55..fcc5648bb 100644 --- a/modules/ppcp-wc-gateway/webpack.config.js +++ b/modules/ppcp-wc-gateway/webpack.config.js @@ -7,6 +7,7 @@ module.exports = { target: 'web', entry: { 'gateway-settings': path.resolve('./resources/js/gateway-settings.js'), + 'fraudnet': path.resolve('./resources/js/fraudnet.js'), }, output: { path: path.resolve(__dirname, 'assets/'),