Browse Source

dev 2020-01

Max F 5 years ago
parent
commit
21c077dc93
3 changed files with 212 additions and 45 deletions
  1. 163 0
      src/Lib/Payment/Payplug_lib.php
  2. 33 43
      src/Lib/Payment/Stripe_lib.php
  3. 16 2
      src/Templates/PhpTemplate.php

+ 163 - 0
src/Lib/Payment/Payplug_lib.php

@@ -0,0 +1,163 @@
+<?php
+
+namespace KarmaFW\Lib\Payment;
+
+//use \Payplug\Payplug;
+
+
+class Payplug_lib
+{
+	// infos API: https://docs.payplug.com/api/paymentpage.html
+
+	public static function getKeys($type=null)
+	{
+		if (in_array(PAYPLUG_ENV, ['LIVE', 'PROD'])) {
+			if (! defined('PAYPLUG_LIVE_PUBLIC_KEY') || ! defined('PAYPLUG_LIVE_SECRET_KEY')) {
+				return false;
+			}
+			$payplug_public_key = PAYPLUG_LIVE_PUBLIC_KEY;
+			$payplug_secret_key = PAYPLUG_LIVE_SECRET_KEY;
+
+		} else {
+			if (! defined('PAYPLUG_TEST_PUBLIC_KEY') || ! defined('PAYPLUG_TEST_SECRET_KEY')) {
+				return false;
+			}
+			$payplug_public_key = PAYPLUG_TEST_PUBLIC_KEY;
+			$payplug_secret_key = PAYPLUG_TEST_SECRET_KEY;
+		}
+		if ($type == 'public') 	{
+			return $payplug_public_key;
+		}
+		if ($type == 'secret') 	{
+			return $payplug_secret_key;
+		}
+		return [
+			'public' => $payplug_public_key,
+			'secret' => $payplug_secret_key,
+		];
+	}
+
+
+	public static function getCustomerData($client, $shipping_address, $delivery_type=null)
+	{
+		$payment_data = [
+			'title'               => ($client['gender'] == 'female') ? 'mrs' : 'mr',
+			'first_name'          => $client['firstname'],
+			'last_name'           => $client['firstname'],
+			'mobile_phone_number' => $client['phone'],
+			'email'               => $client['email'],
+			'address1'            => $shipping_address['address'],
+			'postcode'            => $shipping_address['zipcode'],
+			'city'                => $shipping_address['city'],
+			'country'             => 'FR',
+			'language'            => 'fr'
+		];
+
+		if ($delivery_type) {
+			$payment_data['delivery_type'] = $delivery_type;
+		}
+
+		return $payment_data;
+	}
+
+
+	/* API Hosted page */
+
+	public static function paymentPayplugHosted($client, $shipping_address, $total_price_ttc, $order_id=null)
+	{
+
+		if (substr($client['phone'], 0, 1) == '0') {
+			$client['phone'] = '+33' . substr($client['phone'], 1);
+		}
+		//pre($client, 1);
+
+		$payplug_secret_key = self::getKeys('secret');
+		\Payplug\Payplug::setSecretKey($payplug_secret_key);
+
+		$payment_data = [
+			'amount'           => $total_price_ttc * 100,
+			'currency'         => 'EUR',
+			'save_card'        => false,
+			'billing'          => self::getCustomerData($client, $shipping_address),
+			'shipping'        => self::getCustomerData($client, $shipping_address, 'BILLING'),
+			'hosted_payment' => [
+				'return_url'   => 'http://' . $_SERVER['SERVER_NAME'] . '/payment/payplug/payment-ok?id=' . $order_id,
+				'cancel_url'   => 'http://' . $_SERVER['SERVER_NAME'] . '/payment/payplug/payment-cancel?id=' . $order_id,
+			],
+			'notification_url' => 'http://' . $_SERVER['SERVER_NAME'] . '/payment/payplug/payment-notification?id=' . $order_id,
+			'metadata'      => [
+				'order_id' => $order_id,
+			]
+		];
+		//pre($payment_data, 1);
+
+		$payment = \Payplug\Payment::create($payment_data);
+
+		$payment_url = $payment->hosted_payment->payment_url;
+		$payment_id = $payment->id;
+
+		return [
+			'payment_url' => $payment_url,
+			'payment_id' => $payment_id,
+		];
+	}
+
+
+
+	/* API Lightbox */
+	public static function paymentPayplugLightbox($client, $shipping_address, $total_price_ttc, $order_id=null)
+	{
+
+		if (substr($client['phone'], 0, 1) == '0') {
+			$client['phone'] = '+33' . substr($client['phone'], 1);
+		}
+		//pre($client, 1);
+
+		$payplug_secret_key = self::getKeys('secret');
+		\Payplug\Payplug::setSecretKey($payplug_secret_key);
+
+
+		$payment = \Payplug\Payment::create(
+			[
+			    'amount'         => $total_price_ttc * 100,
+			    'currency'       => 'EUR',
+			    'billing'        => self::getCustomerData($client, $shipping_address),
+			    'shipping'       => self::getCustomerData($client, $shipping_address, 'BILLING'),
+			    'hosted_payment' => array(
+			        'return_url' => 'http://' . $_SERVER['SERVER_NAME'] . '/payment/payplug/payment-ok?id=' . $order_id,
+			        'cancel_url' => 'http://' . $_SERVER['SERVER_NAME'] . '/payment/payplug/payment-cancel?id=' . $order_id,
+			    ),
+			    'notification_url' => 'http://' . $_SERVER['SERVER_NAME'] . '/payment/payplug/payment-notification?id=' . $order_id,
+			]
+		);
+
+		return $payment;
+	}
+
+
+	/* API payplug.js */
+	public static function paymentPayplugPayplugJs($token, $client, $shipping_address, $total_price_ttc, $order_id=null)
+	{
+		if (substr($client['phone'], 0, 1) == '0') {
+			$client['phone'] = '+33' . substr($client['phone'], 1);
+		}
+
+		$payplug_secret_key = self::getKeys('secret');
+		\Payplug\Payplug::setSecretKey($payplug_secret_key);
+
+		$payment = \Payplug\Payment::create(array(
+			'amount'         => $total_price_ttc * 100,
+			'currency'       => 'EUR',
+			'payment_method' => $token,
+			'billing'        => self::getCustomerData($client, $shipping_address),
+			'shipping'       => self::getCustomerData($client, $shipping_address, 'BILLING'),
+			'notification_url' => 'http://karma:dev@' . $_SERVER['SERVER_NAME'] . '/payment/payplug/payment-notification?id=' . $order_id,
+		));
+
+		return $payment;
+	}
+
+
+
+}
+

+ 33 - 43
src/Lib/Payment/Stripe_lib.php

@@ -8,18 +8,8 @@ use \Stripe\Stripe;
 class Stripe_lib
 {
 
-	public function paymentChargeByCard($card, $customer_id=null, $payment_label='', $payment_price=0, $save_customer=false, $new_customer_email=null, $new_customer_description=null)
+	public static function getKeys($type=null)
 	{
-		/*
-		$card = [
-			'number' => '4242424242424242',
-			'exp_month' => 12,
-			'exp_year' => 2020,
-			'cvc' => '314',
-		];
-		*/
-
-
 		if (STRIPE_ENV == 'LIVE') {
 			if (! defined('STRIPE_LIVE_PUBLIC_KEY') || ! defined('STRIPE_LIVE_SECRET_KEY')) {
 				return false;
@@ -34,9 +24,33 @@ class Stripe_lib
 			$stripe_public_key = STRIPE_TEST_PUBLIC_KEY;
 			$stripe_secret_key = STRIPE_TEST_SECRET_KEY;
 		}
+		if ($type == 'public') 	{
+			return $stripe_public_key;
+		}
+		if ($type == 'secret') 	{
+			return $stripe_secret_key;
+		}
+		return [
+			'public' => $stripe_public_key,
+			'secret' => $stripe_secret_key,
+		];
 
-		\Stripe\Stripe::setApiKey($stripe_secret_key);
+	}
+
+
+	public static function paymentChargeByCard($card, $customer_id=null, $payment_label='', $payment_price=0, $save_customer=false, $new_customer_email=null, $new_customer_description=null)
+	{
+		/*
+		$card = [
+			'number' => '4242424242424242',
+			'exp_month' => 12,
+			'exp_year' => 2020,
+			'cvc' => '314',
+		];
+		*/
 
+		$stripe_secret_key = self::getKeys('secret');
+		\Stripe\Stripe::setApiKey($stripe_secret_key);
 
 
 		// Warning: l'API \Stripe\Token requiert que l'on ai vérifié son téléphone sur notre compte Stripe
@@ -45,7 +59,7 @@ class Stripe_lib
 		  'card' => $card,
 		]);
 
-		$result = \KarmaFW\Lib\Payment\Stripe_lib::paymentCharge($client_secret, $customer_id, $payment_label, $payment_price, $save_customer, $new_customer_email, $new_customer_description);
+		$payment_result = \KarmaFW\Lib\Payment\Stripe_lib::paymentCharge($client_secret, $customer_id, $payment_label, $payment_price, $save_customer, $new_customer_email, $new_customer_description);
 
 
 		/*
@@ -97,7 +111,7 @@ class Stripe_lib
 		)
 		*/
 
-		return ! empty($result['payment_accepted']);
+		return $payment_result;
 	}
 
 
@@ -113,23 +127,10 @@ class Stripe_lib
 		// TEST CB valide  => visa: 4242 4242 4242 4242  /  mastercard: 5555 5555 5555 4444
 		// TEST CB refusée => visa: 4242 4242 4242 1214  /  mastercard: 5555 5555 5555 7777
 
-		if (STRIPE_ENV == 'LIVE') {
-			if (! defined('STRIPE_LIVE_PUBLIC_KEY') || ! defined('STRIPE_LIVE_SECRET_KEY')) {
-				return false;
-			}
-			$stripe_public_key = STRIPE_LIVE_PUBLIC_KEY;
-			$stripe_secret_key = STRIPE_LIVE_SECRET_KEY;
-
-		} else {
-			if (! defined('STRIPE_TEST_PUBLIC_KEY') || ! defined('STRIPE_TEST_SECRET_KEY')) {
-				return false;
-			}
-			$stripe_public_key = STRIPE_TEST_PUBLIC_KEY;
-			$stripe_secret_key = STRIPE_TEST_SECRET_KEY;
-		}
-
 		$stripe_error = '';
 		$payment_ok = false;
+
+		$stripe_secret_key = self::getKeys('secret');
 		\Stripe\Stripe::setApiKey($stripe_secret_key);
 
 		try {
@@ -203,25 +204,14 @@ class Stripe_lib
 
 
 
+	// API Methode Stripe.js ajax
+
 	public function paymentIntentInit($unit, $quantity=1, $optionnal_data=[])
 	{
 		// Paiement en 2 temps, via Stripe.js - 1/2  (method 1)
 		// Methode appelée en ajax
 
-		if (in_array(STRIPE_ENV, ['LIVE', 'PROD'])) {
-			if (! defined('STRIPE_LIVE_PUBLIC_KEY') || ! defined('STRIPE_LIVE_SECRET_KEY')) {
-				return false;
-			}
-			$stripe_secret_key = STRIPE_LIVE_SECRET_KEY;
-
-		} else {
-			if (! defined('STRIPE_TEST_PUBLIC_KEY') || ! defined('STRIPE_TEST_SECRET_KEY')) {
-				return false;
-			}
-			$stripe_secret_key = STRIPE_TEST_SECRET_KEY;
-		}
-		
-
+		$stripe_secret_key = self::getKeys('secret');
         \Stripe\Stripe::setApiKey($stripe_secret_key);
 
         $currency = 'eur';

+ 16 - 2
src/Templates/PhpTemplate.php

@@ -195,14 +195,28 @@ class PhpTemplate
 
 			// variables. ex: {$user_name} ==> John
 			if (true) {
-				preg_match_all('/{\$([a-zA-Z0-9_\[\]\']+)}/', $content, $regs, PREG_SET_ORDER);
+				preg_match_all('/{\$([a-zA-Z0-9._\[\]\']+)}/', $content, $regs, PREG_SET_ORDER);
 				foreach($regs as $reg) {
 					$var = $reg[1];
+					$var_parts = explode(".", $var);
 					
-					if (isset(${$var})) {
+					if (count($var_parts) > 1) {
+						// $variable.key  ==> $variable['key']
+
+						$var = ${ array_shift($var_parts) };
+						foreach ($var_parts as $part) {
+							$var = $var[ $part ];
+						}
+
+						$replaced = $var;
+						$content = str_replace($reg[0], $replaced, $content);
+
+					} else if (isset(${$var})) {
+						// $variable
 						$replaced = ${$var};
 						$content = str_replace($reg[0], $replaced, $content);
 					} else {
+
 						// if variable not exists, replace with empty string
 						$content = str_replace($reg[0], '', $content);
 					}