1 | <?php |
||
2 | |||
3 | namespace Pronamic\WordPress\Pay\Gateways\Nocks; |
||
4 | |||
5 | use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway; |
||
6 | use Pronamic\WordPress\Pay\Core\PaymentMethods; |
||
7 | use Pronamic\WordPress\Pay\Payments\PaymentStatus as Core_Statuses; |
||
8 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
9 | |||
10 | /** |
||
11 | * Title: Nocks gateway |
||
12 | * Description: |
||
13 | * Copyright: 2005-2019 Pronamic |
||
14 | * Company: Pronamic |
||
15 | * |
||
16 | * @author Reüel van der Steege |
||
17 | * @version 2.0.1 |
||
18 | * @since 1.0.0 |
||
19 | */ |
||
20 | class Gateway extends Core_Gateway { |
||
21 | /** |
||
22 | * Slug of this gateway |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | const SLUG = 'nocks'; |
||
27 | |||
28 | /** |
||
29 | * Client. |
||
30 | * |
||
31 | * @var Client |
||
32 | */ |
||
33 | protected $client; |
||
34 | |||
35 | /** |
||
36 | * Constructs and initializes an Nocks gateway. |
||
37 | * |
||
38 | * @param Config $config Config. |
||
39 | */ |
||
40 | public function __construct( Config $config ) { |
||
41 | parent::__construct( $config ); |
||
42 | |||
43 | $this->set_method( self::METHOD_HTTP_REDIRECT ); |
||
44 | |||
45 | // Supported features. |
||
46 | $this->supports = array( |
||
47 | 'payment_status_request', |
||
48 | ); |
||
49 | |||
50 | // Client. |
||
51 | $this->client = new Client(); |
||
52 | |||
53 | $this->client->set_access_token( $config->access_token ); |
||
54 | $this->client->set_merchant_profile( $config->merchant_profile ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Get supported payment methods. |
||
59 | * |
||
60 | * @see Core_Gateway::get_supported_payment_methods() |
||
61 | */ |
||
62 | public function get_supported_payment_methods() { |
||
63 | return array( |
||
64 | PaymentMethods::GULDEN, |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Start. |
||
70 | * |
||
71 | * @see Core_Gateway::start() |
||
72 | * |
||
73 | * @param Payment $payment The payment. |
||
74 | */ |
||
75 | public function start( Payment $payment ) { |
||
76 | $payment_method = $payment->get_method(); |
||
77 | $currency = $payment->get_total_amount()->get_currency()->get_alphabetic_code(); |
||
78 | $amount = $payment->get_total_amount()->get_value(); |
||
79 | |||
80 | if ( empty( $payment_method ) ) { |
||
81 | $payment_method = PaymentMethods::GULDEN; |
||
82 | } |
||
83 | |||
84 | if ( PaymentMethods::GULDEN === $payment_method ) { |
||
85 | switch ( $currency ) { |
||
86 | case 'EUR': |
||
87 | // Convert to EUR. |
||
88 | $quote = $this->client->get_transaction_quote( 'EUR', 'NLG', $amount, Methods::IDEAL ); |
||
89 | |||
90 | if ( $quote ) { |
||
91 | $amount = $quote->data->target_amount->amount; |
||
92 | $currency = 'NLG'; |
||
93 | } |
||
94 | |||
95 | break; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | $transaction = new Transaction(); |
||
100 | |||
101 | $transaction->payment_id = $payment->get_id(); |
||
102 | $transaction->merchant_profile = $this->config->merchant_profile; |
||
103 | $transaction->description = $payment->get_description(); |
||
104 | $transaction->currency = $currency; |
||
105 | $transaction->amount = $amount; |
||
106 | $transaction->payment_method = Methods::transform( $payment->get_method() ); |
||
107 | $transaction->redirect_url = $payment->get_return_url(); |
||
108 | $transaction->callback_url = add_query_arg( 'nocks_webhook', '', home_url( '/' ) ); |
||
109 | $transaction->description = $payment->get_description(); |
||
110 | |||
111 | if ( null !== $payment->get_customer() ) { |
||
112 | $transaction->locale = $payment->get_customer()->get_locale(); |
||
113 | } |
||
114 | |||
115 | // Issuer. |
||
116 | if ( Methods::IDEAL === $transaction->payment_method ) { |
||
117 | $transaction->issuer = $payment->get_issuer(); |
||
118 | } |
||
119 | |||
120 | // Start transaction. |
||
121 | $result = $this->client->start_transaction( $transaction ); |
||
122 | |||
123 | // Update payment. |
||
124 | if ( isset( $result->data->payments->data[0]->uuid ) ) { |
||
125 | $payment->set_transaction_id( $result->data->uuid ); |
||
126 | } |
||
127 | |||
128 | if ( isset( $result->data->payments->data[0]->metadata->url ) ) { |
||
129 | $payment->set_action_url( $result->data->payments->data[0]->metadata->url ); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Update status of the specified payment. |
||
135 | * |
||
136 | * @param Payment $payment The payment. |
||
137 | */ |
||
138 | public function update_status( Payment $payment ) { |
||
139 | $transaction_id = $payment->get_transaction_id(); |
||
140 | |||
141 | try { |
||
142 | $nocks_payment = $this->client->get_transaction( $transaction_id ); |
||
143 | } catch ( \Exception $e ) { |
||
144 | return; |
||
145 | } |
||
146 | |||
147 | if ( ! $nocks_payment ) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
148 | $payment->set_status( Core_Statuses::FAILURE ); |
||
149 | |||
150 | $this->error = $this->client->get_error(); |
||
151 | |||
152 | return; |
||
153 | } |
||
154 | |||
155 | if ( is_object( $nocks_payment ) && isset( $nocks_payment->data->status ) ) { |
||
156 | $status = Statuses::transform( $nocks_payment->data->status ); |
||
157 | |||
158 | $payment->set_status( $status ); |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 |