Adding more functions to StripeService

This commit is contained in:
Loann M 2019-06-26 18:56:36 +02:00
parent f734d744bd
commit 7cc0a3a06f
3 changed files with 337 additions and 7 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
.idea
###> symfony/framework-bundle ###
/.env.local
/.env.local.php

View File

@ -34,6 +34,12 @@ class User
private $stripe_customer_id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $stripe_account_id;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Subscription", mappedBy="user")
*/
private $subscriptions;
@ -74,6 +80,26 @@ class User
}
/**
* @return mixed
*/
public function getStripeAccountId()
{
return $this->stripe_account_id;
}
/**
* @param mixed $stripe_account_id
* @return User
*/
public function setStripeAccountId($stripe_account_id)
{
$this->stripe_account_id = $stripe_account_id;
return $this;
}
/**
* @return Collection|Subscription[]
*/
public function getSubscriptions(): Collection

View File

@ -60,6 +60,96 @@ class StripeService
return $customer_id;
}
/* CHARGE -------------------------------------------------------------------------------------------------- */
public function pay(User $user, $cart, $token)
{
$this->initKey();
$customer_id = $this->retrieveCustomerId($user, $token);
$count = 0;
foreach ($cart as $product) {
if ($destination_account = $product->getUser()->getStripeAccount()) {
if ($this->createCharge($customer_id, $product, $destination_account)) {
$count++;
}
}
}
return [
'message' => $count . " paid products",
];
}
// Create a charge
public function createCharge($customer_id, $product, $destination_account)
{
try {
\Stripe\Charge::create([
"amount" => $product->getPrice() / 100,
"currency" => "eur",
"customer" => $customer_id,
"description" => "Paiement pour le produit " . $product->getName(),
"metadata"=> "additionnal_informations",
"destination" => [
"account" => $destination_account,
],
]);
$success = true;
} catch (\Exception $e) {
$success = false;
}
return $success;
}
// Retrieve a charge
public function retrieveCharge($charge_id)
{
$this->initKey();
return \Stripe\Charge::retrieve($charge_id);
}
// Refund a charge
public function refundCharge($charge_id)
{
$this->initKey();
$success = false;
try {
\Stripe\Refund::create([
"charge" => $charge_id
]);
$success = true;
$message = "Refund done";
} catch(\Exception $e) {
$message = $e->getMessage();
}
return [
'message' => $message,
'success' => $success
];
}
/* SUBSCRIPTION --------------------------------------------------------------------------------------------- */
// Create a subscription
public function newSubscription(User $user, $plan_id, $token)
{
@ -71,7 +161,7 @@ class StripeService
$em = $this->doctrine->getManager();
// Checking that the user has not already subscribed to this plan
if ($this->subscribed($user, $plan_id)) {
if ($this->userHasSubscribed($user, $plan_id)) {
$message = "Subscription already existing";
@ -103,7 +193,7 @@ class StripeService
$success = true;
} catch (\Stripe\Error\Base $e) {
} catch (\Exception $e) {
$message = $e->getMessage();
}
@ -115,11 +205,41 @@ class StripeService
}
// Cancel a subscription
public function cancelSubscription(Subscription $subscription)
{
$this->initKey();
$subscription_id = $subscription->getStripeId();
$success = false;
$message = "Entity Subscription doesn t have an id";
if ($subscription_id) {
try {
$sub = \Stripe\Subscription::retrieve($subscription_id);
$sub->cancel();
$success = true;
$message = "Subscription cancelled";
} catch(\Exception $e) {
$message = $e->getMessage();
}
}
return [
'message' => $message,
'success' => $success
];
}
// Check if the user has subscribed to the plan or not
public function subscribed(User $user, $plan_id)
public function userHasSubscribed(User $user, $plan_id)
{
$subscriptionRepository = $this->doctrine->getRepository('App\Subscription');
$subscriptionRepository = $this->doctrine->getRepository('App:Subscription');
$customer_id = $user->getStripeCustomerId();
$subscribed = false;
@ -138,8 +258,192 @@ class StripeService
return $subscribed;
}
/* PAYOUT -------------------------------------------------------------------------------------------------- */
public function createPayout(User $user, $product)
{
$this->initStripe();
$success = false;
$message = "User doesnt have an stripe account id";
if ($account_id = $user->getStripeAccountId()) {
try {
\Stripe\Payout::create(
[
"amount" => $product->getPrice(),
"currency" => "eur",
],
[
"stripe_account" => $account_id
]
);
$success = true;
$message = "Payout created";
} catch(\Exception $e) {
$message = $e->getMessage();
}
}
return [
'message' => $message,
'success' => $success
];
}
/* PRODUCT --------------------------------------------------------------------------------------------------- */
// Create a product
public function createProduct($product)
{
$this->initStripe();
if ($product->getStripeId()) {
return $this->updateProduct($product);
}
$url = "url_product";
try {
$prod = \Stripe\Product::create([
"name" => $product->getTitle(),
"type" => "good",
"description" => $product->getDescription(),
"metadata" => [
"mail_vendeur" => $product->getUser() ? $product->getUser()->getUsername() : null,
"entreprise_vendeur" => $product->getUser() ? $product->getUser()->getCompany() : null,
"date_publication" => $product->getDatePublication()->format('d/m/Y'),
"date_max" => $product->getMaxSellDate()->format('d/m/Y'),
"adresse" => $product->getAddress() . ' ' . $product->getCp() . ' ' . $product->getVille(),
"prix HT" => $product->getPriceHT() .'€',
"prix TTC" => $product->getPriceTTC() . '€',
"quantité" => $product->getTotalQuantity(),
],
"url" => $url
]);
$product->setStripeId($prod->id);
return "success";
} catch (\Exception $e) {
return $e->getMessage();
}
}
// Update a product
public function updateProduct($product)
{
$this->initStripe();
$url = "url_product";
try {
\Stripe\Product::update(
$product->getStripeId(),
[
"name" => $product->getTitle(),
"description" => $product->getDescription(),
"metadata" => [
"date_max" => $product->getMaxSellDate()->format('d/m/Y'),
"adresse" => $product->getAddress() . ' ' . $product->getCp() . ' ' . $product->getVille(),
"prix HT" => $product->getPriceHT() .'€',
"prix TTC" => $product->getPriceTTC() . '€',
"quantité" => $product->getTotalQuantity(),
],
"url" => $url
]);
return "success";
} catch (\Exception $e) {
return $e->getMessage();
}
}
// Delete a product
public function deleteProduct($product)
{
$this->initStripe();
try {
$product = \Stripe\Product::retrieve($product->getStripeId());
$product->delete();
$success = true;
$message = "Product deleted";
} catch (\Exception $e) {
$success = false;
$message = $e->getMessage();
}
return [
'message' => $message,
'success' => $success
];
}
/* COUPON -------------------------------------------------------------------------------------------*/
public function createCoupon($amount, $id, $duration, $type = '%')
{
$this->initStripe();
if ($type == "")
$off = "amount_off";
else {
$off = "percent_off";
}
try {
\Stripe\Coupon::create([
$off => $amount,
"duration" => $duration,
// "duration_in_months" => 3,
"id" => $id
]);
$success = true;
$message = "Coupon created";
} catch (\Exception $e) {
$success = false;
$message = $e->getMessage();
}
return [
'message' => $message,
'success' => $success
];
}
public function retrieveCoupon($coupon_token)
{
$this->initStripe();
$coupon = \Stripe\Coupon::retrieve($coupon_token);
return $coupon;
}
// TODO
// resume subscription
// stop subscription
// Invoice
// check when card is not valid anymore, warn the admins
}