squareetlabs /
LaravelAuthorizeNet
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Squareetlabs\AuthorizeNet\Services; |
||
| 4 | |||
| 5 | use Squareetlabs\AuthorizeNet\AuthorizeNet; |
||
| 6 | use Squareetlabs\AuthorizeNet\Contracts\CardInterface; |
||
| 7 | use Carbon\Carbon; |
||
| 8 | use net\authorize\api\controller as AnetController; |
||
| 9 | use net\authorize\api\contract\v1 as AnetAPI; |
||
| 10 | |||
| 11 | class CardService extends AuthorizeNet implements CardInterface |
||
| 12 | { |
||
| 13 | /** @var array<string, mixed> */ |
||
| 14 | private array $data = [ |
||
| 15 | 'nameOnCard' => null, |
||
| 16 | 'cardNumber' => null, |
||
| 17 | 'expMonth' => null, |
||
| 18 | 'expYear' => null, |
||
| 19 | 'amount' => null, // in cents |
||
| 20 | 'brand' => null, // visa, master, etc. |
||
| 21 | 'type' => null, // credit, debit, gift, etc. |
||
| 22 | 'cvv' => null, |
||
| 23 | ]; |
||
| 24 | |||
| 25 | public function setNumbers(int $cardNumbers): CardInterface |
||
| 26 | { |
||
| 27 | $this->data['cardNumber'] = (string) $cardNumbers; |
||
| 28 | |||
| 29 | return $this; |
||
| 30 | } |
||
| 31 | |||
| 32 | public function setCVV(int $cvvNumbers): CardInterface |
||
| 33 | { |
||
| 34 | $this->data['cvv'] = $cvvNumbers; |
||
| 35 | |||
| 36 | return $this; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function setNameOnCard(string $name): CardInterface |
||
| 40 | { |
||
| 41 | $this->data['nameOnCard'] = $name; |
||
| 42 | |||
| 43 | return $this; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function setAmountInCents(int $cents): CardInterface |
||
| 47 | { |
||
| 48 | $this->data['amount'] = $cents; |
||
| 49 | |||
| 50 | return $this; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function setAmountInDollars(float $amount): CardInterface |
||
| 54 | { |
||
| 55 | $this->data['amount'] = (int) ($amount * 100); // normalize to cents |
||
| 56 | |||
| 57 | return $this; |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getNumbers(): ?string |
||
| 61 | { |
||
| 62 | return $this->data['cardNumber']; |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getCVV(): ?int |
||
| 66 | { |
||
| 67 | return $this->data['cvv']; |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getNameOnCard(): ?string |
||
| 71 | { |
||
| 72 | return $this->data['nameOnCard']; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function getAmountInCents(): ?int |
||
| 76 | { |
||
| 77 | return $this->data['amount']; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getAmountInDollars(): ?float |
||
| 81 | { |
||
| 82 | $amount = $this->data['amount']; |
||
| 83 | |||
| 84 | return $amount !== null ? $amount / 100 : null; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function charge(): AnetAPI\CreateTransactionResponse |
||
| 88 | { |
||
| 89 | $creditCard = new AnetAPI\CreditCardType(); |
||
| 90 | $creditCard->setCardNumber($this->getNumbers()); |
||
| 91 | $creditCard->setExpirationDate( |
||
| 92 | sprintf('%04d-%02d', $this->getExpYear(), $this->getExpMonth()) |
||
| 93 | ); |
||
| 94 | $creditCard->setCardCode((string) $this->getCVV()); |
||
| 95 | |||
| 96 | $paymentOne = new AnetAPI\PaymentType(); |
||
| 97 | $paymentOne->setCreditCard($creditCard); |
||
| 98 | |||
| 99 | $customerData = new AnetAPI\CustomerDataType(); |
||
| 100 | $customerData->setType('individual'); |
||
| 101 | $customerData->setId((string) $this->user->id); |
||
| 102 | $customerData->setEmail($this->user->email ?? ''); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 103 | |||
| 104 | $transactionRequestType = new AnetAPI\TransactionRequestType(); |
||
| 105 | $transactionRequestType->setTransactionType('authCaptureTransaction'); |
||
| 106 | $transactionRequestType->setAmount($this->getAmountInDollars()); |
||
| 107 | $transactionRequestType->setPayment($paymentOne); |
||
| 108 | $transactionRequestType->setCustomer($customerData); |
||
| 109 | |||
| 110 | $request = new AnetAPI\CreateTransactionRequest(); |
||
| 111 | $request->setMerchantAuthentication($this->getMerchantAuthentication()); |
||
| 112 | $request->setRefId($this->getRefId()); |
||
| 113 | $request->setTransactionRequest($transactionRequestType); |
||
| 114 | |||
| 115 | $controller = new AnetController\CreateTransactionController($request); |
||
| 116 | |||
| 117 | return $this->execute($controller); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function setExpMonth(int|string $month): CardInterface |
||
| 121 | { |
||
| 122 | $intMonth = is_string($month) |
||
| 123 | ? (int) Carbon::parse($month)->format('m') |
||
| 124 | : $month; |
||
| 125 | |||
| 126 | $this->data['expMonth'] = $intMonth; |
||
| 127 | |||
| 128 | return $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | public function setExpYear(int|string $year): CardInterface |
||
| 132 | { |
||
| 133 | $yearStr = (string) $year; |
||
| 134 | $intYear = strlen($yearStr) === 2 |
||
| 135 | ? (int) ('20' . $yearStr) |
||
| 136 | : (int) $year; |
||
| 137 | |||
| 138 | $this->data['expYear'] = $intYear; |
||
| 139 | |||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function setType(string $type = 'Credit'): CardInterface |
||
| 144 | { |
||
| 145 | $this->data['type'] = $type; |
||
| 146 | |||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function setBrand(string $brandName): CardInterface |
||
| 151 | { |
||
| 152 | $this->data['brand'] = $brandName; |
||
| 153 | |||
| 154 | return $this; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getBrand(): ?string |
||
| 158 | { |
||
| 159 | return $this->data['brand']; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function getType(): ?string |
||
| 163 | { |
||
| 164 | return $this->data['type']; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function getExpMonth(): ?int |
||
| 168 | { |
||
| 169 | return $this->data['expMonth']; |
||
| 170 | } |
||
| 171 | |||
| 172 | public function getExpYear(): ?int |
||
| 173 | { |
||
| 174 | return $this->data['expYear']; |
||
| 175 | } |
||
| 176 | } |
||
| 177 |