Completed
Push — master ( d9c0db...563854 )
by mahdi
03:41
created

Driver::invoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Shetabit\Payment\Abstracts;
4
5
use Shetabit\Payment\Contracts\DriverInterface;
6
use Shetabit\Payment\Invoice;
7
8
abstract class Driver implements DriverInterface
9
{
10
    /**
11
     * Invoice
12
     *
13
     * @var Invoice
14
     */
15
    protected $invoice;
16
17
    /**
18
     * Driver's settings
19
     *
20
     * @var
21
     */
22
    protected $settings;
23
24
    /**
25
     * Driver constructor.
26
     *
27
     * Driver constructor.
28
     * @param Invoice $invoice
29
     * @param $settings
30
     */
31
    abstract public function __construct(Invoice $invoice, $settings);
32
33
    /**
34
     * Set payment amount.
35
     *
36
     * @param $amount
37
     * @return $this
38
     * @throws \Exception
39
     */
40
    public function amount($amount)
41
    {
42
        $this->invoice->amount($amount);
43
44
        return $this;
45
    }
46
47
    /**
48
     * Set a piece of data to the details.
49
     *
50
     * @param $key
51
     * @param $value|null
52
     * @return $this|DriverInterface
53
     */
54
    public function detail($key, $value = null)
55
    {
56
        $key = is_array($key) ? $key : [$key => $value];
57
58
        foreach ($key as $k => $v) {
59
            $this->invoice->detail($key, $value);
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * Set invoice.
67
     *
68
     * @param Invoice $invoice
69
     * @return $this
70
     */
71
    public function invoice(Invoice $invoice)
72
    {
73
        $this->invoice = $invoice;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Retrieve invoice.
80
     *
81
     * @return Invoice
82
     */
83
    public function getInvoice()
84
    {
85
        return $this->invoice;
86
    }
87
88
    /**
89
     * Create payment redirection form.
90
     *
91
     * @param $url
92
     * @param array $data
93
     * @return string
94
     */
95
    public function createRedirectionForm($url, array $data)
96
    {
97
        $output = '<html><head><meta charset="utf-8" />';
98
        $output .= '<script>function pay() { document.forms["pay"].submit(); }</script>';
99
        $output .= '</head><body onload="pay();"><form name="pay" method="post" action="'.$url.'">';
100
        if ( !empty($data) ) {
101
            foreach ($data as $key => $value) {
102
                $output.='<input type="hidden" name="'.$key.'" value="'.$value.'">';
103
            }
104
        }
105
        $output.='<input type="submit" value="doing the payment...">';
106
        $output.='</form></body></html>';
107
108
        return $output;
109
    }
110
111
    /**
112
     * Purchase the invoice
113
     *
114
     * @return string
115
     */
116
    abstract public function purchase();
117
118
    /**
119
     * Pay the invoice
120
     *
121
     * @return mixed
122
     */
123
    abstract public function pay();
124
125
    /**
126
     * Verify the payment
127
     *
128
     * @return mixed
129
     */
130
    abstract public function verify();
131
}
132