You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user/pages/03.commerce2/02.developer-guide/05.payments/10.code-recipes/docs.md
+213-5
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,217 @@ taxonomy:
4
4
category: docs
5
5
---
6
6
7
-
! We need help filling out this section! Feel free to follow the *edit this page* link and contribute.
7
+
If you want to write custom code to programatically manage payments, you can use these code recipes as a starting point.
8
+
-**Create:**
9
+
-[Payment gateway](#creating-a-payment-gateway)
10
+
-[Payment method](#creating-a-payment-method)
11
+
-[Payment](#creating-a-payment)
12
+
-**Load:**
13
+
-[Payment gateway](#loading-a-payment-gateway)
14
+
-[Payment method](#loading-a-payment-method)
15
+
-[Payment](#loading-a-payment)
16
+
-**Manage payments:**
17
+
-[Setting the payment gateway for an order](#setting-the-payment-gateway-for-an-order)
18
+
-[Adding a refunded amount to an existing payment](#adding-a-refunded-amount-to-an-existing-payment)
19
+
-[**Filter payment gateways available for an order**](#filter-payment-gateways-available-for-an-order)
8
20
9
-
- CRUD operations for payments
10
-
- theming/templates
11
-
- payment events
12
-
- describe Payment example module here or on separate page?
21
+
### Creating a payment gateway
22
+
Payment gateways are configuration entities that store the configuration for payment gateway plugins. The configuration keys will vary based on the payment gateway definition. The `Drupal\commerce_payment\Plugin\Commerce\PaymentGatewayBase` class defines `display_label`, `mode`, and `payment_method_types`. In this example, we've also added an `api_key` key.
23
+
```php
24
+
/**
25
+
* id [String]
26
+
* Primary key for this payment gateway.
27
+
*
28
+
* label [String]
29
+
* Label for this payment gateway.
30
+
*
31
+
* weight [Integer]
32
+
* The payment gateway weight.
33
+
*
34
+
* plugin [String]
35
+
* Foreign key of the payment gateway plugin to use.
36
+
*
37
+
* configuration [Array]
38
+
* The plugin configuration.
39
+
*
40
+
* conditions [Array]
41
+
* The conditions that control gateway availability.
In this second example of payment gateway creation, we're separating the payment gateway plugin configuration from the creation of the payment gateway. This alternative approach works just the same but can result in slightly more readable code. Also, there may be times when you only want to update the configuration for an existing payment gateway.
In this example, we assume that we have that the variable `$order` is an object of type `Drupal\commerce_order\Entity\OrderInterface`. In this example, the payment method will expire on Thursday, January 16th, 2020. To create a payment method that does not expire, omit the `expires` key or set it to 0; its default value is 0.
### Filter payment gateways available for an order
171
+
The payment module includes a `FilterPaymentGatewaysEvent` event that can be used whenever filtering available gateways using [Conditions](../../03.core/01.conditions) is not sufficient. In this example, we use the `data` field on `Order` entities to store a list of explicitly excluded payment gateways.
172
+
173
+
First, use the `data` field to store a payment gateway to be excluded.
0 commit comments