Skip to content

Commit

Permalink
Merge pull request #7 from utopia-php/feat-payment-intent
Browse files Browse the repository at this point in the history
Updated to use newer Stripe APIs
  • Loading branch information
eldadfux authored Mar 15, 2023
2 parents ec95250 + b490e0e commit 5c5ca70
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 156 deletions.
39 changes: 19 additions & 20 deletions src/Pay/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,43 +72,40 @@ public function getCurrency(): string
/**
* Make a purchase request
*/
abstract public function purchase(int $amount, string $customerId, string $cardId, array $additionalParams = []): array;
abstract public function purchase(int $amount, string $customerId, string $paymentMethodId, array $additionalParams = []): array;

/**
* Refund payment
*/
abstract public function refund(string $paymentId, int $amount = null): array;
abstract public function refund(string $paymentId, int $amount = null, string $reason = null): array;

/**
* Add a credit card for a customer
* Add a payment method
*/
abstract public function createCard(string $customerId, string $cardId): array;
abstract public function createPaymentMethod(string $customerId, string $type, array $details): array;

/**
* Update credit card
* Update payment method billing details
*/
abstract public function updateCard(string $customerId, string $cardId, string $name = null, int $expMonth = null, int $expYear = null, Address $billingAddress = null): array;
abstract public function updatePaymentMethodBillingDetails(string $paymentMethodId, string $name = null, string $email = null, string $phone = null, array $address = null): array;

/**
* Get credit card
* Update payment method
*/
abstract public function getCard(string $customerId, string $cardId): array;
abstract public function updatePaymentMethod(string $paymentMethodId, string $type, array $details): array;

/**
* List cards
* List payment methods
*/
abstract public function listCards(string $customerId): array;
abstract public function listPaymentMethods(string $customerId): array;

/**
* Remove a credit card for a customer
* Remove payment method
*/
abstract public function deleteCard(string $customerId, string $cardId): bool;
abstract public function deletePaymentMethod(string $customerId): bool;

/**
* Add new customer in the gateway database
* returns the id of the newly created customer
*
* @throws Exception
*/
abstract public function createCustomer(string $name, string $email, array $address = [], string $paymentMethod = null): array;

Expand Down Expand Up @@ -136,15 +133,17 @@ abstract public function updateCustomer(string $customerId, string $name, string
abstract public function deleteCustomer(string $customerId): bool;

/**
* List Customer Payment Methods
* List Payment Methods
*/
abstract public function listCustomerPaymentMethods(string $customerId): array;
abstract public function getPaymentMethod(string $customerId, string $paymentMethodId): array;

/**
* List Customer Payment Methods
* Create setup for accepting future payments
*
* @param string $customerId
* @param array $paymentMethodTypes
* @return array
*/
abstract public function getCustomerPaymentMethod(string $customerId, string $paymentMethodId): array;

abstract public function createFuturePayment(string $customerId, array $paymentMethodTypes = []): array;

/**
Expand Down
103 changes: 53 additions & 50 deletions src/Pay/Adapter/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,62 +31,68 @@ public function getName(): string
/**
* Make a purchase request
*/
public function purchase(int $amount, string $customerId, string $cardId = null, array $additonalParams = []): array
public function purchase(int $amount, string $customerId, string $paymentMethodId = null, array $additionalParams = []): array
{
$path = '/charges';

$path = '/payment_intents';
$requestBody = [
'customer' => $customerId,
'amount' => $amount,
'currency' => $this->currency,
'customer' => $customerId,
'payment_method' => $paymentMethodId,
'off_session' => 'true',
'confirm' => 'true',
];
$requestBody = array_merge($requestBody, $additonalParams);
if (! empty($cardId)) {
$requestBody['source'] = $cardId;
}
$res = $this->execute(self::METHOD_POST, $path, $requestBody);

return $res;
$requestBody = array_merge($requestBody, $additionalParams);
$result = $this->execute(self::METHOD_POST, $path, $requestBody);

return $result;
}

/**
* Refund payment
*/
public function refund(string $paymentId, int $amount = null): array
public function refund(string $paymentId, int $amount = null, string $reason = null): array
{
$path = '/refunds';
$requestBody = ['charge' => $paymentId];
$requestBody = ['payment_intent' => $paymentId];
if ($amount != null) {
$requestBody['amount'] = $amount;
}

if ($reason != null) {
$requestBody['reason'] = $reason;
}

return $this->execute(self::METHOD_POST, $path, $requestBody);
}

/**
* Add a credit card for customer
*/
public function createCard(string $customerId, string $cardId): array
public function createPaymentMethod(string $customerId, string $type, array $paymentMethodDetails): array
{
$path = '/customers/'.$customerId.'/sources';
$path = '/payment_methods';

return $this->execute(self::METHOD_POST, $path, ['source' => $cardId]);
}
$requestBody = [
'type' => $type,
$type => $paymentMethodDetails,
];

/**
* List cards
*/
public function listCards(string $customerId): array
{
$path = '/customers/'.$customerId.'/sources';
// Create payment method
$paymentMethod = $this->execute(self::METHOD_POST, $path, $requestBody);
$paymentMethodId = $paymentMethod['id'];

return $this->execute(self::METHOD_GET, $path);
// attach payment method to the customer
$path .= '/'.$paymentMethodId.'/attach';

return $this->execute(self::METHOD_POST, $path, ['customer' => $customerId]);
}

/**
* List Customer Payment Methods
* List cards
*/
public function listCustomerPaymentMethods(string $customerId): array
public function listPaymentMethods(string $customerId): array
{
$path = '/customers/'.$customerId.'/payment_methods';

Expand All @@ -96,7 +102,7 @@ public function listCustomerPaymentMethods(string $customerId): array
/**
* List Customer Payment Methods
*/
public function getCustomerPaymentMethod(string $customerId, string $paymentMethodId): array
public function getPaymentMethod(string $customerId, string $paymentMethodId): array
{
$path = '/customers/'.$customerId.'/payment_methods/'.$paymentMethodId;

Expand All @@ -106,50 +112,47 @@ public function getCustomerPaymentMethod(string $customerId, string $paymentMeth
/**
* Update card
*/
public function updateCard(string $customerId, string $cardId, string $name = null, int $expMonth = null, int $expYear = null, Address $billingAddress = null): array
public function updatePaymentMethodBillingDetails(string $paymentMethodId, string $name = null, string $email = null, string $phone = null, array $address = null): array
{
$path = '/customers/'.$customerId.'/sources/'.$cardId;
$path = '/payment_methods/'.$paymentMethodId;
$requestBody = [];
$requestBody['billing_details'] = [];
if (! empty($name)) {
$requestBody['name'] = $name;
$requestBody['billing_details']['name'] = $name;
}
if (! empty($expMonth)) {
$requestBody['exp_month'] = $expMonth;
if (! empty($email)) {
$requestBody['billing_details']['email'] = $email;
}
if (! empty($expYear)) {
$requestBody['exp_year'] = $expYear;
if (! empty($phone)) {
$requestBody['billing_details']['phone'] = $phone;
}
if (! is_null($billingAddress)) {
$requestBody['address_city'] = $billingAddress->getCity() ?? null;
$requestBody['address_country'] = $billingAddress->getCountry() ?? null;
$requestBody['address_line1'] = $billingAddress->getLine1();
$requestBody['address_line2'] = $billingAddress->getLine2();
$requestBody['address_state'] = $billingAddress->getState();
$requestBody['address_zip'] = $billingAddress->getPostalCode();
if (! is_null($address)) {
$requestBody['billing_details']['address'] = $address;
}

return $this->execute(self::METHOD_POST, $path, $requestBody);
}

/**
* Get a card
*/
public function getCard(string $customerId, string $cardId): array
public function updatePaymentMethod(string $paymentMethodId, string $type, array $details): array
{
$path = '/customers/'.$customerId.'/sources/'.$cardId;
$path = '/payment_methods/'.$paymentMethodId;

return $this->execute(self::METHOD_GET, $path);
$requestBody = [
$type => $details,
];

return $this->execute(self::METHOD_POST, $path, $requestBody);
}

/**
* Delete a credit card record
*/
public function deleteCard(string $customerId, string $cardId): bool
public function deletePaymentMethod(string $paymentMethodId): bool
{
$path = '/customers/'.$customerId.'/sources/'.$cardId;
$res = $this->execute(self::METHOD_DELETE, $path);
$path = '/payment_methods/'.$paymentMethodId.'/detach';
$this->execute(self::METHOD_POST, $path);

return $res['deleted'] ?? false;
return true;
}

/**
Expand Down
Loading

0 comments on commit 5c5ca70

Please sign in to comment.