stripe_helper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Helpers;
  3. use Exception;
  4. use Location;
  5. use Stripe\Customer;
  6. use Stripe\Invoice;
  7. use Stripe\StripeClient;
  8. use Test;
  9. class StripeHelper {
  10. private StripeClient $client;
  11. public static function getInstance(): self {
  12. return new self(STRIPE_SECRET_KEY);
  13. }
  14. public function __construct(string $private_key) {
  15. $this->client = new StripeClient($private_key);
  16. }
  17. /**
  18. * @param Location $location
  19. * @return Customer
  20. * @throws Exception
  21. */
  22. public function createOrUpdateCustomer(Location $location): Customer {
  23. $customer = $location->getCustomer();
  24. if($location->legal_entity_name) {
  25. $name = sprintf("%s - %s (%s)", $customer->name, $location->name, $location->legal_entity_name);
  26. }
  27. else {
  28. $name = sprintf("%s - %s", $customer->name, $location->name);
  29. }
  30. $data = [
  31. "name" => $name,
  32. "email" => $location->email,
  33. "phone" => $location->phone,
  34. "shipping" => array_filter([
  35. "address" => array_filter([
  36. "city" => $location->city,
  37. "line1" => $location->address_1,
  38. "line2" => $location->address_2,
  39. "postal_code" => $location->postal_code,
  40. "state" => $location->county,
  41. ]),
  42. "name" => $name,
  43. "phone" => $location->phone,
  44. ]),
  45. "metadata" => [
  46. "location_id" => $location->id,
  47. ]
  48. ];
  49. if($location->stripe_customer_id) {
  50. $stripe_customer = $this->client->customers->update($location->stripe_customer_id, $data);
  51. }
  52. else {
  53. $stripe_customer = $this->client->customers->create($data);
  54. }
  55. $location->stripe_customer_id = $stripe_customer->id;
  56. $location->commit();
  57. return $stripe_customer;
  58. }
  59. /**
  60. * @param Test $test
  61. * @param string $price
  62. * @return Invoice
  63. * @throws Exception
  64. */
  65. public function createInvoice(Test $test, string $price): Invoice {
  66. $location = $test->getLocation();
  67. $customer = $this->createOrUpdateCustomer($location);
  68. if($test->stripe_invoice_id) {
  69. return $this->client->invoices->retrieve($test->stripe_invoice_id);
  70. }
  71. $invoice = $this->client->invoices->create([
  72. "collection_method" => "send_invoice",
  73. "customer" => $customer->id,
  74. "metadata" => [
  75. "test_id" => $test->id,
  76. "test_url" => sprintf("https://%s/account/tests/%d", DOMAIN, $test->id),
  77. ],
  78. "days_until_due" => 0,
  79. "payment_settings" => [
  80. "payment_method_types" => [
  81. "card",
  82. ]
  83. ]
  84. ]);
  85. $this->client->invoiceItems->create([
  86. "customer" => $customer->id,
  87. "amount" => bcmul($price, 100),
  88. "currency" => "gbp",
  89. "description" => "Check Policy test fee",
  90. "metadata" => [
  91. "test_id" => $test->id,
  92. ],
  93. "invoice" => $invoice->id,
  94. ]);
  95. $this->client->invoices->finalizeInvoice($invoice->id);
  96. $test->stripe_invoice_id = $invoice->id;
  97. $test->commit();
  98. $this->client->invoices->sendInvoice($invoice->id);
  99. return $invoice;
  100. }
  101. }