Created
March 5, 2026 18:23
-
-
Save rickalday/227808636c72765bfc0f5030f93f2d03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace GiveGoCardless\Actions; | |
| use Give\Donations\Models\Donation; | |
| use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; | |
| use GiveGoCardless\DataTransferObjects\GoCardlessPayment; | |
| use GiveGoCardless\Helpers\GoCardlessResources; | |
| /** | |
| * @since 2.0.0 | |
| */ | |
| class CreateGoCardlessPayment | |
| { | |
| /** | |
| * @since 2.0.0 | |
| * | |
| * @throws PaymentGatewayException | |
| */ | |
| public function __invoke(Donation $donation, string $mandateId): GoCardlessPayment | |
| { | |
| $paymentParams = [ | |
| 'amount' => $donation->amount->formatToMinorAmount(), | |
| 'description' => gocardless_get_payment_description($donation->id), | |
| 'currency' => $donation->amount->getCurrency()->getCode(), | |
| 'links' => [ | |
| 'mandate' => $mandateId, | |
| ], | |
| 'metadata' => [ | |
| 'order_id' => (string) $donation->id, | |
| ], | |
| ]; | |
| $response = \Give_GoCardless_API::create_payment($paymentParams); | |
| if (is_wp_error($response)) { | |
| throw new PaymentGatewayException(sprintf( | |
| __('Error to create payment: %s - %s', 'give-gocardless'), | |
| $response->get_error_code(), | |
| $response->get_error_message() | |
| )); | |
| } | |
| if (isset($response['error'])) { | |
| throw new PaymentGatewayException(sprintf( | |
| __('Error to create payment: %s - %s', 'give-gocardless'), | |
| $response['error']['code'] ?? '', | |
| $response['error']['message'] ?? '' | |
| )); | |
| } | |
| if (empty($response['payments']['id'])) { | |
| throw new PaymentGatewayException(__('Unexpected payment response from GoCardless', 'give-gocardless')); | |
| } | |
| return GoCardlessPayment::fromArray($response['payments']); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment