stripe_helper.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if($location->legal_entity_name) {
  24. $name = sprintf("%s (%s)", $location->name, $location->legal_entity_name);
  25. }
  26. else {
  27. $name = $location->name;
  28. }
  29. $address = [
  30. "city" => $location->city,
  31. "country" => $location->country,
  32. "line1" => $location->address_1,
  33. "line2" => $location->address_2,
  34. "postal_code" => $location->postal_code,
  35. "state" => $location->county,
  36. ];
  37. $data = [
  38. "name" => $name,
  39. "email" => $location->email,
  40. "phone" => $location->phone,
  41. "address" => $address,
  42. "metadata" => [
  43. "location_id" => $location->id,
  44. ]
  45. ];
  46. if($location->stripe_customer_id) {
  47. $stripe_customer = $this->client->customers->update($location->stripe_customer_id, $data);
  48. }
  49. else {
  50. $stripe_customer = $this->client->customers->create($data);
  51. }
  52. $location->stripe_customer_id = $stripe_customer->id;
  53. $location->commit();
  54. return $stripe_customer;
  55. }
  56. /**
  57. * @param Test $test
  58. * @param string $price
  59. * @return Invoice
  60. * @throws Exception
  61. */
  62. public function createInvoice(Test $test, string $price): Invoice {
  63. $location = $test->getLocation();
  64. $customer = $this->createOrUpdateCustomer($location);
  65. if($test->stripe_invoice_id) {
  66. return $this->client->invoices->retrieve($test->stripe_invoice_id);
  67. }
  68. $invoice = $this->client->invoices->create([
  69. "collection_method" => "send_invoice",
  70. "customer" => $customer->id,
  71. "metadata" => [
  72. "test_id" => $test->id,
  73. "test_url" => sprintf("https://%s/account/tests/%d", DOMAIN, $test->id),
  74. ],
  75. "days_until_due" => 0,
  76. "payment_settings" => [
  77. "payment_method_types" => [
  78. "card",
  79. ]
  80. ]
  81. ]);
  82. $this->client->invoiceItems->create([
  83. "customer" => $customer->id,
  84. "amount" => bcmul($price, 100),
  85. "currency" => "gbp",
  86. "description" => "Check Policy test fee",
  87. "metadata" => [
  88. "test_id" => $test->id,
  89. ],
  90. "invoice" => $invoice->id,
  91. ]);
  92. $this->client->invoices->finalizeInvoice($invoice->id);
  93. $test->stripe_invoice_id = $invoice->id;
  94. $test->commit();
  95. $this->client->invoices->sendInvoice($invoice->id);
  96. return $invoice;
  97. }
  98. }