1
0

stripe_helper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "shipping" => null,
  47. "metadata" => [
  48. "location_id" => $location->id,
  49. ]
  50. ];
  51. if($location->stripe_customer_id) {
  52. $stripe_customer = $this->client->customers->update($location->stripe_customer_id, $data);
  53. }
  54. else {
  55. $stripe_customer = $this->client->customers->create($data);
  56. }
  57. $location->stripe_customer_id = $stripe_customer->id;
  58. $location->commit();
  59. return $stripe_customer;
  60. }
  61. /**
  62. * @param Test $test
  63. * @param string $price
  64. * @return Invoice
  65. * @throws Exception
  66. */
  67. public function createInvoice(Test $test, string $price): Invoice {
  68. $location = $test->getLocation();
  69. $customer = $this->createOrUpdateCustomer($location);
  70. if($test->stripe_invoice_id) {
  71. return $this->client->invoices->retrieve($test->stripe_invoice_id);
  72. }
  73. $invoice = $this->client->invoices->create([
  74. "collection_method" => "send_invoice",
  75. "customer" => $customer->id,
  76. "metadata" => [
  77. "test_id" => $test->id,
  78. "test_url" => sprintf("https://%s/account/tests/%d", DOMAIN, $test->id),
  79. ],
  80. "days_until_due" => 0,
  81. "payment_settings" => [
  82. "payment_method_types" => [
  83. "card",
  84. ]
  85. ]
  86. ]);
  87. $this->client->invoiceItems->create([
  88. "customer" => $customer->id,
  89. "amount" => bcmul($price, 100),
  90. "currency" => "gbp",
  91. "description" => "Check Policy test fee",
  92. "metadata" => [
  93. "test_id" => $test->id,
  94. ],
  95. "invoice" => $invoice->id,
  96. ]);
  97. $this->client->invoices->finalizeInvoice($invoice->id);
  98. $test->stripe_invoice_id = $invoice->id;
  99. $test->commit();
  100. $this->client->invoices->sendInvoice($invoice->id);
  101. return $invoice;
  102. }
  103. }