stripe_helper.php 2.7 KB

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