selimppc

ZIP-PAY Library

May 1st, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.97 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Helpers;
  4.  
  5. use App\Helpers\TntEnquiry;
  6. use App\Product;
  7. use DOMDocument;
  8. use DateTimeZone;
  9. use DateTime;
  10. use zipMoney;
  11. use zipMoney\Request\Version;
  12. use Illuminate\Support\Facades\DB;
  13.  
  14. class ZipPay
  15. {
  16.     /**
  17.      * @param $invoice_number
  18.      * @param $order_head
  19.      * @param $order_detail
  20.      * @param $customer_data
  21.      * @param $delivery_data
  22.      * @throws zipMoney\Exception
  23.      */
  24.     public static function call_to_server($invoice_number, $order_head, $order_detail, $customer_data, $delivery_data, $zip_pay_invoice)
  25.     {
  26.  
  27.         /*
  28.         SANDBOX
  29.             Merchant ID: 3075
  30.             API Signature: TRjrjwZSprkucEtpL9BNOZPpkjydDIAk0Rlh7iYYbc0=
  31.             Unique Id: daef00bf-536f-41b2-9bc7-2f1a47fc1742
  32.          */
  33.  
  34.         /*
  35.          *  Merchant ID: 3075
  36.             API Signature: TRjrjwZSprkucEtpL9BNOZPpkjydDIAk0Rlh7iYYbc0=
  37.             Unique Id: daef00bf-536f-41b2-9bc7-2f1a47fc1742
  38.          */
  39.  
  40.  
  41.         zipMoney\Configuration::$merchant_id  = 3075; //sandbox;
  42.         #zipMoney\Configuration::$merchant_id  = 2485; //production;
  43.  
  44.         zipMoney\Configuration::$merchant_key = 'TRjrjwZSprkucEtpL9BNOZPpkjydDIAk0Rlh7iYYbc0='; //sandbox
  45.         #zipMoney\Configuration::$merchant_key = '3t50JGRyQZeg7X3/00emLUvZYVp4BRLq/j8ty3256ec='; // production
  46.  
  47.         zipMoney\Configuration::$environment  = 'sandbox'; //sandbox;
  48.         #zipMoney\Configuration::$environment  = 'production'; //production;
  49.  
  50.  
  51.         # Initialize the checkout
  52.        $checkout = new \zipMoney\Api\Checkout();
  53.  
  54.         $checkout->request->charge = true;
  55.         $checkout->request->currency_code = "AUD";
  56.         $checkout->request->txn_id = false;
  57.         $checkout->request->order_id =  $invoice_number;
  58.         $checkout->request->in_store = false;
  59.  
  60.         $checkout->request->cart_url    = "http://staging.asimstoys.com.au/mycart";
  61.        
  62.         $checkout->request->success_url =
  63. "http://staging.asimstoys.com.au/zippay/success";
  64.  
  65.         $checkout->request->cancel_url  =
  66. "http://staging.asimstoys.com.au/zippay/cancel/";
  67.  
  68.         $checkout->request->error_url   =
  69. "http://staging.asimstoys.com.au/zippay/error/";
  70.  
  71.         $checkout->request->refer_url   =
  72. "http://staging.asimstoys.com.au/zippay/refer/";
  73.  
  74.         $checkout->request->decline_url =
  75. "http://staging.asimstoys.com.au/zippay/decline/";
  76.  
  77.         $checkout->request->redirect_url =
  78. "http://staging.asimstoys.com.au/zippay/redirect_url/";
  79.  
  80.         // Order Info
  81.         $order = new \zipMoney\Request\Order;
  82.         $order->id = $invoice_number or null;
  83.         $order->tax = 0;
  84.         $order->shipping_tax = 0;
  85.         $order->shipping_value = 0;
  86.         $order->total = $order_head->net_amount + $order->shipping_value;
  87.  
  88.         // Order Item 1
  89.         foreach ($order_detail as  $value)
  90.         {
  91.  
  92.             $order_item = new \zipMoney\Request\OrderItem;
  93.             $product = $customer_data = DB::table('product')->where('id',$value->product_id)->first();
  94.             $order_item->id = $value->product_id;
  95.             $order_item->sku  = $product->slug;
  96.             $order_item->name = $product->title;
  97.             $order_item->price =  $value->price;
  98.             $order_item->quantity = $value->qty;
  99.             $order_item->image_url = 'http://asimstoys.com.au/'.$product->image;
  100.  
  101.             $order->detail[] = $order_item;
  102.         }
  103.  
  104.  
  105.         $checkout->request->order = $order;
  106.  
  107.         // Billing Address
  108.         $billingAddress  = new \zipMoney\Request\Address;
  109.  
  110.  
  111.         $billingAddress->first_name = isset($customer_data->first_name)?$customer_data->first_name : null;
  112.         $billingAddress->last_name = isset($customer_data->first_name)?$customer_data->first_name : null;
  113.         $billingAddress->line1 = isset($customer_data->first_name)?$customer_data->first_name : null;
  114.         $billingAddress->line2 = isset($customer_data->suburb)?$customer_data->suburb : null;
  115.         $billingAddress->country = isset($customer_data->country)?$customer_data->country : null;
  116.         $billingAddress->zip = isset($customer_data->postcode)?$customer_data->postcode : null;
  117.         $billingAddress->city = isset($customer_data->state)?$customer_data->state : null;
  118.         $billingAddress->state = isset($customer_data->state)?$customer_data->state : null;
  119.  
  120.         $checkout->request->billing_address  = $billingAddress;
  121.  
  122.         // Shipping Address
  123.         $shippingAddress = new \zipMoney\Request\Address;
  124.  
  125.         $shippingAddress->first_name = isset($delivery_data->first_name)?$delivery_data->first_name : null;
  126.         $shippingAddress->last_name = isset($delivery_data->first_name)?$delivery_data->first_name:null;
  127.         $shippingAddress->line1 = isset($delivery_data->first_name)? $delivery_data->first_name : null;
  128.         $shippingAddress->line2 = isset($delivery_data->suburb)?$delivery_data->suburb : null;
  129.         $shippingAddress->country = isset($delivery_data->country ) ? $delivery_data->country : null;
  130.         $shippingAddress->zip = isset($delivery_data->postcode) ?$delivery_data->postcode : null;
  131.         $shippingAddress->city = isset($delivery_data->state) ?$delivery_data->state : null;
  132.         $shippingAddress->state = isset($delivery_data->state) ? $delivery_data->state : null;
  133.  
  134.         $checkout->request->shipping_address  = $shippingAddress;
  135.  
  136.  
  137.         // Consumer Info
  138.         $consumer  = new \zipMoney\Request\Consumer;
  139.  
  140.         $consumer->first_name = isset($delivery_data->first_name)?$delivery_data->first_name:null;
  141.         $consumer->last_name = isset($delivery_data->last_name)?$delivery_data->last_name:null;
  142.         $consumer->phone = isset($delivery_data->telephone)?$delivery_data->telephone:null;
  143.         $consumer->email = isset($delivery_data->email)?$delivery_data->email:null;
  144.         $consumer->gender = null;
  145.         $consumer->dob = null;
  146.         $consumer->title = "Mr/Mrs";
  147.  
  148.         $checkout->request->consumer  = $consumer;
  149.         $checkout->request->version = new Version();
  150.         $checkout->request->version->platform = "php";
  151.        
  152.  
  153.         try{
  154.             $response = $checkout->process();
  155.  
  156.             if($response->isSuccess())
  157.             {
  158.                 $arr_response = @$response->toArray();
  159.                 $url = @$arr_response['redirect_url'];
  160.  
  161.                 //Do Something
  162.                 header('Location: '.$url);
  163.  
  164.                 exit();
  165.  
  166.                 #$result = 'Payment Success!';
  167.            } else {
  168.                 //Handle Error
  169.                 $result = 'Invalid Request. Please try again !';
  170.             }
  171.  
  172.         } catch (Exception $e){
  173.  
  174.             echo "<pre>";
  175.             print_r($response);
  176.  
  177.             print "\n";
  178.             print "\n";
  179.             print "Please go back and use another payment";
  180.             print "\n";
  181.  
  182.             exit("Status : Declined ! Please try later !");
  183.  
  184.             // Handle Error
  185.             $result = 'Oops! Something went wrong. Please try again !';
  186.         }
  187.  
  188.         return $result;
  189.  
  190.  
  191.     }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment